Minimum 1s to lend power to make whole array powerful

Given a binary array and an integer k where every 1 has power and it can lend power to neighbors and itself. A cell with value 1 can lend power within distance <k. Task is to find the minimum number of 1s that need to lend power so that all cells become powerful.

If it is not possible to make whole array powerful, return -1.
Examples:

Input  : arr[] = {0, 1, 1, 1, 1, 0}
K = 2
Output : 2
arr and arr need to lend

Input  :  arr[] = {1, 0, 1, 0, 0, 1, 0, 0, 0, 1}
K = 3
Output : 3
a, a and a lend to their right side zeros.

Input  :  arr[] = {1, 1, 1}
K = 2
Output : 1
arr need to lend power to all cells.

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple solution is to try all possible combinations of 1s, starting with one 1, then two 1s and so on until we find a combination that makes the whole array powerful.

An efficient solution is based on the observation that there should be a 1 within distance k for every cell. We preprocess the array and create an auxiliary array that stores indexes of closest 1 on left side. We traverse through the whole array and find index of the closest left 1 within k distance of current cell. If there is no left 1, we return -1. Else, we increment ans and move to the next cell after k distance.

C++

 // C++ program to minimum of number of lendings // by 1s. #include using namespace std;    int minLendings(int arr[], int n, int k) {     // Create an auxiliary array and fill      // indexes of closest 1s on left side.      int leftOne[n];     int oneInd = -1;     for (int i = 0; i < n; i++) {         if (arr[i] == 1)             oneInd = i;         leftOne[i] = oneInd;     }        // Traverse the array from left side. If     // current element has a 1 on     int ans = 0;     for (int i = 0; i < n;) {             // Find index of closest 1 after shift of         // k-1 or end whichever is closer.         int pos = leftOne[min(i + k - 1, n - 1)];            // If there is no closest 1 within allowed         // range.          if (pos == -1 || pos + k <= i)             return -1;            // If a closest 1 is found, check after k         // jumps and increment answer.         i = pos + k;         ans++;     }     return ans; }    // Driver code int main() {     int arr[] = { 0, 1, 1, 1, 1, 0 };     int n = sizeof(arr) / sizeof(arr);     int k = 2;     cout << minLendings(arr, n, k);     return 0; }

Java

 // Java program to minimum of number of  // lendings by 1s. import java.math.*;    class GFG {    static int minLendings(int arr[], int n, int k) {     // Create an auxiliary array and fill      // indexes of closest 1s on left side.      int leftOne[] = new int[n];     int oneInd = -1;     for (int i = 0; i < n; i++) {         if (arr[i] == 1)             oneInd = i;         leftOne[i] = oneInd;     }        // Traverse the array from left side. If     // current element has a 1 on     int ans = 0;     for (int i = 0; i < n;) {            // Find index of closest 1 after shift of         // k-1 or end whichever is closer.         int pos = leftOne[Math.min(i + k - 1, n - 1)];            // If there is no closest 1 within          // allowed range.          if (pos == -1 || pos + k <= i)             return -1;            // If a closest 1 is found, check after k         // jumps and increment answer.         i = pos + k;         ans++;     }     return ans; }    // Driver code public static void main(String[] args) {     int arr[] = { 0, 1, 1, 1, 1, 0 };     int n = arr.length;     int k = 2;     System.out.println(minLendings(arr, n, k)); } }    // This code is contributed by Prerna Saini

Python3

 # Python3 program to minimum # of number of lendings by 1s.    def minLendings(arr, n, k):        # Create an auxiliary array and fill      # indexes of closest 1s on left side.      leftOne = [0 for i in range(n + 1)]     oneInd = -1            for i in range(n):         if (arr[i] == 1):             oneInd = i         leftOne[i] = oneInd        # Traverse the array from left side.     # If current element has a 1 on     ans = 0; i = 0            while(i < n):            # Find index of closest 1 after shift of         # k-1 or end whichever is closer.         pos = leftOne[min(i + k - 1, n - 1)]            # If there is no closest 1           # within allowed range.          if (pos == -1 or pos + k <= i):             return -1            # If a closest 1 is found, check after k         # jumps and increment answer.         i = pos + k         ans += 1            return ans    # Driver program arr = [ 0, 1, 1, 1, 1, 0 ] n = len(arr) k = 2 print(minLendings(arr, n, k))    # This code is contributed by Anant Agarwal.

C#

 // C# program to minimum of number of  // lendings by 1s. using System;    class GFG {        static int minLendings(int []arr, int n, int k)     {                    // Create an auxiliary array and fill          // indexes of closest 1s on left side.          int []leftOne = new int[n];         int oneInd = -1;         for (int i = 0; i < n; i++)         {             if (arr[i] == 1)                 oneInd = i;             leftOne[i] = oneInd;         }                // Traverse the array from left side.         // If current element has a 1 on         int ans = 0;         for (int i = 0; i < n;)         {                    // Find index of closest 1 after             // shift of k-1 or end whichever             // is closer.             int pos = leftOne[Math.Min(i + k - 1,                                           n - 1)];                    // If there is no closest 1 within              // allowed range.              if (pos == -1 || pos + k <= i)                 return -1;                    // If a closest 1 is found, check              // after k jumps and increment answer.             i = pos + k;             ans++;         }                    return ans;     }            // Driver code     public static void Main()     {         int []arr = { 0, 1, 1, 1, 1, 0 };         int n = arr.Length;         int k = 2;                    Console.WriteLine(minLendings(arr, n, k));     } }    // This code is contributed by vt_m.

PHP



Output :

2

The time complexity for this algorithm is O(n) .

This article is contributed by Bibhu Prasad Pala. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up

Improved By : vt_m, jit_t

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.