Modify a binary array to Bitwise AND of all elements as 1
Last Updated :
15 Nov, 2022
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 operation.
If it is possible, then the output is "YES", otherwise the output is "NO".
Examples:
Input: arr[] = {0, 1, 0, 0, 1}
Output: Yes
Choose these pair of indices (0, 1), (1, 2), (3, 4).
Input: arr[] = {0, 0, 0}
Output: No
Approach:
The main observation is, if the array consists of at least one 1, then the answer will be YES, otherwise the output will be NO because OR with 1 will give us 1, as the array consists of only 0 and 1.
If there is at least one 1, then we will choose all indices with a 0 value and replace them with an OR value with the index having 1 and the OR value will always be 1.
After all operations, the array will consist of only 1 and the AND value between any pair of indices will be 1 as (1 AND 1)=1.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check if it is possible or not
bool check(int a[], int n)
{
for (int i = 0; i < n; i++)
if (a[i])
return true;
return false;
}
// Driver code
int main()
{
int a[] = { 0, 1, 0, 1 };
int n = sizeof(a) / sizeof(a[0]);
check(a, n) ? cout << "YES\n"
: cout << "NO\n";
return 0;
}
Java
// Java implementation of the above approach
class GFG
{
// Function to check if it is possible or not
static boolean check(int a[], int n)
{
for (int i = 0; i < n; i++)
if (a[i] == 1)
return true;
return false;
}
// Driver code
public static void main (String[] args)
{
int a[] = { 0, 1, 0, 1 };
int n = a.length;
if(check(a, n) == true )
System.out.println("YES\n") ;
else
System.out.println("NO\n");
}
}
// This code is contributed by Ryuga
Python3
# Python 3 implementation of the
# above approach
# Function to check if it is
# possible or not
def check(a, n):
for i in range(n):
if (a[i]):
return True
return False
# Driver code
if __name__ == '__main__':
a = [0, 1, 0, 1]
n = len(a)
if(check(a, n)):
print("YES")
else:
print("NO")
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function to check if it is possible or not
static bool check(int []a, int n)
{
for (int i = 0; i < n; i++)
if (a[i] == 1)
return true;
return false;
}
// Driver code
public static void Main ()
{
int []a = { 0, 1, 0, 1 };
int n = a.Length;
if(check(a, n) == true )
Console.Write("YES\n") ;
else
Console.Write("NO\n");
}
}
// This code is contributed
// by Akanksha Rai
PHP
<?php
// PHP implementation of the
// above approach
// Function to check if it is
// possible or not
function check($a, $n)
{
for ($i = 0; $i < $n; $i++)
if ($a[$i])
return true;
return false;
}
// Driver code
$a = array(0, 1, 0, 1);
$n = sizeof($a);
if(check($a, $n))
echo "YES\n";
else
echo "NO\n";
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// Javascript implementation of the above approach
// Function to check if it is possible or not
function check(a, n)
{
for (var i = 0; i < n; i++)
if (a[i])
return true;
return false;
}
// Driver code
var a = [0, 1, 0, 1 ];
var n = a.length;
check(a, n) ? document.write( "YES")
: document.write( "NO\n");
</script>
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Check if all elements of binary array can be made 1 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,
7 min read
Check if an Array exists with Bitwise OR as A and Bitwise AND as B Three integers N, A and B are given to you. you need to create an array arr of length 'N' such that it satisfies the following conditions : Each elements of arr should be unique.arr[1] | arr[2] | arr[3] |....arr[N] = A arr[1] & arr[2] & arr[3] &....arr[N] = B Where '|' Represents Bitwise
8 min read
Bitwise XOR of a Binary array Given a binary array arr[], the task is to calculate the bitwise XOR of all the elements in this array and print it. Examples: Input: arr[] = {â100â, â1001â, â0011â} Output: 1110 0100 XOR 1001 XOR 0011 = 1110 Input: arr[] = {â10â, â11â, â1000001â} Output: 1000000 Approach: Step 1: First find the max
7 min read
Maximize bitwise AND of Array by changing at most K bits of elements Given an array arr[] of length N. You can perform at most K operations on the array of the following type: Choose an index i (0 ? i ? N-1) and set the j-th bit of arr[i] to 1 (0 ? j ? 30). The task is to find the maximum possible value of bitwise AND of all array elements after performing at most K
8 min read
Modify an array by sorting after reversal of bits of each array element Given an array arr[] consisting of N integers, the task is to modify the array by replacing each array element with the number obtained by reversing their respective binary representations and sort the modified array. Examples: Input: arr[ ] = {43, 422, 132}Output: 33 53 203Explanation: The binary r
10 min read