Skip to content

Commit d940c16

Browse files
authored
Merge pull request #857 from Karnika06/Devincept13
Added Maximum Array XOR problem (CodeChef)
2 parents 5cc93ba + 9b6d82a commit d940c16

File tree

2 files changed

+76
-118
lines changed

2 files changed

+76
-118
lines changed

Codechef Problems/FlappyBird.java

Lines changed: 0 additions & 118 deletions
This file was deleted.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Implementation of Maximum Array XOR problem from CodeChef
3+
*
4+
* Problem name: Maximum Array XOR problem
5+
* Problem code: MAXARXOR
6+
* Problem link: https://www.codechef.com/START7C/problems/MAXARXOR
7+
*
8+
* Solution:
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+
*
29+
*/
30+
import java.util.*;
31+
import java.lang.*;
32+
import java.io.*;
33+
34+
class MaximumArrayXOR
35+
{
36+
public static void main (String[] args) throws java.lang.Exception
37+
{
38+
Scanner sc=new Scanner(System.in);
39+
int t=sc.nextInt();
40+
41+
while(t-->0)
42+
{
43+
long count=0;
44+
long N=sc.nextInt();
45+
long K=sc.nextInt();
46+
47+
//maximum value each cell of the array should have
48+
count=(long)Math.pow(2,N);
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
52+
if(K>=count/2)
53+
K=count/2;
54+
55+
//finding out the maximum value of Array
56+
count=(count-1)*K*2;
57+
58+
System.out.println(count);
59+
60+
}
61+
}
62+
}
63+
/*
64+
* Sample Input 1
65+
* 3
66+
* 2 0
67+
* 2 1
68+
* 10 100
69+
*
70+
* Sample Output 1
71+
* 0
72+
* 6
73+
* 204600
74+
*
75+
* Time complexity: O(n)
76+
*/

0 commit comments

Comments
 (0)