Maximize Bitwise AND of first element with complement of remaining elements for any permutation of given Array
Last Updated :
07 Jun, 2021
Given an array arr[] consisting of N integers, the task is to find the maximum value of Bitwise AND of the first element with the complement of remaining elements for any permutation of this array, i.e.
A1 &(~A2) & (~A3) & ......& (~An)
Examples:
Input: arr[] = {1, 2, 4, 8, 16}
Output: 16
Explanation:
For the permutation {16, 1, 2, 4, 8}, the maximum value of the expression can be obtained.
Input: arr[] = {0, 2, 3, 4, 9, 8}
Output: 4
Explanation:
For the permutation {4, 8, 9, 3, 2, 0}, the maximum value of the expression can be obtained
Naive Approach: The simplest approach to solve the problem is to generate all possible permutations of the given array and find the required value for each permutation and print the maximum among them.
Time Complexity: O(N * N!)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized by the following observation:
- The expression A1 &(~A2) & (~A3) & ...... & (~An) is solely dependent on the value of A1.
- Therefore, to get the maximize the value from the expression, choose A1 such that it has the set bit of as highest significance as possible, which is unset in all other array elements.
- Since, the order of remaining array elements doesn't matter, print any permutation having the obtained A1 as the first element.
Illustration:
For arr[] = {1, 2, 4, 8, 16}
Binary representation of the array elements:
(16)10 = (10000)2
(8)10 = (01000)2
(4)10 = (00100)2
(2)10 = (00010)2
(1)10 = (00001)2
As it can be seen that 16 has the highest significant set bit which is unset in all other array elements. Therefore, the required permutation of given permutation will contain 16 as the first element.
Hence, the required Bitwise AND is maximum for permutation having 16 as the first element, which is equal to 16 in this case.
Below is the implementation of the above approach:
C++
// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
#define size_int 32
// Function to maximize the value for
// the given function and the array elements
int functionMax(int arr[], int n)
{
// Vector array to maintain which bit is set
// for which integer in the given array by
// saving index of that integer
vector<int> setBit[32];
for (int i = 0; i < n; i++) {
for (int j = 0; j < size_int; j++) {
// Check if j-th bit is set for
// i-th integer
if (arr[i] & (1 << j))
// Push the index of that
// integer in setBit[j]
setBit[j].push_back(i);
}
}
// Find the element having
// highest significant set bit
// unset in other elements
for (int i = size_int; i >= 0; i--) {
if (setBit[i].size() == 1) {
// Place that integer at 0-th index
swap(arr[0], arr[setBit[i][0]]);
break;
}
}
// Store the maximum AND value
int maxAnd = arr[0];
for (int i = 1; i < n; i++) {
maxAnd = maxAnd & (~arr[i]);
}
// Return the answer
return maxAnd;
}
// Driver Code
int main()
{
int arr[] = { 1, 2, 4, 8, 16 };
int n = sizeof arr / sizeof arr[0];
// Function call
cout << functionMax(arr, n);
return 0;
}
Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
static final int size_int = 32;
// Function to maximize the value for
// the given function and the array elements
static int functionMax(int arr[], int n)
{
// Vector array to maintain which bit is set
// for which integer in the given array by
// saving index of that integer
Vector<Integer> []setBit = new Vector[32 + 1];
for (int i = 0; i < setBit.length; i++)
setBit[i] = new Vector<Integer>();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < size_int; j++)
{
// Check if j-th bit is set for
// i-th integer
if ((arr[i] & (1 << j)) > 0)
// Push the index of that
// integer in setBit[j]
setBit[j].add(i);
}
}
// Find the element having
// highest significant set bit
// unset in other elements
for (int i = size_int; i >= 0; i--)
{
if (setBit[i].size() == 1)
{
// Place that integer at 0-th index
swap(arr, 0, setBit[i].get(0));
break;
}
}
// Store the maximum AND value
int maxAnd = arr[0];
for (int i = 1; i < n; i++)
{
maxAnd = maxAnd & (~arr[i]);
}
// Return the answer
return maxAnd;
}
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, 2, 4, 8, 16 };
int n = arr.length;
// Function call
System.out.print(functionMax(arr, n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python 3 Program to
# implement the above approach
# Function to maximize the
# value for the given function
# and the array elements
def functionMax(arr, n):
# Vector array to maintain
# which bit is set for which
# integer in the given array by
# saving index of that integer
setBit = [[] for i in range(32)]
for i in range(n):
for j in range(32):
# Check if j-th bit is
# set for i-th integer
if (arr[i] & (1 << j)):
# Push the index of that
# integer in setBit[j]
setBit[j].append(i)
# Find the element having
# highest significant set bit
# unset in other elements
i = 31
while(i >= 0):
if (len(setBit[i]) == 1):
# Place that integer
# at 0-th index
temp = arr[0]
arr[0] = arr[setBit[i][0]]
arr[setBit[i][0]] = temp
break
i -= 1
# Store the maximum
# AND value
maxAnd = arr[0]
for i in range(1, n, 1):
maxAnd = (maxAnd & (~arr[i]))
# Return the answer
return maxAnd
# Driver Code
if __name__ == '__main__':
arr = [1, 2, 4, 8, 16]
n = len(arr)
# Function call
print(functionMax(arr, n))
# This code is contributed by bgangwar59
C#
// C# Program to implement
// the above approach
using System;
using System.Collections.Generic;
class GFG{
static readonly int size_int = 32;
// Function to maximize the value for
// the given function and the array elements
static int functionMax(int []arr, int n)
{
// List array to maintain which bit is set
// for which integer in the given array by
// saving index of that integer
List<int> []setBit = new List<int>[32 + 1];
for (int i = 0; i < setBit.Length; i++)
setBit[i] = new List<int>();
for (int i = 0; i < n; i++)
{
for (int j = 0; j < size_int; j++)
{
// Check if j-th bit is set for
// i-th integer
if ((arr[i] & (1 << j)) > 0)
// Push the index of that
// integer in setBit[j]
setBit[j].Add(i);
}
}
// Find the element having
// highest significant set bit
// unset in other elements
for (int i = size_int; i >= 0; i--)
{
if (setBit[i].Count == 1)
{
// Place that integer at 0-th index
swap(arr, 0, setBit[i][0]);
break;
}
}
// Store the maximum AND value
int maxAnd = arr[0];
for (int i = 1; i < n; i++)
{
maxAnd = maxAnd & (~arr[i]);
}
// Return the answer
return maxAnd;
}
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, 2, 4, 8, 16 };
int n = arr.Length;
// Function call
Console.Write(functionMax(arr, n));
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// Javascript Program to implement
// the above approach
var size_int = 32;
// Function to maximize the value for
// the given function and the array elements
function functionMax( arr, n)
{
// Vector array to maintain which bit is set
// for which integer in the given array by
// saving index of that integer
var setBit = Array.from(Array(32), ()=>new Array());
for (var i = 0; i < n; i++) {
for (var j = 0; j < size_int; j++) {
// Check if j-th bit is set for
// i-th integer
if (arr[i] & (1 << j))
// Push the index of that
// integer in setBit[j]
setBit[j].push(i);
}
}
// Find the element having
// highest significant set bit
// unset in other elements
for (var i = size_int-1; i >= 0; i--) {
if (setBit[i].length == 1) {
// Place that integer at 0-th index
[arr[0], arr[setBit[i][0]]] = [arr[setBit[i][0]], arr[0]];
break;
}
}
// Store the maximum AND value
var maxAnd = arr[0];
for (var i = 1; i < n; i++) {
maxAnd = maxAnd & (~arr[i]);
}
// Return the answer
return maxAnd;
}
// Driver Code
var arr = [1, 2, 4, 8, 16];
var n = arr.length;
// Function call
document.write( functionMax(arr, n));
// This code is contributed by rrrtnx.
</script>
Time Complexity: O(N * sizeof(int)), where sizeof(int) is 32
Auxiliary Space: O(N)
Similar Reads
Maximize sum of Bitwise AND of same-indexed elements of a permutation of first N natural numbers and a given array Given an array arr[] consisting of N positive integers, the task is to find the maximum sum of Bitwise AND of same-indexed elements of permutation of the first N natural numbers and the array arr[]. Examples: Input: arr[] = {4, 2, 3, 6}Output: 5Explanation: Consider the permutation {1, 0, 3, 2}. Sum
9 min read
Maximize minimum distance between repetitions from any permutation of the given Array Given an array arr[], consisting of N positive integers in the range [1, N], the task is to find the largest minimum distance between any consecutive repetition of an element from any permutation of the given array. Examples: Input: arr[] = {1, 2, 1, 3} Output: 3 Explanation: The maximum possible di
5 min read
Maximize frequency of an element by at most one increment or decrement of all array elements | Set 2 Given an array arr[] of size N, the task is to find the maximum frequency of any array element by incrementing or decrementing each array element by 1 at most once. Examples: Input: arr[] = { 3, 1, 4, 1, 5, 9, 2 } Output: 4 Explanation: Decrementing the value of arr[0] by 1 modifies arr[] to { 2, 1,
6 min read
Maximize modulus by replacing adjacent pairs with their modulus for any permutation of given Array Given an array A[] consisting of distinct elements, the task is to obtain the largest possible modulus value that remains after repeatedly replacing adjacent elements by their modulus, starting from the first element, for any possible permutations of the given array. (...(( A[1] mod A[2]) mod A[3])
11 min read
Generate a permutation of first N natural numbers from an array of differences between adjacent elements Given an array arr[] consisting of (N - 1), the task is to construct a permutation array P[] consisting of the first N Natural Numbers such that arr[i] = (P[i +1] - P[i]). If there exists no such permutation, then print "-1". Examples: Input: arr[] = {-1, 2, -3, -1}Output: 4 3 5 2 1Explanation:For t
8 min read
Count of subarrays which forms a permutation from given Array elements Given an array A[] consisting of integers [1, N], the task is to count the total number of subarrays of all possible lengths x (1 ? x ? N), consisting of a permutation of integers [1, x] from the given array. Examples: Input: A[] = {3, 1, 2, 5, 4} Output: 4 Explanation: Subarrays forming a permutati
6 min read