Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 5761 |

@blackshadows | 5701 |

@akhayrutdinov | 5111 |

@mb1973 | 4989 |

@Quandray | 4944 |

@saiujwal13083 | 4506 |

@sanjay05 | 3762 |

@marius_valentin_dragoi | 3516 |

@sushant_a | 3459 |

@verma_ji | 3341 |

@KshamaGupta | 3318 |

Complete Leaderboard | |

Handle | Score |

@aroranayan999 | 938 |

@bt8816103042 | 739 |

@SHOAIBVIJAPURE | 428 |

@codeantik | 412 |

@SherlockHolmes3 | 407 |

@neverevergiveup | 348 |

@mahlawatep | 347 |

@shalinibhataniya1097 | 343 |

@murarry3625 | 333 |

@saiujwal13083 | 326 |

@rohitanand | 314 |

Complete Leaderboard |

Given an array **Arr[]** of size **N **and an integer **K**, you have to add the first two minimum elements of the array until all the elements are greater than or equal to **K** and find the number of such operations required.

**Example 1:**

**Input:
**N = 6, K = 6
Arr[] = {1, 10, 12, 9, 2, 3}
**Output:** 2
**Explanation:** First we add **(1 + 2)**, now the new list
becomes **3 10 12 9 3**, then we add **(3 + 3), **now the
new list becomes **6 10 12 9**, Now all the elements
in the list are greater than **6**. Hence the output
is **2** i:e **2** operations are required to do this.

**Example 2:**

**Input:
**N = 4, K = 4
Arr[] = {5, 4, 6, 4}
**Output:** 0
**Explanation:** Every element in the given array
is greater than or equal to **K**.

**Your Task:**

You don't need to read input or print anything. Your task is to complete the function **minOperations()** which takes array of integers **arr, n** and **k **as parameters and returns an integer denoting the answer. If it is not possible to make elements greater than or equal to** K**, return **-1**.

**Expected Time Complexity:** O(N*logN)

**Expected Auxiliary Space:** O(N)

**Constraints :**

1 ≤ N, K ≤ 10^{5}

1 ≤ Arr[i] ≤ 10^{6}

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes
Adding Array Elements

...