Open In App

Check if all elements of binary array can be made 1

Last Updated : 25 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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++




// C++ implementation
#include <bits/stdc++.h>
using namespace std;
 
// Function to print 1 if the
// it is possible to make all array
// element equal to 1 else 0
void checkAllOnes(int arr[], int n,
                 int k)
{
 
    int brr[n];
 
    // Iterating over the array
    for (int i = 0; i < n; i++) {
 
        // If element is 1
        if (arr[i] == 1) {
 
            int h = k + 1;
            int j = i;
 
            // Put b[j...j-k] = 0
            while (j >= 0 && (h--)) {
                brr[j] = 1;
                j--;
            }
 
            h = k + 1;
            j = i;
 
            // Put b[j...j+k] = 0
            while (j < n && (h--)) {
                brr[j] = 1;
                j++;
            }
        }
    }
 
    int flag = 0;
 
    // If any value in aux
    // array equal to 0
    // then set flag
    for (int i = 0; i < n; i++) {
        if (brr[i] == 0) {
            flag = 1;
            break;
        }
    }
 
    // If flag is set this
    // means array after
    // conversion contains
    // 0 so print 0
    if (flag == 1)
        cout << "0";
 
    // else print 1
    else
        cout << "1\n";
}
 
// Driver Code
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




// Java implementation of above approach
class GFG
{
         
// Function to print 1 if the
// it is possible to make all array
// element equal to 1 else 0
static void checkAllOnes(int arr[],
                         int n, int k)
{
    int brr[] = new int[n];
 
    // Iterating over the array
    for (int i = 0; i < n; i++)
    {
 
        // If element is 1
        if (arr[i] == 1)
        {
            int h = k + 1;
            int j = i;
 
            // Put b[j...j-k] = 0
            while ((j >= 0) && (h-- != 0))
            {
                brr[j] = 1;
                j--;
            }
 
            h = k + 1;
            j = i;
 
            // Put b[j...j+k] = 0
            while ((j < n) && (h-- != 0))
            {
                brr[j] = 1;
                j++;
            }
        }
    }
 
    int flag = 0;
 
    // If any value in aux
    // array equal to 0
    // then set flag
    for (int i = 0; i < n; i++)
    {
        if (brr[i] == 0)
        {
            flag = 1;
            break;
        }
    }
 
    // If flag is set this
    // means array after
    // conversion contains
    // 0 so print 0
    if (flag == 1)
        System.out.println("0");
 
    // else print 1
    else
        System.out.println("1");
}
 
// Driver Code
public static void main (String[] args)
{
    int arr[] = { 1, 0, 1, 0 };
    int k = 2;
    int n = arr.length;
 
    checkAllOnes(arr, n, k);
}
}
 
// This code is contributed by AnkitRai01


Python3




# Python3 implementation
 
# Function to print 1 if the
# it is possible to make all array
# element equal to 1 else 0
def checkAllOnes(arr, n, k):
 
    brr = [0 for i in range(n)]
 
    # Iterating over the array
    for i in range(n):
 
        # If element is 1
        if (arr[i] == 1):
 
            h = k + 1
            j = i
 
            # Put b[j...j-k] = 0
            while (j >= 0 and (h)):
                brr[j] = 1
                h -= 1
                j -= 1
 
            h = k + 1
            j = i
 
            # Put b[j...j+k] = 0
            while (j < n and (h)):
                brr[j] = 1
                j += 1
                h -= 1
 
    flag = 0
 
    # If any value in aux
    # array equal to 0
    # then set flag
    for i in range(n):
        if (brr[i] == 0):
            flag = 1
            break
 
    # If flag is set this
    # means array after
    # conversion contains
    # 0 so pr0
    if (flag == 1):
        print("0")
 
    # else pr1
    else:
        print("1")
 
# Driver Code
arr = [1, 0, 1, 0]
k = 2
n = len(arr)
 
checkAllOnes(arr, n, k)
 
# This code is contributed by Mohit Kumar


C#




// C# implementation of above approach
using System;
                     
class GFG
{
         
// Function to print 1 if the
// it is possible to make all array
// element equal to 1 else 0
static void checkAllOnes(int []arr,
                         int n, int k)
{
    int []brr = new int[n];
 
    // Iterating over the array
    for (int i = 0; i < n; i++)
    {
 
        // If element is 1
        if (arr[i] == 1)
        {
            int h = k + 1;
            int j = i;
 
            // Put b[j...j-k] = 0
            while ((j >= 0) && (h-- != 0))
            {
                brr[j] = 1;
                j--;
            }
 
            h = k + 1;
            j = i;
 
            // Put b[j...j+k] = 0
            while ((j < n) && (h-- != 0))
            {
                brr[j] = 1;
                j++;
            }
        }
    }
 
    int flag = 0;
 
    // If any value in aux
    // array equal to 0
    // then set flag
    for (int i = 0; i < n; i++)
    {
        if (brr[i] == 0)
        {
            flag = 1;
            break;
        }
    }
 
    // If flag is set this
    // means array after
    // conversion contains
    // 0 so print 0
    if (flag == 1)
        Console.WriteLine("0");
 
    // else print 1
    else
        Console.WriteLine("1");
}
 
// Driver Code
public static void Main (String[] args)
{
    int []arr = { 1, 0, 1, 0 };
    int k = 2;
    int n = arr.Length;
 
    checkAllOnes(arr, n, k);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
 
// Javascript implementation
 
// Function to print 1 if the
// it is possible to make all array
// element equal to 1 else 0
function checkAllOnes(arr, n, k)
{
 
    let brr = new Array(n);
 
    // Iterating over the array
    for (let i = 0; i < n; i++) {
 
        // If element is 1
        if (arr[i] == 1) {
 
            let h = k + 1;
            let j = i;
 
            // Put b[j...j-k] = 0
            while (j >= 0 && (h--)) {
                brr[j] = 1;
                j--;
            }
 
            h = k + 1;
            j = i;
 
            // Put b[j...j+k] = 0
            while (j < n && (h--)) {
                brr[j] = 1;
                j++;
            }
        }
    }
 
    let flag = 0;
 
    // If any value in aux
    // array equal to 0
    // then set flag
    for (let i = 0; i < n; i++) {
        if (brr[i] == 0) {
            flag = 1;
            break;
        }
    }
 
    // If flag is set this
    // means array after
    // conversion contains
    // 0 so print 0
    if (flag == 1)
        document.write("0");
 
    // else print 1
    else
        document.write("1");
}
 
// Driver Code
 
    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
Previous
Next
Share your thoughts in the comments

Similar Reads