Minimum Number Of Taps To Water Garden Problem Statement

You are required to determine the minimum number of taps that need to be opened to water an entire one-dimensional garden defined along the x-axis, which begins at 0 and concludes at N. There are N + 1 taps located at points [0, 1, 2, ..., N].

Given an integer N and an array 'ranges' of size N + 1, where the i-th tap can water the garden from (i - ranges[i]) to (i + ranges[i]), find the minimum number of taps to open to fully water the garden. Return -1 if it's impossible to water the entire garden.

Example:

Input:
T = 1
N = 5
ranges = [3, 4, 1, 1, 0, 0]
Output:
1
Explanation:

Opening the tap at position 0 covers positions [0, 6].

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 104
  • 0 <= ranges[i] <= 100

Note:

You do not need to print anything; the output will be handled by the system.
Be the first one to answer
Add answer anonymously...
Ola Cabs Software 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