Skip to content

Commit baf8616

Browse files
authored
Unique Number - III Solution Added
1 parent c151f8e commit baf8616

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Hacker Blocks/Unique Number - III.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* Hacker Blocks */
2+
/* Title - Unique Number - III */
3+
/* Created By - Akash Modak */
4+
/* Date 7/1/2020 */
5+
6+
7+
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+
1 1 1 2 2 2 3
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+
using namespace std;
31+
int main() {
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
41+
return 0;
42+
}

0 commit comments

Comments
 (0)