-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.3.3.cpp
38 lines (35 loc) · 850 Bytes
/
14.3.3.cpp
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
#include <iostream>
using namespace std;
// // write a program to find a unique number in an array where all numbers except one, are present thrice
int getBit(int num, int pos){
return (num & (1<<pos)) != 0;
}
int setBit(int num, int pos){
return num | (1<<pos);
}
int uniqueNumber(int arr[], int n){
int result = 0;
for(int i = 0; i<64; i++){
int occourence = 0;
for(int j=0; j<n; j++){
// Check if bit at position i in arr[j] is set or not
if(getBit(arr[j], i)){
occourence++;
}
}
if(occourence % 3 != 0){
result = setBit(result, i);
}
}
return result;
}
int main(){
int n;
cin>>n;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
cout<<uniqueNumber(arr, n)<<endl;
return 0;
}