-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path02_unique_number_I.cpp
65 lines (44 loc) · 1.43 KB
/
02_unique_number_I.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem Name - Unique Number - I
We are given an array containg n numbers. All the numbers are present twice except for one number
which is only present once. Find the unique number without taking any extra spaces and in linear time.
(Hint - Use Bitwise)
Input Format: First line contains the number n.
Second line contains n space separated number.
Constraints: n < 10^5
Output Format: Output a single line containing the unique number
Sample Input: 7
1 1 2 2 3 3 4
Sample Output: 4
Explanation: 4 is present only once
*/
#include <iostream>
using namespace std;
int main()
{
int total_num, num, ans=0;
cout << "Enter total numbers: ";
cin >> total_num;
cout << "Enter numbers: ";
for(int i=0; i<total_num; i++)
{
cin >> num;
// Using bitwise XOR Operator to solve, It helped to not use any storage
ans = ans^num;
}
cout<<"Unique No. is : "<< ans << endl;
return 0;
}
/*
OUTPUT:
Enter total numbers: 7
Enter numbers: 1 1 2 2 3 3 4
Unique No. is : 4
APPROACH:
Use property of xor function to solve this problem.
Concept :
If we take XOR of zero and some bit, it will return that bit: a^0 = a
If we take XOR of two same bits, it will return 0: a^a=0
a^b^a = (a^a)^b = 0^b = b
So we can XOR all bits together to find the unique number.
*/