Commonly asked Data Structures and Algorithms Problems by big tech and different solution approaches with code in Java and C

Powered by Blogger.

Wednesday, May 24, 2017

Jesse and Cookies Solution


Problem:

Given k you need to have elements greater than k 
if a element is less than k perform :
( Least no+2* 2nd least number).

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        PriorityQueue<Integer> heap=new PriorityQueue<Integer>();
        Scanner ab=new Scanner(System.in);
        int n=ab.nextInt();
        int k=ab.nextInt();
        int count=0;
         
            while(ab.hasNext())
            {
            heap.offer(ab.nextInt());
        }
        while(true)
            {
            if(heap.peek()<k)
                {
                count++;
                int temp=heap.poll();
                if(heap.size()<=0)
                    {
                    System.out.println("-1");
                    System.exit(0);
                }
                int temp2=heap.poll();
                heap.offer((temp+(2*temp2)));
            }
            else
                break;
        }
        System.out.println(count);
    }
}

0 Comments:

Post a Comment

Stats