OVRFWBAS - Editorial

PROBLEM LINK:

https://www.codechef.com/RSC2017/problems/OVRFWBAS

Author: spraveenkumar

DIFFICULTY:

EASY

PROBLEM:

There are n baskets each capable of carrying k goods without being unstable. But initially the 'i’th basket have Ai goods. The initial state may be unstable. Find out what is the number of more goods that can be added possibly by rearranging the goods across different baskets. If it can’t be made stable, output OFERFLOW.

QUICK EXPLANATION:

There are n baskets with k capacity. So the total number of goods that can be carried is k x n. As long as the total number of goods across all the baskets is less than kn, we can add goods to it. When they are equal, we can add 0 goods, otherwise it’s OVERFLOW.

EXPLANATION:

There are n baskets with k capacity each. The baskets maybe unstable initially which can be altered. The total number of goods that can be carried is kn. If the total goods, doesn’t exceed kn, we can add more goods(possibly 0). Otherwise, it is OVERFLOW. But a more efficient way, is to calculate the relative number of goods in each basket. That is, we find the sum of Ai-k. When this value is negative, it means, that many goods can be added more. When this is positive, it is overflowing already. In case of 0, it is stable but no more can be added.

AUTHOR’s SOLUTION:


import java.util.Scanner;
class Basket{
    public static void main(String[] args){
	Scanner sc = new Scanner(System.in);
	int t = sc.nextInt();
	while(t-- != 0){
	    int n = sc.nextInt();
	    int k = sc.nextInt();
	    int i,a, diff=0;
	    for(i=0;i<n;i++){
		a = sc.nextInt();
		diff+=a-k;
	    }
	    if(diff>0)
		System.out.println("OVERFLOW");
	    else
		System.out.println(-diff);
	}
	sc.close();
    }
}

what is wrong in my approach

class overflowing
{
public static void main(String[] args)
{

Scanner sc=new Scanner(System.in);

    int t=sc.nextInt();

    while(t!=0)

    {

    int n=sc.nextInt();

    long sum=0;

    long k=sc.nextLong();

    for(int i=0;i<n;i++)

    {

    sum=sum+sc.nextLong();

    }

    long r=k*n;

    if(r==sum)
            System.out.println("0");

    else if(r>sum)
            System.out.println(r-sum);

    else
            System.out.println("OVERFLOW");

   t--; 

  }

}

}

@vivek96 Seems good. The only conflicting test case is
1
1 0
0
Since you cannot add any more it should be 0. But your code outputs OVERFLOW. I apologise for not stating what to do when it is 0 in advance.

//