Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Problem Description

There is given an integer array A of size N denoting the heights of N trees.

Lumberjack Ojas needs to chop down B metres of wood. It is an easy job for him since he has a nifty new woodcutting machine that can take down forests like wildfire. However, Ojas is only allowed to cut a single row of trees.

Ojas's machine works as follows: Ojas sets a height parameter H (in metres), and the machine raises a giant sawblade to that height and cuts off all tree parts higher than H (of course, trees not higher than H meters remain intact). Ojas then takes the parts that were cut off. For example, if the tree row contains trees with heights of 20, 15, 10, and 17 metres, and Ojas raises his sawblade to 15 metres, the remaining tree heights after cutting will be 15, 15, 10, and 15 metres, respectively, while Ojas will take 5 metres off the first tree and 2 metres off the fourth tree (7 metres of wood in total).

Ojas is ecologically minded, so he doesn't want to cut off more wood than necessary. That's why he wants to set his sawblade as high as possible. Help Ojas find the maximum integer height of the sawblade that still allows him to cut off at least B metres of wood.

NOTE:

• The sum of all heights will exceed B, thus Ojas will always be able to obtain the required amount of wood.

• Problem Constraints

1 <= N <= 106

1 <= A[i] <= 106

1 <= B <= 2106

Input Format

First argument is an integer array A of size N denoting the heights of each tree(in metres).

Second argument is an integer B.

Output Format

Return a single integer denoting maximum integer height of the sawblade that still allows him to cut off at least B metres of wood.

Example Input

Input 1:

``` A = [20, 15, 10, 17]
B = 7
```

Input 2:

``` A = [4, 42, 40, 26, 46]
B = 20
```

Example Output

Output 1:

``` 15
```

Output 2:

``` 36
```

Example Explanation*

Explanation 1:

``` If you the height parameter to 15 then you can chop:
5 metres from first tree
0 metres from second tree
0 metres from third tree
2 metres from fourth tree
So in total you chopped 7 metres of wood.
```

Explanation 2:

``` If you the height parameter to 36 then you can chop:
0 metres from first tree
6 metres from second tree
4 metres from third tree
0 metres from fourth tree
10 metres from fifth tree
So in total you chopped 20 metres of wood.
```

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a question? Checkout Sample Codes for more details.
Hints
• Hint 1
• Solution Approach
• Complete Solution

## Discussion

Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College/University Name
Job Title
Job Title
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer