Geeksforgeeks

Error

×

There are n trees in a forest. The heights of the trees is stored in array **tree[], **where **tree[i]** denotes the height of the **i ^{th}** tree in the forest. If the

**Example 1:**

**Input:
**n = 5, k = 4
nums[] = {2, 3, 6, 2, 4}
**Output: **3**
Explanation: **Wood collected by cutting trees
at height 3 = 0 + 0 + (6-3) + 0 + (4-3) = 4
hence 3 is to be subtracted from all numbers.
Since 2 is lesser than 3, nothing gets
subtracted from it.

**Example 2:**

**Input:
**n = 6, k = 8
nums[] = {1, 7, 6, 3, 4, 7}
**Output: **4**
****Explanation: **Wood collected by cutting trees
at height 4 = 0+(7-4)+(6-4)+0+0+(7-4) = 8

**Your Task:**

Your task is to complete the function **find_height()**. This function takes the array **tree[ ]**, and the integers **n** and **k** as input parameters and returns the height at which trees have to be cut. If no positive integer value of height is possible, return **-1**.

**Expected Time Complexity:** O(n log h)

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

**Constraints: **

1 <= n <= 10^{4}

1 <= tree[i] <= 10^{3}

1 <= k <= 10^{7}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

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?

YesLoading...

Binary Search in forest

...