forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoElementsOccurOnceWhileOthersOccurTwice.java
More file actions
38 lines (32 loc) · 1.21 KB
/
TwoElementsOccurOnceWhileOthersOccurTwice.java
File metadata and controls
38 lines (32 loc) · 1.21 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
// Fin the two non-repeating elements in an array where elements occur two times.
package BitManipulation;
import java.util.*;
import java.lang.*;
public class TwoElementsOccurOnceWhileOthersOccurTwice {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in the array:");
int n = sc.nextInt();
System.out.println("Enter the elements:");
int[] a = new int[n];
for(int i=0 ; i<n ; i++){
a[i] = sc.nextInt();
}
int xor = 0;
for(int i : a){
xor = xor ^ i; // finding the xor of all the elements of the array.
}
int setBit = xor & (-xor); // finding the right most set bit of the xor variable.
int x = 0;
int y = 0;
for(int i : a){
if((setBit & i) == 0){ // checking right most set bits of elements of the array is set or not
x = x ^ i; // if not set then put in in x variable after doing XOR operation.
}
else{
y = y ^ i; // else put it in y variable after performing gXOR operation.
}
}
System.out.println(x+" "+y);
}
}