Leaderboard
Showing:
Binary Search in forest
Easy Accuracy: 53.12% Submissions: 2045 Points: 2

There are n tree in a forest. Heights of the trees is stored in array tree[ ]. If ith tree is cut at height h, the wood obtained is tree[i]-h, given that tree[i]>h. If total wood needed is k (not less, neither more) find the height at which every tree should be cut (all trees have to be cut at the same height).


Example 1:

Input:
N = 5, K = 4
tree[] = {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 
tree[] = {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 <= 104
1 <= tree[i] <= 103
1 <= k <= 107

to report an issue on this page.

Editorial

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

Yes

All Submissions

My Submissions:

Login to access your submissions.

Binary Search in forest

Output Window