Maximum commulative XOR of elements with their indices
Last Updated :
29 Nov, 2023
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++
#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;
}
}
int main()
{
long N = 2;
long K = 1;
Max_XOR(N, K);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
long N = 2 ;
long K = 1 ;
Max_XOR(N, K);
}
public static void Max_XOR( long N, long K)
{
long ans = ( long )Math.pow( 2 , N);
if (K >= ans / 2 ) {
System.out.println((ans - 1 ) * ans);
}
else {
System.out.println((ans - 1 ) * ( 2 * K));
}
}
}
|
Python3
def max_xor(n, k):
ans = 2 * * n
if k > = ans / / 2 :
print ((ans - 1 ) * ans)
else :
print ((ans - 1 ) * ( 2 * k))
if __name__ = = "__main__" :
N = 2
K = 1
max_xor(N, K)
|
C#
using System;
class Program
{
static void Max_XOR( long N, long K)
{
long ans = ( long )Math.Pow(2, N);
if (K >= ans / 2)
{
Console.WriteLine((ans - 1) * ans);
}
else
{
Console.WriteLine((ans - 1) * (2 * K));
}
}
static void Main()
{
long N = 2;
long K = 1;
Max_XOR(N, K);
}
}
|
Javascript
function Max_XOR(N, K) {
let ans = Math.pow(2, N);
if (K >= ans / 2) {
console.log((ans - 1) * ans);
} else {
console.log((ans - 1) * (2 * K));
}
}
function Main() {
let N = 2;
let K = 1;
Max_XOR(N, K);
}
Main();
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...