Maximize Bitwise AND of Array by replacing at most one element Last Updated : 18 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: 2Explanation: Following are the operations performed to maximize the bitwise AND of arr[]Initially bitwise AND of {1, 2, 3} = 0Incrementing 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: 10Explanation: 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; // Function to find maximum AND by // Replacing atmost one element by any value int maxAnd(int n, vector<int> a) { // To Calculate answer int ans = 0; // Iterating through the array for (int i = 0; i < n; i++) { int max_and = 0XFFFFFFFF; // Checking and for element and // Leaving only jth element for (int j = 0; j < n; j++) { if (i != j) { max_and = max_and & a[j]; } } // Comparing previous answers // and max answers ans = max(ans, max_and); } return ans; } // Driver Code int main() { int N = 3; vector<int> arr = { 1, 2, 3 }; // Function Call cout << maxAnd(N, arr) << "\n"; } Java import java.io.*; class GFG{ // Function to find maximum AND by // Replacing atmost one element by any value static int maxAnd(int n,int[] a) { // To Calculate answer int ans = 0; // Iterating through the array for(int i = 0; i < n; i++) { int max_and = 0XFFFFFFFF; // Checking and for element and // Leaving only jth element for(int j = 0; j < n; j++) { if (i != j) { max_and = max_and & a[j]; } } // Comparing previous answers // and max answers ans = Math.max(ans, max_and); } return ans; } // Driver Code public static void main(String[] args) { int N = 3; int[] arr = { 1, 2, 3 }; // Function Call System.out.println( maxAnd(N, arr) ); } } // This code is contributed by Potta Lokesh Python # Function to find maximum AND by # Replacing atmost one element by any value def maxAnd(n, a): # To Calculate answer ans = 0 # Iterating through the array for i in range(0, n): max_and = 0XFFFFFFFF # Checking and for element and # Leaving only jth element for j in range(0, n): if (i != j): max_and = max_and & a[j] # Comparing previous answers # and max answers ans = max(ans, max_and) return ans # Driver Code if __name__ == '__main__': N = 3 arr = [ 1, 2, 3 ] print(maxAnd(N, arr)) # This code is contributed by Samim Hossain Mondal. C# using System; class GFG{ // Function to find maximum AND by // Replacing atmost one element by any value static int maxAnd(int n,int[] a) { // To Calculate answer int ans = 0; // Iterating through the array for(int i = 0; i < n; i++) { uint max_and = 0XFFFFFFFF; // Checking and for element and // Leaving only jth element for(int j = 0; j < n; j++) { if (i != j) { max_and = Convert.ToUInt32(max_and & a[j]); } } // Comparing previous answers // and max answers ans = Math.Max(ans, (int)max_and); } return ans; } // Driver Code public static void Main() { int N = 3; int[] arr = { 1, 2, 3 }; // Function Call Console.Write( maxAnd(N, arr) ); } } // This code is contributed by gfgking JavaScript <script> // Function to find maximum AND by // Replacing atmost one element by any value const maxAnd = (n, a) => { // To Calculate answer let ans = 0; // Iterating through the array for(let i = 0; i < n; i++) { let max_and = 0XFFFFFFFF; // Checking and for element and // Leaving only jth element for(let j = 0; j < n; j++) { if (i != j) { max_and = max_and & a[j]; } } // Comparing previous answers // and max answers ans = Math.max(ans, max_and); } return ans; } // Driver Code let N = 3; let arr = [ 1, 2, 3 ]; // Function Call document.write(maxAnd(N, arr)); // This code is contributed by rakeshsahni </script> Output: 2 Time Complexity: O(N^2) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Maximize Bitwise AND of Array by replacing at most one element H harshalkhond Follow Improve Article Tags : Bit Magic Competitive Programming DSA Arrays Bitwise-AND +1 More Practice Tags : ArraysBit Magic Similar Reads 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 Maximize array sum by replacing at most L elements to R for Q queries Given an array arr[] consisting of N integers and an array Query[][] consisting of M pairs of the type {L, R}, the task is to find the maximum sum of the array by performing the queries Query[][] such that for each query {L, R} replace at most L array elements to the value R. Examples: Input: arr[]= 7 min read Minimize the maximum of Array by replacing any element with other element at most K times Given an array arr[] of size N and an integer K, the task is to minimize the value of the maximum element of the array arr[] after replacing any element of the array with any other element of that array at most K times.Examples:Input: arr[] = {5, 3, 3, 2, 1}, K = 3Output: 2Explanation: Replace the e 8 min read Minimize the max of Array by breaking array elements at most K times Given an integer array arr[] of size N and a positive integer K, the task is to minimize the maximum of the array by replacing any element arr[i] into two positive elements (X, Y) at most K times such that arr[i] = X + Y. Examples: Input: arr = {9}, K = 2Output: 3Explanation: Operation 1: Replace el 15+ min read Maximize the minimum element of Array by reducing elements one by one Given an array arr[] containing N integers. In each operation, a minimum integer is chosen from the array and deleted from the array after subtracting it from the remaining elements. The task is to find the maximum of minimum values of the array after any number of such operations. Examples: Input: 6 min read Maximize the cost of reducing array elements Given an array arr[] of N positive integers. We can choose any one index(say K) of the array and reduce all the elements of the array from index 0 to K - 1 by 1. The cost of this operation is K. If at any index(say idx) element is reduced to 0 then we can't perform this operation in the range [idx, 6 min read Minimize the maximum frequency of Array elements by replacing them only once Given an array A[] of length N, the task is to minimize the maximum frequency of any array element by performing the following operation only once: Choose any random set (say S) of indices i (0 ? i ? N-1) of the array such that every element of S is the same and Replace every element of that index w 7 min read Maximize the count of distinct elements in Array after at most K changes Given an array arr[], the task is to find the maximum number of distinct numbers in arr after at most K changes. In each change pick any element X from arr and change it to Y such that L <= Y <= R. Examples: Input: arr[] = {1, 2, 1, 4, 6, 4, 4}, L = 1, R = 5 and K = 2Output: 6Explanation: Foll 8 min read Maximize first array element by performing given operations at most K times Given an array arr[] of size N an integer K, the task is to find the maximize the first element of the array by performing the following operations at most K times: Choose a pair of indices i and j (0 ? i, j ? N-1) such that |i ? j| = 1 and arri > 0.Set arri = arri ? 1 and arrj = arrj + 1 on thos 7 min read Rearrange array elements to maximize the sum of MEX of all prefix arrays Given an array arr[] of size N, the task is to rearrange the array elements such that the sum of MEX of all prefix arrays is the maximum possible. Note: MEX of a sequence is the minimum non-negative number not present in the sequence. Examples: Input: arr[] = {2, 0, 1}Output: 0, 1, 2Explanation:Sum 7 min read Like