Geeksforgeeks

Error

×

Leaderboard

Showing:

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

@Ibrahim Nash | 6420 |

@blackshadows | 6376 |

@mb1973 | 5578 |

@Quandray | 5231 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 4510 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3522 |

@sushant_a | 3459 |

@verma_ji | 3412 |

Complete Leaderboard | |

Handle | Score |

@codersgroup18 | 714 |

@Manikanta punnam | 587 |

@sriramgoparaju99 | 532 |

@praveenbgp6 | 525 |

@yashkaril4 | 517 |

@prankursharma31 | 489 |

@rdakka | 467 |

@sonamnigam1999 | 441 |

@sonamkumari63928 | 427 |

@purohitmn02 | 427 |

@shubhamstudent5 | 419 |

Complete Leaderboard |

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 <= 10^{4}

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

1 <= k <= 10^{7}

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
Binary Search in forest

...