Given an array a[] consisting 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 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 only 0 and 1.
If there is at least one 1 then we will choose all index with 0 value and replace with OR value with the index having 1 and the OR value will be 1 always.
After all operation, 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 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 ?> |
YES
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Modify array by merging elements with addition such that it consists of only Primes.
- Minimum operations required to modify the array such that parity of adjacent elements is different
- Rearrange array elements such that Bitwise AND of first N - 1 elements is equal to last element
- Bitwise XOR of a Binary array
- Bitwise AND of all the elements of array
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Bitwise AND operations to make any two array elements equal
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Check if all elements of binary array can be made 1
- Distinct adjacent elements in a binary array
- Maximum number of consecutive 1's in binary representation of all the array elements
- Minimum Group Flips to Make Binary Array Elements Same
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
- Bitwise AND of N binary strings
- Bitwise XOR of elements having odd frequency
- Count of paths in given Binary Tree with odd bitwise AND for Q queries
- Maximum value of Bitwise AND from root to leaf in a Binary tree
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

