forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElementOccurringOnce.java
59 lines (55 loc) · 1.67 KB
/
ElementOccurringOnce.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
package me.ramswaroop.bits;
/**
* Created by IntelliJ IDEA.
*
* @author: ramswaroop
* @date: 6/14/15
* @time: 12:37 AM
*/
public class ElementOccurringOnce {
/**
* Returns the element occurring once in {@param a} having
* other elements repeating thrice.
*
* @param a
* @return
*/
public static int getElementOccurringOnceInElementsRepeatedThreeTimes(int a[]) {
int result = 0;
for (int i = 0; i < 32; i++) {
int sum = 0;
// sum of all bits at ith position for all elements in a[]
for (int j = 0; j < a.length; j++) {
sum += (a[j] >>> i) & 1;
}
// bit will be set in result if sum mod 3 isn't 0
result |= (sum % 3) << i;
}
return result;
}
public static void main(String a[]) {
System.out.println(getElementOccurringOnceInElementsRepeatedThreeTimes(new int[]{12, 12, 6, 6, 2, 12, 6}));
System.out.println(getElementOccurringOnceInElementsRepeatedThreeTimes(new int[]{5, 5, 45, 45, 456, 5, 45}));
System.out.println(getElementOccurringOnceInElementsRepeatedThreeTimes(new int[]{12, 12, 34, 34, 6, 12, 34}));
}
}
/**
* EXPLANATION:
* 12 = 1100
* 12 = 1100
* 6 = 0110
* 6 = 0110
* 2 = 0010
* 12 = 1100
* 6 = 0110
* -------------
* res = 0010
*
* 1st bit of res = (0+0+0+0+0+0+0) % 3 = 0
* 2nd bit of res = (0+0+1+1+1+0+1) % 3 = 1
* 3rd bit of res = (1+1+1+1+0+1+1) % 3 = 0
* 4th bit of res = (1+1+0+0+0+1+0) % 3 = 0
*
* NOTE: Sum of bits at a particular position will not be divisible
* by 3 if the no. occurring once has a set bit at that position.
*/