forked from architsingla13/InterviewBit-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDifferentBitsSum.java
More file actions
41 lines (31 loc) · 812 Bytes
/
DifferentBitsSum.java
File metadata and controls
41 lines (31 loc) · 812 Bytes
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
package BitManipulation;
import java.util.ArrayList;
/**
* Author - archit.s
* Date - 13/10/18
* Time - 1:27 PM
*/
public class DifferentBitsSum {
final int M = (int) 1e9+7;
public int cntBits(ArrayList<Integer> A) {
long sum = 0;
for(int i=31;i>=0;i--){
long count = 0;
for(int j=0;j<A.size();j++){
if((A.get(j)& (1<<i)) == 0){
count++;
}
}
sum = (sum + 2L*count*(A.size()-count))%M;
sum = sum%M;
}
return (int)sum%M;
}
public static void main(String[] args) {
ArrayList<Integer> r = new ArrayList<>();
r.add(1);
r.add(3);
r.add(5);
System.out.println(new DifferentBitsSum().cntBits(r));
}
}