Programming Practice challenge : Fredo and Array Update

  Programming Practice challenge Fredo and Array Update            Hacker Earth Problem.

    Problem:

Fredo and Array Update
Max. score: 100

Fredo is assigned a new task today. He is given an array A containing N integers. His task is to update all elements of array to some minimum value x , that is, A[i]=x ; 1iN such that sum of this new array is strictly greater than the sum of the initial array. Note that x should be as minimum as possible such that sum of the new array is greater than the sum of the initial array.

Input Format:
First line of input consists of an integer N denoting the number of elements in the array A.
Second line consists of N space separated integers denoting the array elements.

Output Format:
The only line of output consists of the value of x.

Input Constraints:
1N105
1A[i]1000

SAMPLE INPUT
 
5
1 2 3 4 5
SAMPLE OUTPUT
 
4
Explanation

Initial sum of array =1+2+3+4+5=15
When we update all elements to 4, sum of array =4+4+4+4+4=20 which is greater than 15.
Note that if we had updated the array elements to 3sum=15 which is not greater than 15. So, 4 is the minimum value to which array


Solution in Java:

import java.util.*;
public class Array{

     public static void main(String []args) throws Exception{
        Scanner sc=new Scanner(System.in);
        int size=sc.nextInt();
        int a[]=new int[size];
        int sum=0;
        for(int i=0;i<size;i++)
        {
            a[i]=sc.nextInt();
            sum+=a[i];
        }
        sum+=size;
        int avg=sum/size;
        System.out.println(avg);
        
        
     }
}

Comments