Balls and Boxes codechef problem Problem Code: BALLBOX
Balls And Boxes
Problem
You have balls and boxes. You want to divide the balls into boxes such that:
- Each box contains balls.
- No two boxes contain the same number of balls.
Determine if it is possible to do so.
Input Format
- The first line contains a single integer — the number of test cases. Then the test cases follow.
- The first and only line of each test case contains two space-separated integers and — the number of balls and the number of boxes respectively.
Output Format
For each test case, output YES if it is possible to divide the balls into boxes such that the conditions are satisfied. Otherwise, output NO.
You may print each character of YES and NO in uppercase or lowercase (for example, yes, yEs, Yes will be considered identical).
Sample 1:
4 3 4 30 3 2 2 1 1
NO YES NO YES
Explanation:
Test Case 1: It is not possible to divide the balls into boxes such that each box contains balls.
Test Case 2: One way to divide the balls into boxes is the following: .
Test Case 3: It is not possible to divide the balls into boxes such that no two boxes contain the same number of balls.
Test Case 4: We can divide ball into box
Solution
#include <iostream>
using namespace std;
int main() {
// your code goes here
int t ;
cin >> t;
while(t--){
int n,k, x;
cin >> n >> k ;
x = (k * (k + 1)) / 2;
if(n >= x){
cout << "YES" << endl;
}
else{
cout << "NO" << endl;
}
}
}
Code Explained
According to condition we have to put atleast single ball in a box and no two box contain same number of balls
First box contain atleast 1 ball and second box contain atleast 2 balls like this 3 number box contain atleast 3 balls ,
so the number of total balls must be more then or equals to
1 + 2 + 3 = 6
for fullfill all conditions.
like that if we have k number of box then the least number of balls for fulfill all condition must be more then
k * (k + 1) / 2
balls ( which is sum of natural number from 1 to k )


Comments
Post a Comment
Please Enter Your View And Feedback About This Page