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++
// 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.
Similar Reads
Check if elements of a Binary Matrix can be made alternating Given a 2D array grid[][] of size N * M, consisting of the characters "1", "0", and "*", where "*" denotes an empty space and can be replaced by either a "1" or a "0". The task is to fill the grid such that "0" and "1" occur alternatively and no two consecutive characters occur together, i.e. (10101
15+ min read
Check if given Array can be made a binary Array with K consecutive 1s Given an array A[] of size N and an integer K. Each element of the array is either 0, 1 or 2. The task is to check if the array can be converted into a binary array (elements will be either 0 or 1) by replacing every element equal to 2 with either 0 or 1, such that there are only K occurrences of 1,
11 min read
Modify a binary array to Bitwise AND of all elements as 1 Given an array, a[] consists of only 0 and 1. The task is to check if it is possible to transform the array such that the AND value between every pair of indices is 1. The only operation allowed is to: Take two indices i and j and replace the a[i] and a[j] with a[i] | a[j] where '|' means bitwise OR
4 min read
Check if given Array can be made a permutation of 1 to N by reducing elements by half Given an array nums[] of size N, the task is to check whether the given array can be converted into a permutation of 1 to N after performing given operations any number of times (may be 0). An operation is defined as: Pick any element of the array say 'x', and replace it with 'x/2'. Note: In a permu
6 min read
Check if Array Elements can be Made Equal with Given Operations Given an array arr[] consisting of N integers and following are the three operations that can be performed using any external number X. Add X to an array element once.Subtract X from an array element once.Perform no operation on the array element.Note : Only one operation can be performed on a numbe
6 min read
Check if Binary Array can be made palindrome after K bitwise XOR with 1 Given a binary array arr[] of size N and an integer K, the task is to check whether the binary array can be turned into a palindrome after K number of operations where in one operation, any random index can be chosen and the value stored in the index will be replaced by its bitwise XOR(^) with1. Exa
9 min read