4
4
* Problem name: Maximum Array XOR problem
5
5
* Problem code: MAXARXOR
6
6
* 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
+ *
7
29
*/
8
30
import java .util .*;
9
31
import java .lang .*;
@@ -22,11 +44,15 @@ public static void main (String[] args) throws java.lang.Exception
22
44
long N =sc .nextInt ();
23
45
long K =sc .nextInt ();
24
46
47
+ //maximum value each cell of the array should have
25
48
count =(long )Math .pow (2 ,N );
26
49
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
27
52
if (K >=count /2 )
28
53
K =count /2 ;
29
54
55
+ //finding out the maximum value of Array
30
56
count =(count -1 )*K *2 ;
31
57
32
58
System .out .println (count );
0 commit comments