Minimum 1s to lend power to make whole array powerful
Last Updated :
24 Mar, 2023
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[1] and arr[5] need to lend
Input : arr[] = {1, 0, 1, 0, 0, 1, 0, 0, 0, 1}
K = 3
Output : 3
a[2], a[5] and a[9] lend to their right side zeros.
Input : arr[] = {1, 1, 1}
K = 2
Output : 1
arr[1] need to lend power to all cells.
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.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int minLendings( int arr[], int n, int k)
{
int leftOne[n];
int oneInd = -1;
for ( int i = 0; i < n; i++) {
if (arr[i] == 1)
oneInd = i;
leftOne[i] = oneInd;
}
int ans = 0;
for ( int i = 0; i < n;) {
int pos = leftOne[min(i + k - 1, n - 1)];
if (pos == -1 || pos + k <= i)
return -1;
i = pos + k;
ans++;
}
return ans;
}
int main()
{
int arr[] = { 0, 1, 1, 1, 1, 0 };
int n = sizeof (arr) / sizeof (arr[0]);
int k = 2;
cout << minLendings(arr, n, k);
return 0;
}
|
Java
import java.math.*;
class GFG {
static int minLendings( int arr[], int n, int k)
{
int leftOne[] = new int [n];
int oneInd = - 1 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] == 1 )
oneInd = i;
leftOne[i] = oneInd;
}
int ans = 0 ;
for ( int i = 0 ; i < n;) {
int pos = leftOne[(Math.min(i + k - 1 , n - 1 ))];
if (pos == - 1 || pos + k <= i)
return - 1 ;
i = pos + k;
ans++;
}
return ans;
}
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));
}
}
|
Python3
def minLendings(arr, n, k):
leftOne = [ 0 for i in range (n + 1 )]
oneInd = - 1
for i in range (n):
if (arr[i] = = 1 ):
oneInd = i
leftOne[i] = oneInd
ans = 0 ; i = 0
while (i < n):
pos = leftOne[ min (i + k - 1 , n - 1 )]
if (pos = = - 1 or pos + k < = i):
return - 1
i = pos + k
ans + = 1
return ans
arr = [ 0 , 1 , 1 , 1 , 1 , 0 ]
n = len (arr)
k = 2
print (minLendings(arr, n, k))
|
C#
using System;
class GFG {
static int minLendings( int []arr, int n, int k)
{
int []leftOne = new int [n];
int oneInd = -1;
for ( int i = 0; i < n; i++)
{
if (arr[i] == 1)
oneInd = i;
leftOne[i] = oneInd;
}
int ans = 0;
for ( int i = 0; i < n;)
{
int pos = leftOne[(Math.Min(i + k - 1,
n - 1))];
if (pos == -1 || pos + k <= i)
return -1;
i = pos + k;
ans++;
}
return ans;
}
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));
}
}
|
PHP
<?php
function minLendings( $arr , $n , $k )
{
$leftOne [ $n ] = array (0);
$oneInd = -1;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $arr [ $i ] == 1)
$oneInd = $i ;
$leftOne [ $i ] = $oneInd ;
}
$ans = 0;
for ( $i = 0; $i < $n 😉
{
$pos = $leftOne [min( $i + $k - 1, $n - 1)];
if ( $pos == -1 || $pos + $k <= $i )
return -1;
$i = $pos + $k ;
$ans ++;
}
return $ans ;
}
$arr = array (0, 1, 1, 1, 1, 0 );
$n = sizeof( $arr );
$k = 2;
echo minLendings( $arr , $n , $k );
?>
|
Javascript
<script>
function minLendings(arr, n, k)
{
let leftOne = new Array(n);
let oneInd = -1;
for (let i = 0; i < n; i++)
{
if (arr[i] == 1)
oneInd = i;
leftOne[i] = oneInd;
}
let ans = 0;
for (let i = 0; i < n;)
{
let pos = leftOne[(Math.min(i + k - 1, n - 1))];
if (pos == -1 || pos + k <= i)
return -1;
i = pos + k;
ans++;
}
return ans;
}
let arr = [ 0, 1, 1, 1, 1, 0 ];
let n = arr.length;
let k = 2;
document.write(minLendings(arr, n, k));
</script>
|
The time complexity for this algorithm is O(n) .
Share your thoughts in the comments
Please Login to comment...