Furthest Building You Can Reach

In this problem, Ninja prefers jumping over building roofs rather than walking through the streets. The heights of the buildings in his city are represented by an array HEIGHTS, where HEIGHT[i] is the height of the ith building. Ninja starts his journey from the 1st building and can move to the next building in one step.

If the height of the ith building is greater than or equal to the (i+1)th building, Ninja simply jumps to the next building. Otherwise, he uses either the difference HEIGHTS[i+1] - HEIGHTS[i] in bricks or one ladder.

With a limited number of bricks and ladders, Ninja wants to know the farthest building he can reach using them optimally.

Example:

Input:
T = 1
N = 5, BRICKS = 10, LADDERS = 1
HEIGHTS = [4, 2, 7, 6, 9]
Output:
4
Explanation:

With the given bricks and ladders, Ninja can reach up to the 4th building.

Constraints:

  • 1 <= T <= 10
  • 2 <= N <= 105
  • 1 <= HEIGHTS[i] <= 106
  • 0 <= BRICKS <= 104
  • 0 <= LADDERS < N

Input:

The first line contains an integer ‘T’ denoting the number of test cases. Each test case consists of three integers ‘N’, ‘BRICKS’ and ‘LADDERS’, followed by a line of ‘N’ integers representing the heights of the buildings.

Output:

For each test case, print a single integer denoting the maximum index (1-based) of the building Ninja can reach.

Note:

No need to print anything; just implement the function to return the appropriate result.
Be the first one to answer
Add answer anonymously...
Whitehat jr React Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter