Open In App

Count minimum number of fountains to be activated to cover the entire garden

Improve
Improve
Like Article
Like
Save
Share
Report

There is a one-dimensional garden of length N. In each position of the N length garden, a fountain has been installed. Given an array a[]such that a[i] describes the coverage limit of ith fountain. A fountain can cover the range from the position max(i – a[i], 1) to min(i + a[i], N). In beginning, all the fountains are switched off. The task is to find the minimum number of fountains needed to be activated such that the whole N-length garden can be covered by water.

Examples:

Input: a[] = {1, 2, 1}
Output: 1
Explanation:
For position 1: a[1] = 1, range = 1 to 2
For position 2: a[2] = 2, range = 1 to 3
For position 3: a[3] = 1, range = 2 to 3
Therefore, the fountain at position a[2] covers the whole garden.
Therefore, the required output is 1.

Input: a[] = {2, 1, 1, 2, 1} 
Output:

 

Approach: The problem can be solved using Dynamic Programming. Follow the steps below to solve the problem: 

  • traverse the array and for every array index, i.e. ith fountain, find the leftmost fountain up to which the current fountain covers.
  • Then, find the rightmost fountain that the leftmost fountain obtained in the above step covers up to and update it in the dp[] array.
  • Initialize a variable cntFount to store the minimum number of fountains that need to be activated.
  • Now, traverse the dp[] array and keep activating the fountains from the left that covers maximum fountains currently on the right and increment cntFount by 1. Finally, print cntFount as the required answer.

Below is the implementation of the above approach.

C++14




// C++ program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum
// number of fountains to be
// activated
int minCntFoun(int a[], int N)
{
 
    // dp[i]: Stores the position of
    // rightmost fountain that can
    // be covered by water of leftmost
    // fountain of the i-th fountain
    int dp[N];
     
    // initializing all dp[i] values to be -1,
    // so that we don't get garbage value
      for(int i=0;i<N;i++){
          dp[i]=-1;
    }
 
    // Stores index of leftmost fountain
    // in the range of i-th fountain
    int idxLeft;
 
    // Stores index of rightmost fountain
    // in the range of i-th fountain
    int idxRight;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        idxLeft = max(i - a[i], 0);
        idxRight = min(i + (a[i] + 1), N);
        dp[idxLeft] = max(dp[idxLeft],
                          idxRight);
    }
 
    // Stores count of fountains
    // needed to be activated
    int cntfount = 1;
 
    idxRight = dp[0];
 
    // Stores index of next fountain
    // that needed to be activated
    // initializing idxNext with -1
    // so that we don't get garbage value
    int idxNext=-1;
 
    // Traverse dp[] array
    for (int i = 0; i < N; i++)
    {
        idxNext = max(idxNext,
                      dp[i]);
 
        // If left most fountain
        // cover all its range
        if (i == idxRight)
        {
            cntfount++;
            idxRight = idxNext;
        }
    }
 
    return cntfount;
}
 
// Driver Code
int main()
{
    int a[] = { 1, 2, 1 };
    int N = sizeof(a) / sizeof(a[0]);
    cout << minCntFoun(a, N);
}


Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG {
 
    // Function to find minimum
    // number of fountains to be
    // activated
    static int minCntFoun(int a[], int N)
    {
 
        // dp[i]: Stores the position of
        // rightmost fountain that can
        // be covered by water of leftmost
        // fountain of the i-th fountain
        int[] dp = new int[N];
        for(int i=0;i<N;i++)
        {
             dp[i]=-1;
        }
 
        // Stores index of leftmost fountain
        // in the range of i-th fountain
        int idxLeft;
 
        // Stores index of rightmost fountain
        // in the range of i-th fountain
        int idxRight;
 
        // Traverse the array
        for (int i = 0; i < N; i++)
        {
            idxLeft = Math.max(i - a[i], 0);
            idxRight = Math.min(i + (a[i] + 1), N);
            dp[idxLeft] = Math.max(dp[idxLeft],
                                   idxRight);
        }
 
        // Stores count of fountains
        // needed to be activated
        int cntfount = 1;
 
        // Stores index of next fountain
        // that needed to be activated
        int idxNext = 0;
        idxRight = dp[0];
 
        // Traverse dp[] array
        for (int i = 0; i < N; i++)
        {
            idxNext = Math.max(idxNext, dp[i]);
 
            // If left most fountain
            // cover all its range
            if (i == idxRight)
            {
                cntfount++;
                idxRight = idxNext;
            }
        }
        return cntfount;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int a[] = { 1, 2, 1 };
        int N = a.length;
 
        System.out.print(minCntFoun(a, N));
    }
}
 
// This code is contributed by Amit Katiyar


Python3




# Python3 program to implement
# the above approach
 
# Function to find minimum
# number of fountains to be
# activated
 
 
def minCntFoun(a, N):
 
    # dp[i]: Stores the position of
    # rightmost fountain that can
    # be covered by water of leftmost
    # fountain of the i-th fountain
    dp = [0] * N
    for i in range(N):
      dp[i] = -1
 
    # Traverse the array
    for i in range(N):
        idxLeft = max(i - a[i], 0)
        idxRight = min(i + (a[i] + 1), N)
        dp[idxLeft] = max(dp[idxLeft],
                          idxRight)
 
    # Stores count of fountains
    # needed to be activated
    cntfount = 1
 
    idxRight = dp[0]
 
    # Stores index of next fountain
    # that needed to be activated
    idxNext = 0
 
    # Traverse dp[] array
    for i in range(N):
        idxNext = max(idxNext,
                      dp[i])
 
        # If left most fountain
        # cover all its range
        if (i == idxRight):
            cntfount += 1
            idxRight = idxNext
 
    return cntfount
 
 
# Driver code
if __name__ == '__main__':
 
    a = [1, 2, 1]
    N = len(a)
 
    print(minCntFoun(a, N))
 
# This code is contributed by Shivam Singh


C#




// C# program to implement
// the above approach
using System;
class GFG {
 
    // Function to find minimum
    // number of fountains to be
    // activated
    static int minCntFoun(int[] a, int N)
    {
        // dp[i]: Stores the position of
        // rightmost fountain that can
        // be covered by water of leftmost
        // fountain of the i-th fountain
        int[] dp = new int[N];
        for (int i = 0; i < N; i++)
        {
            dp[i] = -1;
        }
 
        // Stores index of leftmost
        // fountain in the range of
        // i-th fountain
        int idxLeft;
 
        // Stores index of rightmost
        // fountain in the range of
        // i-th fountain
        int idxRight;
 
        // Traverse the array
        for (int i = 0; i < N; i++) {
            idxLeft = Math.Max(i - a[i], 0);
            idxRight = Math.Min(i + (a[i] + 1),
                                N);
            dp[idxLeft] = Math.Max(dp[idxLeft],
                                   idxRight);
        }
 
        // Stores count of fountains
        // needed to be activated
        int cntfount = 1;
 
        // Stores index of next
        // fountain that needed
        // to be activated
        int idxNext = 0;
        idxRight = dp[0];
 
        // Traverse []dp array
        for (int i = 0; i < N; i++)
        {
            idxNext = Math.Max(idxNext, dp[i]);
 
            // If left most fountain
            // cover all its range
            if (i == idxRight)
            {
                cntfount++;
                idxRight = idxNext;
            }
        }
        return cntfount;
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int[] a = { 1, 2, 1 };
        int N = a.Length;
 
        Console.Write(minCntFoun(a, N));
    }
}
 
// This code is contributed by gauravrajput1


Javascript




<script>
 
// Javascript program to implement
// the above approach
 
// Function to find minimum
// number of fountains to be
// activated
function minCntFoun(a, N)
{
     
    // dp[i]: Stores the position of
    // rightmost fountain that can
    // be covered by water of leftmost
    // fountain of the i-th fountain
    let dp = [];
    for(let i = 0; i < N; i++)
    {
         dp[i] = -1;
    }
 
    // Stores index of leftmost fountain
    // in the range of i-th fountain
    let idxLeft;
 
    // Stores index of rightmost fountain
    // in the range of i-th fountain
    let idxRight;
 
    // Traverse the array
    for(let i = 0; i < N; i++)
    {
        idxLeft = Math.max(i - a[i], 0);
        idxRight = Math.min(i + (a[i] + 1), N);
        dp[idxLeft] = Math.max(dp[idxLeft],
                               idxRight);
    }
 
    // Stores count of fountains
    // needed to be activated
    let cntfount = 1;
 
    // Stores index of next fountain
    // that needed to be activated
    let idxNext = 0;
    idxRight = dp[0];
 
    // Traverse dp[] array
    for(let i = 0; i < N; i++)
    {
        idxNext = Math.max(idxNext, dp[i]);
 
        // If left most fountain
        // cover all its range
        if (i == idxRight)
        {
            cntfount++;
            idxRight = idxNext;
        }
    }
    return cntfount;
}
 
// Driver Code
let a = [ 1, 2, 1 ];
let N = a.length;
 
document.write(minCntFoun(a, N));
 
// This code is contributed by souravghosh0416
 
</script>


 
 

Output

1





 

Time Complexity: O(N)
Auxiliary Space: O(N)

 Brute Force in python:

Approach:

The brute force approach involves checking all possible combinations of fountains that can be activated to cover the entire garden. For each combination, we check if it covers the entire garden and keep track of the minimum number of fountains required.

Initialize the minimum number of fountains to a very large number.
Use a loop to iterate over all possible combinations of fountains that can be activated.
For each combination, check if it covers the entire garden using a helper function is_covered.
If it does, update the minimum number of fountains required.
Return the minimum number of fountains required.

C++




#include <iostream>
#include <vector>
#include <algorithm>  // Add this header for std::all_of
#include <limits>     // Add this header for std::numeric_limits
 
bool isCovered(const std::vector<int>& activated, const std::vector<int>& fountains) {
    int n = fountains.size();
    std::vector<int> coverage(n, 0);
 
    for (int i : activated) {
        int left = std::max(0, i - fountains[i]);
        int right = std::min(n - 1, i + fountains[i]);
 
        for (int j = left; j <= right; j++) {
            coverage[j] = 1;
        }
    }
 
    // Check if all positions are covered
    return std::all_of(coverage.begin(), coverage.end(), [](int val) { return val == 1; });
}
 
int activateFountains(const std::vector<int>& fountains) {
    int n = fountains.size();
    int minFountains = std::numeric_limits<int>::max();
 
    for (int i = 1; i < (1 << n); i++) {
        std::vector<int> activated;
         
        // Extract indices of activated fountains based on the binary representation of 'i'
        for (int j = 0; j < n; j++) {
            if ((i >> j) & 1) {
                activated.push_back(j);
            }
        }
 
        if (isCovered(activated, fountains)) {
            minFountains = std::min(minFountains, static_cast<int>(activated.size()));
        }
    }
 
    return minFountains;
}
 
int main() {
    // Example usage
    std::vector<int> a1 = {1, 2, 1};
    std::vector<int> a2 = {2, 1, 1, 2, 1};
 
    std::cout << activateFountains(a1) << std::endl; // Output: 1
    std::cout << activateFountains(a2) << std::endl; // Output: 2
 
    return 0;
}


Java




import java.util.ArrayList;
import java.util.List;
 
public class FountainActivation {
    public static boolean isCovered(List<Integer> activated, List<Integer> fountains) {
        int n = fountains.size();
        List<Integer> coverage = new ArrayList<>(n);
 
        for (int i = 0; i < n; i++) {
            coverage.add(0);
        }
 
        for (int i : activated) {
            int left = Math.max(0, i - fountains.get(i));
            int right = Math.min(n - 1, i + fountains.get(i));
 
            for (int j = left; j <= right; j++) {
                coverage.set(j, 1);
            }
        }
 
        // Check if all positions are covered
        return coverage.stream().allMatch(val -> val == 1);
    }
 
    public static int activateFountains(List<Integer> fountains) {
        int n = fountains.size();
        int minFountains = Integer.MAX_VALUE;
 
        for (int i = 1; i < (1 << n); i++) {
            List<Integer> activated = new ArrayList<>();
 
            // Extract indices of activated fountains based on the binary representation of 'i'
            for (int j = 0; j < n; j++) {
                if ((i >> j & 1) == 1) {
                    activated.add(j);
                }
            }
 
            if (isCovered(activated, fountains)) {
                minFountains = Math.min(minFountains, activated.size());
            }
        }
 
        return minFountains;
    }
 
    public static void main(String[] args) {
        // Example usage
        List<Integer> a1 = List.of(1, 2, 1);
        List<Integer> a2 = List.of(2, 1, 1, 2, 1);
 
        System.out.println(activateFountains(a1)); // Output: 1
        System.out.println(activateFountains(a2)); // Output: 2
    }
}


Python3




def activate_fountains(fountains):
    n = len(fountains)
    min_fountains = float('inf')
    for i in range(1, 2**n):
        activated = []
        for j in range(n):
            if (i >> j) & 1:
                activated.append(j)
        if is_covered(activated, fountains):
            min_fountains = min(min_fountains, len(activated))
    return min_fountains
 
def is_covered(activated, fountains):
    n = len(fountains)
    coverage = [0] * n
    for i in activated:
        left = max(0, i - fountains[i])
        right = min(n - 1, i + fountains[i])
        for j in range(left, right + 1):
            coverage[j] = 1
    return all(coverage)
 
# example usage
a1 = [1, 2, 1]
a2 = [2, 1, 1, 2, 1]
print(activate_fountains(a1)) # output: 1
print(activate_fountains(a2)) # output: 2


C#




using System;
using System.Collections.Generic;
using System.Linq;
 
class FountainActivation {
    static bool IsCovered(List<int> activated,
                          List<int> fountains)
    {
        int n = fountains.Count;
        List<int> coverage
            = Enumerable.Repeat(0, n).ToList();
 
        foreach(int i in activated)
        {
            int left = Math.Max(0, i - fountains[i]);
            int right = Math.Min(n - 1, i + fountains[i]);
 
            for (int j = left; j <= right; j++) {
                coverage[j] = 1;
            }
        }
 
        // Check if all positions are covered
        return coverage.All(val = > val == 1);
    }
 
    static int ActivateFountains(List<int> fountains)
    {
        int n = fountains.Count;
        int minFountains = int.MaxValue;
 
        for (int i = 1; i < (1 << n); i++) {
            List<int> activated = new List<int>();
 
            // Extract indices of activated fountains based
            // on the binary representation of 'i'
            for (int j = 0; j < n; j++) {
                if ((i >> j & 1) == 1) {
                    activated.Add(j);
                }
            }
 
            if (IsCovered(activated, fountains)) {
                minFountains = Math.Min(minFountains,
                                        activated.Count);
            }
        }
 
        return minFountains;
    }
 
    static void Main()
    {
        // Example usage
        List<int> a1 = new List<int>{ 1, 2, 1 };
        List<int> a2 = new List<int>{ 2, 1, 1, 2, 1 };
 
        Console.WriteLine(
            ActivateFountains(a1)); // Output: 1
        Console.WriteLine(
            ActivateFountains(a2)); // Output: 2
    }
}


Javascript




function isCovered(activated, fountains) {
    const n = fountains.length;
    const coverage = Array(n).fill(0);
 
    for (const i of activated) {
        const left = Math.max(0, i - fountains[i]);
        const right = Math.min(n - 1, i + fountains[i]);
 
        for (let j = left; j <= right; j++) {
            coverage[j] = 1;
        }
    }
 
    // Check if all positions are covered
    return coverage.every(val => val === 1);
}
 
function activateFountains(fountains) {
    const n = fountains.length;
    let minFountains = Infinity;
 
    for (let i = 1; i < (1 << n); i++) {
        const activated = [];
 
        // Extract indices of activated fountains based on the binary representation of 'i'
        for (let j = 0; j < n; j++) {
            if ((i >> j & 1) === 1) {
                activated.push(j);
            }
        }
 
        if (isCovered(activated, fountains)) {
            minFountains = Math.min(minFountains, activated.length);
        }
    }
 
    return minFountains;
}
 
// Example usage
const a1 = [1, 2, 1];
const a2 = [2, 1, 1, 2, 1];
 
console.log(activateFountains(a1)); // Output: 1
console.log(activateFountains(a2)); // Output: 2


Output

1
2






The time complexity of this approach is O(2^n) where n is the number of fountains 

 the space complexity is O(n).



Last Updated : 27 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads