Check if all elements of binary array can be made 1
Last Updated :
25 Sep, 2022
Given a binary array Arr and an integer K. If the value at index i is 1 you can change 0 to 1 with in the range of ( i – K ) to ( i + K ).
The task is to determine whether all the elements of the array can be made 1 or not.
Examples:
Input: Arr = { 0, 1, 0, 1 }, K = 2
Output: 2
Input: Arr = { 1, 0, 0, 0, 0, 0, 1 }, K = 2
Output: 0
It is not possible to make all the elements equal to 1
Approach:
Here another array is being used to mark as 1 if we can reach that index.
For every index in the range of 1 to N if the value of Arr[i] is 1 then make a loop from (i – K) to (i + K) and update b[i] to 1.
At last check the entry of b[i], and it should be 1 for every i, if it is then print 1 else print 0.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void checkAllOnes( int arr[], int n,
int k)
{
int brr[n];
for ( int i = 0; i < n; i++) {
if (arr[i] == 1) {
int h = k + 1;
int j = i;
while (j >= 0 && (h--)) {
brr[j] = 1;
j--;
}
h = k + 1;
j = i;
while (j < n && (h--)) {
brr[j] = 1;
j++;
}
}
}
int flag = 0;
for ( int i = 0; i < n; i++) {
if (brr[i] == 0) {
flag = 1;
break ;
}
}
if (flag == 1)
cout << "0" ;
else
cout << "1\n" ;
}
int main()
{
int arr[] = { 1, 0, 1, 0 };
int k = 2;
int n = sizeof (arr) / sizeof (arr[0]);
checkAllOnes(arr, n, k);
return 0;
}
|
Java
class GFG
{
static void checkAllOnes( int arr[],
int n, int k)
{
int brr[] = new int [n];
for ( int i = 0 ; i < n; i++)
{
if (arr[i] == 1 )
{
int h = k + 1 ;
int j = i;
while ((j >= 0 ) && (h-- != 0 ))
{
brr[j] = 1 ;
j--;
}
h = k + 1 ;
j = i;
while ((j < n) && (h-- != 0 ))
{
brr[j] = 1 ;
j++;
}
}
}
int flag = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (brr[i] == 0 )
{
flag = 1 ;
break ;
}
}
if (flag == 1 )
System.out.println( "0" );
else
System.out.println( "1" );
}
public static void main (String[] args)
{
int arr[] = { 1 , 0 , 1 , 0 };
int k = 2 ;
int n = arr.length;
checkAllOnes(arr, n, k);
}
}
|
Python3
def checkAllOnes(arr, n, k):
brr = [ 0 for i in range (n)]
for i in range (n):
if (arr[i] = = 1 ):
h = k + 1
j = i
while (j > = 0 and (h)):
brr[j] = 1
h - = 1
j - = 1
h = k + 1
j = i
while (j < n and (h)):
brr[j] = 1
j + = 1
h - = 1
flag = 0
for i in range (n):
if (brr[i] = = 0 ):
flag = 1
break
if (flag = = 1 ):
print ( "0" )
else :
print ( "1" )
arr = [ 1 , 0 , 1 , 0 ]
k = 2
n = len (arr)
checkAllOnes(arr, n, k)
|
C#
using System;
class GFG
{
static void checkAllOnes( int []arr,
int n, int k)
{
int []brr = new int [n];
for ( int i = 0; i < n; i++)
{
if (arr[i] == 1)
{
int h = k + 1;
int j = i;
while ((j >= 0) && (h-- != 0))
{
brr[j] = 1;
j--;
}
h = k + 1;
j = i;
while ((j < n) && (h-- != 0))
{
brr[j] = 1;
j++;
}
}
}
int flag = 0;
for ( int i = 0; i < n; i++)
{
if (brr[i] == 0)
{
flag = 1;
break ;
}
}
if (flag == 1)
Console.WriteLine( "0" );
else
Console.WriteLine( "1" );
}
public static void Main (String[] args)
{
int []arr = { 1, 0, 1, 0 };
int k = 2;
int n = arr.Length;
checkAllOnes(arr, n, k);
}
}
|
Javascript
<script>
function checkAllOnes(arr, n, k)
{
let brr = new Array(n);
for (let i = 0; i < n; i++) {
if (arr[i] == 1) {
let h = k + 1;
let j = i;
while (j >= 0 && (h--)) {
brr[j] = 1;
j--;
}
h = k + 1;
j = i;
while (j < n && (h--)) {
brr[j] = 1;
j++;
}
}
}
let flag = 0;
for (let i = 0; i < n; i++) {
if (brr[i] == 0) {
flag = 1;
break ;
}
}
if (flag == 1)
document.write( "0" );
else
document.write( "1" );
}
let arr = [ 1, 0, 1, 0 ];
let k = 2;
let n = arr.length;
checkAllOnes(arr, n, k);
</script>
|
Output:
1
Time complexity: O(n) where n is the number of elements in the given array.
Auxiliary space: O(n), as using extra space for array brr.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...