Given two integers N and K. Consider that there is an array A[] of 2N elements starting from 0. Then the task is to output the maximum cumulative XOR of elements with their respective indices after performing K number of swaps.
Examples:
Input: N = 2, K = 1
Output: 6
Explanation: For N = 2, A[] will be: {0, 1, 2, 3}. It will be optimal to swap A[1] = 1 and A[2] = 2, then A[] = {0, 2, 1, 3}. The cumulative XOR of elements with their indices will be (0^0) + (2^1) + (1^2) + (3^3) = 0 + 3 + 3 + 0 = 6. Which is the maximum possible XOR value with 1 swap.Input: N = 3, K = 2
Output: 28
Explanation: It can be verified that with 2 optimal swaps, the maximum cumulative XOR of elements with respective to their indices will be 28.
Approach: To solve the problem follow the below observations:
Observation:
The problem is based on Bitwise concept and can be solved using observations. Consider the test case below:
Test Case: N = 3, Then:
- A[] = {0, 1, 2, 3, 4, 5, 6, 7}
- Indices = {0, 1, 2, 3, 4, 5, 6, 7}
Then cumulative XOR will be zero because: (0^0) + (1^1) + (2^2) . . . . + (7^7) = 0.
Can we make XOR maximum, If we change the position of element at different indices?
- A[] = {0, 1, 2, 3, 4, 5, 6, 7}
- Indices = {7, 6, 5, 4, 3, 2, 1, 0}
Then cumulative XOR will be: (0^7) + (1^6) + (7^0). Which will be maximum because in binary form the set bit of element will be with unset bit of index.
Clarification is below:
- Elements of A[] in Binary form: 000 001 010 011 100 101 110 111
- Indices in Binary form: 111 110 101 100 011 010 001 000
Cumulative XOR = (000^111) + (001^110) + . . . . (111^000). Which will be maximum because set bit of element is XORed with unset bit of index and vise-versa.
So, the observation is, we have to make optimal swaps by using above observation.
Algorithm:
- Declare a variable let say ans and initialize it equal to Pow(2, N) that is the length of array.
-
If (K >= ans/2)
- Then all the pairs (i,n-1-i) can be swaped and xor of all these index is maximised to (ans-1).
- Maximum sum of xor’s of indices is (ans-1)*ans.
-
Else
- Only k pairs are swapped and maximum xor of (ans-1) is achvied at 2*k indices.
- Maximum sum of xor’s of indices is (ans-1)*2k.
Implementation of the above approach:
// C++ program for the given approach #include <bits/stdc++.h> using namespace std;
void Max_XOR( long N, long K)
{ long ans = pow (2, N);
if (K >= ans / 2) {
cout << (ans - 1) * ans << endl;
}
else {
cout << (ans - 1) * (2 * K) << endl;
}
} // Drivers Code int main()
{ long N = 2;
long K = 1;
Max_XOR(N, K);
return 0;
} |
// Java code to implement the approach import java.util.*;
class GFG {
public static void main(String[] args)
{
// Inputs
long N = 2 ;
long K = 1 ;
// Function call
Max_XOR(N, K);
}
// Method to output the maximum XOR
// with K swaps
public static void Max_XOR( long N, long K)
{
long ans = ( long )Math.pow( 2 , N);
if (K >= ans / 2 ) {
// Then all the pairs (i,n-1-i) can be swaped
// and xor of all these index is maximised to
// (ans-1)
System.out.println((ans - 1 ) * ans);
}
else {
// Only k pairs are swapped and maximum xor of
// (ans-1) is achvied at 2*k indices
System.out.println((ans - 1 ) * ( 2 * K));
}
}
} |
def max_xor(n, k):
# Calculate the maximum possible value of XOR
ans = 2 * * n
# Check if K is greater than or equal to half the maximum XOR value
if k > = ans / / 2 :
# If yes, output the maximum possible XOR value
print ((ans - 1 ) * ans)
else :
# If no, output a modified XOR value based on the value of K
print ((ans - 1 ) * ( 2 * k))
# Main function if __name__ = = "__main__" :
N = 2 # Given N value
K = 1 # Given K value
# Calculate and print the maximum XOR value
max_xor(N, K)
|
using System;
class Program
{ // Function to calculate and print the maximum XOR value
static void Max_XOR( long N, long K)
{
// Calculate the maximum value that can be represented with N bits
long ans = ( long )Math.Pow(2, N);
if (K >= ans / 2)
{
// If K is greater than or equal to half of the maximum value, use this formula
Console.WriteLine((ans - 1) * ans);
}
else
{
// If K is less than half of the maximum value, use this formula
Console.WriteLine((ans - 1) * (2 * K));
}
}
static void Main()
{
// Input values
long N = 2; // Number of bits
long K = 1; // Given value
// Call the Max_XOR function with the provided input values
Max_XOR(N, K);
}
} |
function Max_XOR(N, K) {
// Calculate the maximum value that can be represented with N bits
let ans = Math.pow(2, N);
if (K >= ans / 2) {
// If K is greater than or equal to half of the maximum value, use this formula
console.log((ans - 1) * ans);
} else {
// If K is less than half of the maximum value, use this formula
console.log((ans - 1) * (2 * K));
}
} function Main() {
// Input values
let N = 2; // Number of bits
let K = 1; // Given value
// Call the Max_XOR function with the provided input values
Max_XOR(N, K);
} Main(); |
6
Time Complexity: O(1)
Auxiliary Space: O(1)