304 North Cardinal St.
Dorchester Center, MA 02124

# Balls and Boxes CodeChef Solution

## Problem – Balls and Boxes CodeChef Solution

You have N balls and K boxes. You want to divide the N balls into K boxes such that:

• Each box contains ≥1 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 T — the number of test cases. Then the test cases follow.
• The first and only line of each test case contains two space-separated integers N and K — 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 N balls into K 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).

• 1≤T≤10^5
• 1≤N≤10^9
• 1≤K≤10^4

### Sample 1:

``````Input:
4
3 4
30 3
2 2
1 1
Output:
NO
YES
NO
YES``````

### Explanation:

Test Case 1: It is not possible to divide the 3 balls into 4 boxes such that each box contains ≥1 balls.

Test Case 2: One way to divide the 30 balls into 3 boxes is the following: [5,9,16].

Test Case 3: It is not possible to divide the 2 balls into 2 boxes such that no two boxes contain the same number of balls.

Test Case 4: We can divide 1 ball into 1 box.

## Balls and Boxes CodeChef Solution in C++17

``````#include <iostream>
using namespace std;

int main() {
long long int t,n,k,sum;
cin>>t;
while(t--)
{
cin>>n>>k;
sum=(k*(k+1)/2);
if(sum<=n)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;

}
}
return 0;
}
``````

## Balls and Boxes CodeChef Solution in Java

``````/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- > 0)
{
int n = in.nextInt();
int k = in.nextInt();

if(n >= ((k*(k+1))/2))
System.out.println("Yes");
else
System.out.println("No");
}
}
}
``````

## Balls and Boxes CodeChef Solution in Pyth 3

``````# cook your dish here
t = int(input())
for _ in range(t):
n,k = map(int,input().split())
if n >= k*(k+1)/2:
print("YES")
else:
print("NO")``````
##### Balls and Boxes CodeChef Solution Review:

In our experience, we suggest you solve this Balls and Boxes CodeChef Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Balls and Boxes CodeChef Solution

Find on CodeChef

##### Conclusion:

I hope this Balls and Boxes CodeChef Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions