-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathBitDifference.java
59 lines (44 loc) · 1.06 KB
/
BitDifference.java
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
58
59
public static long sumBitDiff(int arr[], int n) {
int ans = 0;
for(int i=0; i<32; i++) {
int cur = 0;
for(int j=0; j<n; j++) {
if((arr[j] & (1<<i)) == 0) cur++;
}
ans += cur * (n - cur) * 2;
}
return ans;
}
// For negative Numbers also
static int countBits(int n, long a[]) {
long ans = 0;
int m = 1000000007;
for(int i=0; i<31; i++) {
int count = 0;
for(int j=0; j<n; j++) {
if((a[j] & (1<<i)) == 0) count++;
}
// ans += count * (n - count) * 2;
ans += ((count%m) * ((n-count)%m)) % m * (2);
if(ans >= m)
ans -= m;
}
return (int)ans;
}
// public static long sumBitDiff(int arr[], int n) {
// long ans = 0;
// for(int i=0; i<n; i++) {
// for(int j=i; j<n; j++) {
// ans += one(arr[i] ^ arr[j]);
// }
// }
// return ans*2;
// }
// static int one(int n) {
// int ans = 0;
// while(n > 0) {
// n = n & (n-1);
// ans++;
// }
// return ans;
// }