# Buying Sweets | CodeChef Solution

Hello coders, today we are going to solve Buying Sweets CodeChef Solution whose Problem Code is BUYING2.

Contents

Banknotes in the state of Strangeland don’t have any regulated values like 1, 5, 10, 20, 50, 100, etc. In fact, it’s possible to see any positive integer on a banknote of Strangeland. Indeed, this isn’t the most convenient thing.

Ann is working as a sweet seller at a shop in Strangeland. Every kind of sweets in this shop has its own cost, and sweets of the same kind have the same cost.

Customers in Strangeland are strange. A customer points at some kind of sweets and gives several banknotes to the seller. This means that he wants to buy a positive number of sweets of that kind. He doesn’t tell the exact number of sweets he wants to buy. The only thing Ann knows is: an ‘adequate’ customer won’t give any extra banknotes. It means that if you throw away any banknote, the resulting amount of money won’t be enough to buy the wanted number of sweets.

Ann has to determine the number of sweets the customer wants. Help Ann write a program which determines this number or tells that it’s impossible.

## Input Format

The first line of the input contains a single integerÂ T, the number of test cases (no more than 20).Â TÂ test cases follow. Each test case consists of two lines. The first of these lines contains two integersÂ NÂ andÂ XÂ (1 â‰¤Â N,Â XÂ â‰¤ 100) separated by a single space.Â NÂ is the number of banknotes given by the customer.Â XÂ is the cost of a single sweet of the chosen kind. The second of these lines containsÂ NÂ space-separated integersÂ AiÂ (1 â‰¤Â AiÂ â‰¤ 100), the values of the banknotes.

## Output Format

For each test case output exactly one line containing a single integer:

• -1Â if the customer is inadequate and has given extra banknotes, or
• K, the number of sweets the customer wants to buy. If there are several possible answers, output theÂ largestÂ of them.

Example

Sample Input

``````3
4 7
10 4 8 5
1 10
12
2 10
20 50``````

Sample Output

``````-1
1
7``````

Explanation

In the first test case, the total amount of money received from the customer is 27. He can’t buy more than 3 sweets with this amount. If you throw away the banknote of value 5 (or of value 4), it will still be possible to buy 3 sweets. Hence the customer is inadequate.

In the second test case, it’s clear that he wants to buy just one sweet (note that this number should be positive).

In the third test case, the total amount of money received is 70, which is enough for 7 sweets. The customer might want to buy only 6 sweets, but we are interested in the largest possible answer.

### C++

```#include <bits/stdc++.h>
using namespace std;

int main()
{
int t;
cin >> t;
while (t--)
{
int n , x,sum=0;
cin >>n >> x;
int arr[n];
for (int i =0; i<n ; i++){
cin >> arr[i];
sum += arr[i];
}
sort(arr,arr+n);
int res = sum / x;
int res2 = (sum - arr[0])/ x;

if (res == res2){
cout << -1<<endl;
}else{
cout << sum/ x <<endl;
}
}
return 0;
}```

### Python

```T= int(input())
for i in range(T):
N,X= map(int, input().split())
li=list(map(int, input().split()))
li.sort()
li.reverse()
total_money= sum(li)
total_sweets= total_money//X
li.pop()
new_sum= sum(li)
if new_sum//X==total_sweets:
print(-1)
else:
print(total_sweets)
```

### 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 scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-->0){
int n = scan.nextInt();
int x = scan.nextInt();
int[] arr = new int[n];
int sum = 0;
for(int i=0 ; i<n ; i++){
arr[i] = scan.nextInt();
sum+=arr[i];
}
Arrays.sort(arr);
if(sum%x>=arr[0]){
System.out.println(-1);
}
else{
System.out.println(sum/x);
}
}
}
}
```

Disclaimer: The above Problem (Buying Sweets) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.