-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Unique_Number_III.cs
48 lines (41 loc) · 1.11 KB
/
Unique_Number_III.cs
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
//Unique_Number_III
//Given an array that contains all elements occurring 3 times, but one occurs only once. Find that unique element.
using System.IO;
using System;
class Neoalgo {
static void Main() {
int[] count = new int[64];
int len, no, i;
Console.WriteLine("Enter number of element:");
len = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter space separated elements:");
for (i = 0; i < len; i++) {
//Reading array element
no = Convert.ToInt32(Console.ReadLine());
int j = 0;
//"AND" each element of the array with each set digit
while (no > 0) {
int last_bit = no & 1;
count[j] += last_bit;
j++;
no = no >> 1;
}
}
//consider all bits whose count is not multiple of 3
int num = 1;
int ans = 0;
for (i = 0; i < 64; i++) {
count[i] %= 3;
ans += (count[i] * num);
num = num << 1;
}
Console.Write("unique num is:");
Console.Write(ans);
}
}
/* output
Enter number of element: 7
Enter space separated elements: 10 10 12 21 21 10 21
unique num is: 12
Time Complexity:O(n)
*/