- Amazon, Arrays, Flipkart, interview-preparation, Mathematical, Sorting

Minimum sprinkers required to be turned on to water the plants

Given an array arr[] consisting of N integers, where the ith element represents the range of a sprinkler i.e [i-arr[i], i+arr[i]] it can water, the task is to find the minimum number of the sprinkler to be turned on to water every plant at the gallery. If it is not possible to water every plant, then print -1.Note: If arr[i] = -1, then the sprinkler cannot be turned on.Examples:Input: arr[ ] = {-1, 2, 2, -1, 0, 0}Output: 2Explanation: One of the possible way is:Turn on the sprinkler at index 2, it can water the plants in the range [0, 4].Turn on the sprinkler at index 2, it can water the plants in the range [5, 5].Therefore, turning two sprinklers on can water all the plants. Also, it is the minimum possible count of sprinklers to be turned on.Input: arr[ ] = {2, 3, 4, -1, 2, 0, 0, -1, 0}Output: -1Approach: The above problem can be solved using the greedy technique. The idea is to first sort the range by left boundary and then traversing ranges from left and in each iteration select the rightmost boundary a sprinkler can cover having the left boundary in the current range. Follow the steps below to solve the problem:Initialize a vector say V to store the range of every sprinkler as a pair.Traverse the array arr[] and if arr[i] is not equal to -1 then push the pair (i-arr[i], i+arr[i]) in the vector V.Sort the vector of pairs in ascending order by the first element.Initialize 2 variables say res, and maxRight to store the minimum sprinklers to be turned on and to store the rightmost boundary of an array.Initialize a variable say i as 0 to iterate over the V.Iterate until maxRight is less than N and perform the following steps:If i is equal to V.size() or V[i].first is greater than maxRight then print -1 and return.Store the right boundary of the current sprinkler in the variable say currMax.Now iterate until i+1 is less than V.size() and V[i+1].first is less than or equal to maxRight then in each iteration increment i by 1 and update currMax as currMax = min(currMax, V[i].second).If currMax is less than the maxRight then print -1 and return.Update maxRight as maxRight = currMax+1 then Increment res and i by 1.Finally, after completing the above step, print the res as the answer.Below is the implementation of the above approach:C++#include using namespace std;  int minSprinklers(int arr[], int N){            vector V;        for (int i = 0; i < N; i++) {        if (arr[i] > -1) {            V.push_back(                pair(i – arr[i], i + arr[i]));        }    }            sort(V.begin(), V.end());              int maxRight = 0;            int res = 0;      int i = 0;              while (maxRight < N) {                                    if (i == V.size() || V[i].first > maxRight) {            return -1;        }                        int currMax = V[i].second;                                  while (i + 1 < V.size()               && V[i + 1].first