Rearrange array elements such that Bitwise AND of first N – 1 elements is equal to last element
Given an array arr[] of N positive integers, the task is to find an arrangement such that Bitwise AND of the first N – 1 elements is equal to the last element. If no such arrangement is possible then output will be -1.
Examples:
Input: arr[] = {1, 5, 3, 3}
Output: 3 5 3 1
(3 & 5 & 3) = 1 which is equal to the last element.
Input: arr[] = {2, 3, 7}
Output: -1
No such arrangement is possible.
Approach:
- Let p = x & y then p ≤ min(x, y) which means Bitwise AND is a non-increasing function. If bitwise AND is performed on some elements then the value will be decreasing or remain the same.
- So, it is obvious to put the smallest element at the last index and then check if the last element is equal to the bitwise AND of the first N – 1 elements or not. If yes, then print the required arrangement otherwise print -1.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Utility function to print // the elements of an array void printArr(int arr[], int n) { for (int i = 0; i < n; i++) cout << arr[i] << " "; } // Function to find the required arrangement void findArrangement(int arr[], int n) { // There has to be atleast 2 elements if (n < 2) { cout << "-1"; return; } // Minimum element from the array int minVal = *min_element(arr, arr + n); // Swap any occurrence of the minimum // element with the last element for (int i = 0; i < n; i++) { if (arr[i] == minVal) { swap(arr[i], arr[n - 1]); break; } } // Find the bitwise AND of the // first (n - 1) elements int andVal = arr[0]; for (int i = 1; i < n - 1; i++) { andVal &= arr[i]; } // If the bitwise AND is equal // to the last element then // print the arrangement if (andVal == arr[n - 1]) printArr(arr, n); else cout << "-1"; } // Driver code int main() { int arr[] = { 1, 5, 3, 3 }; int n = sizeof(arr) / sizeof(int); findArrangement(arr, n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Utility function to print // the elements of an array static void printArr(int []arr, int n) { for (int i = 0; i < n; i++) System.out.print(arr[i] + " "); } // Function to find the required arrangement static void findArrangement(int arr[], int n) { // There has to be atleast 2 elements if (n < 2) { System.out.print("-1"); return; } // Minimum element from the array int minVal = Arrays.stream(arr).min().getAsInt(); // Swap any occurrence of the minimum // element with the last element for (int i = 0; i < n; i++) { if (arr[i] == minVal) { swap(arr, i, n - 1); break; } } // Find the bitwise AND of the // first (n - 1) elements int andVal = arr[0]; for (int i = 1; i < n - 1; i++) { andVal &= arr[i]; } // If the bitwise AND is equal // to the last element then // print the arrangement if (andVal == arr[n - 1]) printArr(arr, n); else System.out.print("-1"); } static int[] swap(int []arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } // Driver code public static void main(String []args) { int arr[] = { 1, 5, 3, 3 }; int n = arr.length; findArrangement(arr, n); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach # Utility function to print # the elements of an array def printArr(arr, n) : for i in range(n) : print(arr[i], end = " "); # Function to find the required arrangement def findArrangement(arr, n) : # There has to be atleast 2 elements if (n < 2) : print("-1", end = ""); return; # Minimum element from the array minVal = min(arr); # Swap any occurrence of the minimum # element with the last element for i in range(n) : if (arr[i] == minVal) : arr[i], arr[n - 1] = arr[n - 1], arr[i]; break; # Find the bitwise AND of the # first (n - 1) elements andVal = arr[0]; for i in range(1, n - 1) : andVal &= arr[i]; # If the bitwise AND is equal # to the last element then # print the arrangement if (andVal == arr[n - 1]) : printArr(arr, n); else : print("-1"); # Driver code if __name__ == "__main__" : arr = [ 1, 5, 3, 3 ]; n = len(arr); findArrangement(arr, n); # This code is contributed by AnkitRai01 |
C#
// C# implementation of the approach using System; using System.Linq; class GFG { // Utility function to print // the elements of an array static void printArr(int []arr, int n) { for (int i = 0; i < n; i++) Console.Write(arr[i] + " "); } // Function to find the required arrangement static void findArrangement(int []arr, int n) { // There has to be atleast 2 elements if (n < 2) { Console.Write("-1"); return; } // Minimum element from the array int minVal = arr.Min(); // Swap any occurrence of the minimum // element with the last element for (int i = 0; i < n; i++) { if (arr[i] == minVal) { swap(arr, i, n - 1); break; } } // Find the bitwise AND of the // first (n - 1) elements int andVal = arr[0]; for (int i = 1; i < n - 1; i++) { andVal &= arr[i]; } // If the bitwise AND is equal // to the last element then // print the arrangement if (andVal == arr[n - 1]) printArr(arr, n); else Console.Write("-1"); } static int[] swap(int []arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; return arr; } // Driver code public static void Main(String []args) { int []arr = { 1, 5, 3, 3 }; int n = arr.Length; findArrangement(arr, n); } } // This code is contributed by PrinciRaj1992 |
3 5 3 1
Time Complexity: O(N)
Recommended Posts:
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Bitwise AND operations to make any two array elements equal
- Modify a binary array to Bitwise AND of all elements as 1
- XOR of all elements of array with set bits equal to K
- Sorting array elements with set bits equal to K
- Smallest power of 2 which is greater than or equal to sum of array elements
- Make array elements equal in Minimum Steps
- Count pairs in an array such that both elements has equal set bits
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum Increment / decrement to make array elements equal
- Construct an array from XOR of all elements of array except element at same index
- Unique element in an array where all elements occur k times except one
- Count of elements which are equal to the XOR of the next two elements
- Replace every array element by Bitwise Xor of previous and next element
- Generate an array of K elements such that sum of elements is N and the condition a[i] < a[i+1] <= 2*a[i] is met | Set 2
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.


