-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlip the Bit
More file actions
57 lines (40 loc) · 1.6 KB
/
Flip the Bit
File metadata and controls
57 lines (40 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
https://www.hackerearth.com/practice/basic-programming/bit-manipulation/basics-of-bit-manipulation/practice-problems/algorithm/monk-and-binary-array-1/
import java.util.*;
class TestClass {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int a[]=new int[N];
int c[]=new int[N];
for (int i = 0; i < N; i++) {
a[i]=sc.nextInt();
}
int totalones=0;
for (int i = 0; i < N; i++)
{
int exor=0;
for(int j=i;j<N;j++)
{
exor=exor^a[j];
if(exor==0)
{
for(int k=i;k<=j;k++) //incrementing the count of all those bit which are involved in making this exor value to 0
{
c[k]++;
}
}
else
{
totalones++;
}
}
}
int max=Integer.MIN_VALUE;
for(int i=0;i<N;i++)
{
max=(int)Math.max(max,-(i+1)*(N-i)+2*c[i]); //maximizing (countof zeros- countof ones)
}
// (i+1)*(N-i) ==> total no of subaarrays formed by using this position
System.out.println(totalones+max);
}
}