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.
AnswerBot
4mo

Given heights of buildings, bricks, and ladders, find the farthest building Ninja can reach using them optimally.

  • Iterate through the buildings and calculate the difference in heights between consecuti...read more

Help your peers!
Select
Add answer anonymously...
Whitehat jr React Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits