plz tell whats wrong in my code

plz tell me whats wrong in my code

question link-link text

import java.util.Scanner;
class practice
{
public static void main(String[] arg)
{
	Scanner kb=new Scanner(System.in);
	int test=kb.nextInt();
	for(int j=0;j<test;j++)
	{
		int size=kb.nextInt();
		int arr[] =new int [size];
		int cust=kb.nextInt();
		for(int i=0;i<arr.length;i++)
		arr[i]=kb.nextInt();
		int sum=0;
		for(int i=0;i<arr.length;i++)
		sum=sum+arr[i];
		if((sum/cust)<size)
		System.out.println("-1");
		else
		System.out.println(sum/cust);
	}
}

}

Heres the bug-

Compilation Successful
Input (stdin)
1
5 5
1 1 1 1 1
Your Output
-1
Expected Output
1

Here, he gave 5 bank notes of 1, and one sweet costs $1. So he can buy one sweet, but your code flags him as inadequate.

The problem is here-

if((sum/cust)<size)

This is not a valid condition to check for adequate customers. To see if a customer is adequate or not, check for presence of a redundant note, meaning if you remove that note from total, is the number of sweet still same? If yes, then the person is redundant. That’s what I feel.

Make changes and get back to me with result :slight_smile:

https://www.codechef.com/viewsolution/8618823
This is my AC solution…actually your approach is not correct.
Feel free to ask any query.

but how to find which note is to be remove…removing randomly or what!

You can find it by removing all notes one by one and checking. Meaning, make a loop, remove one note from sum and see if its redundant or not, then remove next one… until we reach the end. If somewhere you find a note is redundant, mark it and print -1. Else print sum/cust.

1 Like

@vijju123
Also it can be done alternatively…I used sort function and then removed first note.
It was easy to check.

1 Like

Thanks bro…!

and hatsoff to your explanation

and now i feel like “yeah i have over the right place and free to ask anything…beacuse mentors are there to help”
thanks!

1 Like

Ya, this is also correct, because the smallest note is the one to prove redundant. If its not redundant, then all notes larger than it are.

Glad we were able to help you dear :).

//