Thursday 25 June 2015

HackerEarth Questions Asked on 24th June 2015

Q)Monk and the Magical Candy Bags
---------------------------------------------------------------------------------

Our Monk loves candy!
While taking a stroll in the park, he stumbled upon N Bags with candies. The i'th of these bags contains Ai candies.
He picks up a bag, eats all the candies in it and drops it on the ground. But as soon as he drops the bag, the number of candies in the bag increases magically! Say the bag that used to contain X candies (before eating), now contains [X/2] candies! ,where [x] is the greatest integer less than x (Greatest Integer Function).
Amazed by the magical spell, Monk can now have a lot more candies! But he has to return home in K minutes. In a singleminute,Monk can consume all the candies in a single bag, regardless of the number of candies in it.
Find the maximum number of candies that Monk can consume.
Input:
First line contains an integer TT test cases follow.
First line of each test case contains two space-separated integers N and K.
Second line of each test case contains N space-separated integers,the number of candies in the bags.
Output:
Print the answer to each test case in a new line.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 105
0 ≤ K ≤ 105
0 ≤ Ai ≤ 1010

Sample Input                       Sample Output
1
5 3
2 1 7 4 2               14





Answer
---------------------------------------------------------------------------------

import java.io.BufferedReader;
import java.io.InputStreamReader;


class TestClass {
    public static void main(String args[] ) throws Exception {

     BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       String line = br.readLine();
       int N = Integer.parseInt(line);
       int monk=0;
       for (int i = 0; i < N; i++) {
        line=br.readLine();
        String str[]=new String[2];
        str=line.split(" ");
        int a=Integer.parseInt(str[0]);
        int b=Integer.parseInt(str[1]);        
        int arr[]=new int[a];
       
        line=br.readLine();
        String st1[]=new String[a];
        st1=line.split(" ");
        for(int l=0;l<st1.length;l++){
        arr[l]=Integer.parseInt(st1[l]);
        }
           for(int j=0;j<b;j++){
            long max=0;
              int d=0;
for(int k=0;k<arr.length-1;k++){
if(arr[k]>arr[k+1]&&arr[k]>max){
max=arr[k];
d=k;
}else if(arr[k]<arr[k+1]&&arr[k+1]>max){
max=(long)arr[k+1];
d=k+1;
}
}
arr[d]/=2;
monk+=max;
}  
           }
       System.out.print(monk);
           
        
       }
}




No comments:

Post a Comment