Skip to content

Commit 6b62ad7

Browse files
committed
Done the requested changes
1 parent 538f8b5 commit 6b62ad7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Codechef Problems/MaximumArrayXOR.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,28 @@
44
* Problem name: Maximum Array XOR problem
55
* Problem code: MAXARXOR
66
* Problem link: https://www.codechef.com/START7C/problems/MAXARXOR
7+
*
8+
* Approach:
9+
*
10+
* Value of an array A is defined as the sum of (A(i)⊕i) for all 0≤i<L, where ⊕ denotes bitwise xor operation.
11+
* You are given an integer N and an array A consisting of 2^N integers where A(i)=i for all 0≤i<2^N.
12+
* Example :
13+
* - For N=1, you have an array A of length 2^1=2 and A=[0,1].
14+
* - For N=2, you have an array A of length 2^2=4 and A=[0,1,2,3].
15+
*
16+
* You can do at most K operations on this array. In one operation, you can choose
17+
* two indices i and j (0≤i,j<2^N) and swap A(i) and A(j) (i.e. A(i) becomes A(j) and vice versa).
18+
*
19+
* In this problem, we need to find the maximum value of array A after at most K operations.
20+
* Maximum value is only possible when the value (A(i)⊕i) is 2^N - 1 for each index. So to get maximum value for each index
21+
* we have to swap values according to the condition. But to get the solution of this problem we don't really need to swap.
22+
* We can find the maximum value of array without actually swapping them.
23+
*
24+
* For every single swap we will maximize the value of two indexes in the array, so for K operations we can
25+
* maximize 2*K cells. But it is also possible that 2*K cells can be greater than 2^N so that's why
26+
* we will reduce it. It's given in the question that atmost K operations are allowed. So now we need
27+
* to multiply 2*K to 2^N - 1, to get the maximum value.
28+
*
729
*/
830
import java.util.*;
931
import java.lang.*;
@@ -22,11 +44,15 @@ public static void main (String[] args) throws java.lang.Exception
2244
long N=sc.nextInt();
2345
long K=sc.nextInt();
2446

47+
//maximum value each cell of the array should have
2548
count=(long)Math.pow(2,N);
2649

50+
//reducing value of K if it is greater than count/2
51+
//count/2 is the maximum number of swaps that can be performed to get the maximum value of Array
2752
if(K>=count/2)
2853
K=count/2;
2954

55+
//finding out the maximum value of Array
3056
count=(count-1)*K*2;
3157

3258
System.out.println(count);

0 commit comments

Comments
 (0)