You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are given an array containg n numbers. All the numbers are present thrice except for one number which is only present once. Find the unique number. Only use - bitwise operators, and no extra space.
8
+
9
+
Input Format
10
+
First line contains the number n. Second line contains n space separated number.
11
+
12
+
Constraints
13
+
N < 10^5
14
+
15
+
Output Format
16
+
Output a single line containing the unique number
17
+
18
+
Sample Input
19
+
7
20
+
1112223
21
+
Sample Output
22
+
3
23
+
Explanation
24
+
3 is present only once
25
+
26
+
27
+
#include<iostream>
28
+
#include<numeric>
29
+
#include<unordered_set>
30
+
usingnamespacestd;
31
+
intmain() {
32
+
int n;
33
+
cin>>n;
34
+
int a[n];
35
+
for(int i=0;i<n;i++)
36
+
cin>>a[i];
37
+
unordered_set<int> s(a,a+n);
38
+
int sum_array = accumulate(a,a+n,0);
39
+
int sum_set = accumulate(s.begin(),s.end(),0);
40
+
cout<<(3*sum_set-sum_array)/2; //(k*sum_set-sum_array)/(k-1) where is number of similar elements
0 commit comments