GFG App
Open App
Browser
Continue

# Max XOR sum

Given four integers A, B, C, and D, the task is to find the maximum sum of the given numbers by using the below operations any number of times.

• Calculate X = A ^ B, and replace A or B with  X
• Calculate Y = C ^ D, and replace C or D with Y.

Examples:

Input: arr[] = {4, 5, 9, 12}
Output: 30
Explanation: X = 4 ^ 5 = 1
Y = 9 ^ 12 = 5 replace A with X and D with Y
X = 1 ^ 5 = 4
Y = 9 ^ 5 = 12 replace A with X and D with Y
Now, A + B + C + D = 30, it can be proved that this is the maximum sum that can be obtained

Input: arr[] = {0, 1, 0, 1}
Output: 4
Explanation: X = 0 ^ 1 = 1
Y = 1 ^ 0 = 1 replace A with X and C with Y
Now, A + B + C + D = 4, It can be proved that this is the maximum sum that can be proved.

Approach: This can be solved with the following idea:

This can be solved by some mathematical operations.

Below are the steps of implementation:

• Calculate the XOR of A and B, C and D.
• See maximum of (A + B), ( A + X), and (B + X).
• Similarly for C and D.
• Return the sum of the maximums found above.

Below is the implementation for the above approach:

## C++

 // C++ code for the above approach: #include using namespace std;   // Function to find maximum sum int maxSumXor(int A, int B, int C, int D) {     // Calculate X     int X = A ^ B;       // Calculate Y     int Y = C ^ D;       // Calculating maximum value     // A + b can gain     int fpos = max(A + B, max(A + X, X + B));       // Calculating maximum value     // C + D can gain     int spos = max(C + D, max(C + Y, Y + D));       // Calculating (maximum of A + B) +     // (maximum of C + D)     int ans = fpos + spos;       return ans; }   // Driver code int main() {     int a = 4, b = 5, c = 9, d = 12;       // Function call     cout << maxSumXor(a, b, c, d);     return 0; }

## Java

 // Java code for the above approach: import java.util.*;   class GFG {     // Function to find maximum sum   public static int maxSumXor(int A, int B, int C, int D)   {     // Calculate X     int X = A ^ B;       // Calculate Y     int Y = C ^ D;       // Calculating maximum value     // A + b can gain     int fpos = Math.max(A + B, Math.max(A + X, X + B));       // Calculating maximum value     // C + D can gain     int spos = Math.max(C + D, Math.max(C + Y, Y + D));       // Calculating (maximum of A + B) +     // (maximum of C + D)     int ans = fpos + spos;       return ans;   }     // Driver code   public static void main(String[] args)   {     int a = 4, b = 5, c = 9, d = 12;       // Function call     System.out.println(maxSumXor(a, b, c, d));   } }   // This code is contributed by Prasad Kandekar(prasad264)

## Python3

 # Python code for the above approach   # Function to find maximum sum     def maxSumXor(A, B, C, D):     # Calculate X     X = A ^ B       # Calculate Y     Y = C ^ D       # Calculating maximum value     # A + b can gain     fpos = max(A + B, max(A + X, X + B))       # Calculating maximum value     # C + D can gain     spos = max(C + D, max(C + Y, Y + D))       # Calculating (maximum of A + B) +     # (maximum of C + D)     ans = fpos + spos       return ans     # Driver code a, b, c, d = 4, 5, 9, 12   # Function call print(maxSumXor(a, b, c, d))

## C#

 // C# code for the above approach   using System;   class GFG {     // Function to find maximum sum     static int maxSumXor(int A, int B, int C, int D)     {         // Calculate X         int X = A ^ B;           // Calculate Y         int Y = C ^ D;           // Calculating maximum value         // A + b can gain         int fpos = Math.Max(A + B, Math.Max(A + X, X + B));           // Calculating maximum value         // C + D can gain         int spos = Math.Max(C + D, Math.Max(C + Y, Y + D));           // Calculating (maximum of A + B) +         // (maximum of C + D)         int ans = fpos + spos;           return ans;     }       // Driver code     public static void Main()     {         int a = 4, b = 5, c = 9, d = 12;           // Function call         Console.Write(maxSumXor(a, b, c, d));     } }

## Javascript



Output

30

Time Complexity: O(1)
Auxiliary space: O(1)

My Personal Notes arrow_drop_up