Maximize Bitwise AND of Array by replacing at most one element
Last Updated :
18 Jan, 2022
Given an array arr[] containing N positive integers, the task is to maximize bitwise AND of the arr[] by picking at most one element of the arr[] and increment or decrement it by any value.
Examples:
Input: arr[] = {1, 2, 3}
Output: 2
Explanation: Following are the operations performed to maximize the bitwise AND of arr[]
Initially bitwise AND of {1, 2, 3} = 0
Incrementing arr[0] by 1 updates arr[] = {2, 2, 3}.
Now bitwise AND of {2, 2, 3} is 2 which is maximum possible.
Therefore, the answer is 2.
Input: arr[] = {10, 10}
Output: 10
Explanation: Do not perform any operation that would be optimal.
Approach: The problem statement of this problem says to maximize AND of arr[] by replacing almost one element from arr[]. The brute force method to solve this problem is to take bitwise AND of all the elements in arr[] except one at a time and find an appropriate replacement for that element whose contribution is not added in the whole bitwise AND of arr[] each time. Do this for all the elements in arr[] and find the optimal result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maxAnd( int n, vector< int > a)
{
int ans = 0;
for ( int i = 0; i < n; i++) {
int max_and = 0XFFFFFFFF;
for ( int j = 0; j < n; j++) {
if (i != j) {
max_and = max_and & a[j];
}
}
ans = max(ans, max_and);
}
return ans;
}
int main()
{
int N = 3;
vector< int > arr = { 1, 2, 3 };
cout << maxAnd(N, arr) << "\n" ;
}
|
Java
import java.io.*;
class GFG{
static int maxAnd( int n, int [] a)
{
int ans = 0 ;
for ( int i = 0 ; i < n; i++)
{
int max_and = 0XFFFFFFFF ;
for ( int j = 0 ; j < n; j++)
{
if (i != j)
{
max_and = max_and & a[j];
}
}
ans = Math.max(ans, max_and);
}
return ans;
}
public static void main(String[] args)
{
int N = 3 ;
int [] arr = { 1 , 2 , 3 };
System.out.println( maxAnd(N, arr) );
}
}
|
Python
def maxAnd(n, a):
ans = 0
for i in range ( 0 , n):
max_and = 0XFFFFFFFF
for j in range ( 0 , n):
if (i ! = j):
max_and = max_and & a[j]
ans = max (ans, max_and)
return ans
if __name__ = = '__main__' :
N = 3
arr = [ 1 , 2 , 3 ]
print (maxAnd(N, arr))
|
C#
using System;
class GFG{
static int maxAnd( int n, int [] a)
{
int ans = 0;
for ( int i = 0; i < n; i++)
{
uint max_and = 0XFFFFFFFF;
for ( int j = 0; j < n; j++)
{
if (i != j)
{
max_and = Convert.ToUInt32(max_and & a[j]);
}
}
ans = Math.Max(ans, ( int )max_and);
}
return ans;
}
public static void Main()
{
int N = 3;
int [] arr = { 1, 2, 3 };
Console.Write( maxAnd(N, arr) );
}
}
|
Javascript
<script>
const maxAnd = (n, a) => {
let ans = 0;
for (let i = 0; i < n; i++)
{
let max_and = 0XFFFFFFFF;
for (let j = 0; j < n; j++)
{
if (i != j)
{
max_and = max_and & a[j];
}
}
ans = Math.max(ans, max_and);
}
return ans;
}
let N = 3;
let arr = [ 1, 2, 3 ];
document.write(maxAnd(N, arr));
</script>
|
Time Complexity: O(N^2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...