Jesse and Cookies
Link will be apear in 15 seconds.
Well done! you have successfully gained access to Decrypted Link.
Code:
import java.util.PriorityQueue; public class JesseAndCookies { public static void main(String[] args) { int[] arr = {1, 2, 3, 9, 10, 12}; int k = 7; increaseSweetness(arr,k); } static void increaseSweetness(int[] arr, int k) { //Make a MinHeap PriorityQueue<Integer> pq = new PriorityQueue<>(); //Add all the elements to pq for (int i = 0; i < arr.length; i++) { pq.add(arr[i]); } //Initialise a counter variable which maintains no. of operations int count = 0; //Loop until there is only one element present in pq while (pq.size() != 1) { //if the root of min heap is lesser than k then we need to add the two min value if (pq.peek() < k) { int sum = 1 * pq.poll() + 2 * pq.poll(); pq.add(sum); count++; } //else if root of min heap is greater than k that means all the value in the pq is greater than k since it is a min Heap //Therefore we break the loop else { break; } } //after combining min element also we are not able to create all value greater than k then we return "-1" if (pq.size() == 1) { System.out.println("-1"); } else { System.out.println(count); } } }