Minimizing array sum by applying XOR operation on all elements of the array
Given an array arr[] of N integer elements, the task is to choose an element X and apply XOR operation on every element of the array with X such that array sum is minimized.
Input: arr[] = {3, 5, 7, 11, 15}
Output: 26
Binary representation of the array elements are {0011, 0101, 0111, 1011, 1111}
We take xor of every element with 7 in order to minimize the sum.
3 XOR 7 = 0100 (4)
5 XOR 7 = 0010 (2)
7 XOR 7 = 0000 (0)
11 XOR 7 = 1100 (12)
15 XOR 7 = 1000 (8)
Sum = 4 + 2 + 0 + 12 + 8 = 26Input: arr[] = {1, 2, 3, 4, 5}
Output: 14
Approach: The task is to find the element X with which we have to take xor of each element.
- Convert each number into binary form and update the frequency of bit (0 or 1) in array corresponding to position of each bit in element in array.
- Now, Traverse the array and check whether element at index is more than n/2 (for ‘n’ elements, we check whether the set bit appears more than n/2 at index) and subsequently we obtain element ‘X’
- Now, take xor of ‘X’ with all the elements and return the sum.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; const int MAX = 25; // Function to return the minimized sum int getMinSum( int arr[], int n) { int bits_count[MAX], max_bit = 0, sum = 0, ans = 0; memset (bits_count, 0, sizeof (bits_count)); // To store the frequency // of bit in every element for ( int d = 0; d < n; d++) { int e = arr[d], f = 0; while (e > 0) { int rem = e % 2; e = e / 2; if (rem == 1) { bits_count[f] += rem; } f++; } max_bit = max(max_bit, f); } // Finding element X for ( int d = 0; d < max_bit; d++) { int temp = pow (2, d); if (bits_count[d] > n / 2) ans = ans + temp; } // Taking XOR of elements and finding sum for ( int d = 0; d < n; d++) { arr[d] = arr[d] ^ ans; sum = sum + arr[d]; } return sum; } // Driver code int main() { int arr[] = { 3, 5, 7, 11, 15 }; int n = sizeof (arr) / sizeof (arr[0]); cout << getMinSum(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { static int MAX = 25 ; // Function to return the minimized sum static int getMinSum( int arr[], int n) { int bits_count[] = new int [MAX], max_bit = 0 , sum = 0 , ans = 0 ; // To store the frequency // of bit in every element for ( int d = 0 ; d < n; d++) { int e = arr[d], f = 0 ; while (e > 0 ) { int rem = e % 2 ; e = e / 2 ; if (rem == 1 ) { bits_count[f] += rem; } f++; } max_bit = Math.max(max_bit, f); } // Finding element X for ( int d = 0 ; d < max_bit; d++) { int temp = ( int )Math.pow( 2 , d); if (bits_count[d] > n / 2 ) ans = ans + temp; } // Taking XOR of elements and finding sum for ( int d = 0 ; d < n; d++) { arr[d] = arr[d] ^ ans; sum = sum + arr[d]; } return sum; } // Driver code public static void main(String[] args) { int arr[] = { 3 , 5 , 7 , 11 , 15 }; int n = arr.length; System.out.println(getMinSum(arr, n)); } } // This code has been contributed by 29AjayKumar |
Python3
# Python3 implementation of the approach MAX = 25 ; # Function to return the minimized sum def getMinSum(arr, n) : bits_count = [ 0 ] * MAX max_bit = 0 ; sum = 0 ; ans = 0 ; # To store the frequency # of bit in every element for d in range (n) : e = arr[d]; f = 0 ; while (e > 0 ) : rem = e % 2 ; e = e / / 2 ; if (rem = = 1 ) : bits_count[f] + = rem; f + = 1 max_bit = max (max_bit, f); # Finding element X for d in range (max_bit) : temp = pow ( 2 , d); if (bits_count[d] > n / / 2 ) : ans = ans + temp; # Taking XOR of elements and finding sum for d in range (n) : arr[d] = arr[d] ^ ans; sum = sum + arr[d]; return sum # Driver code if __name__ = = "__main__" : arr = [ 3 , 5 , 7 , 11 , 15 ]; n = len (arr); print (getMinSum(arr, n)) # This code is contributed by Ryuga |
C#
// C# implementation of the approach using System; class GFG { static int MAX = 25; // Function to return the minimized sum static int getMinSum( int [] arr, int n) { int [] bits_count = new int [MAX]; int max_bit = 0, sum = 0, ans = 0; // To store the frequency // of bit in every element for ( int d = 0; d < n; d++) { int e = arr[d], f = 0; while (e > 0) { int rem = e % 2; e = e / 2; if (rem == 1) { bits_count[f] += rem; } f++; } max_bit = Math.Max(max_bit, f); } // Finding element X for ( int d = 0; d < max_bit; d++) { int temp = ( int )Math.Pow(2, d); if (bits_count[d] > n / 2) ans = ans + temp; } // Taking XOR of elements and finding sum for ( int d = 0; d < n; d++) { arr[d] = arr[d] ^ ans; sum = sum + arr[d]; } return sum; } // Driver code public static void Main(String[] args) { int [] arr = { 3, 5, 7, 11, 15 }; int n = arr.Length; Console.WriteLine(getMinSum(arr, n)); } } /* This code contributed by PrinciRaj1992 */ |
26
Recommended Posts:
- Find the number of different numbers in the array after applying the given operation q times
- Minimizing array sum by subtracting larger elements from smaller ones
- Make all elements of an array equal with the given operation
- Minimum possible sum of array elements after performing the given operation
- Minimum cost to equal all elements of array using two operation
- Minimum operation to make all elements equal in array
- Sort an array after applying the given equation
- Find original array from encrypted array (An array of sums of other elements)
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Maximizing array sum with given operation
- Sum of the updated array after performing the given operation
- Make the array non-decreasing with the given operation
- Maximum possible array sum after performing the given operation
- Find minimum value to assign all array elements so that array product becomes greater
- Generate original array from an array that store the counts of greater elements on right
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.