-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathfind_2_odd_occurring_numbers.c
69 lines (61 loc) · 1.69 KB
/
find_2_odd_occurring_numbers.c
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
66
67
68
69
/*
* Date: 2018-09-28
*
* Description:
* Given an array in which 2 numbers has odd number of occurrence and all other
* has even number of occurrences. Find 2 numbers which occurred odd number of
* times.
*
* Approach:
* - Take XOR of all numbers, this will nullify(0) all bits corresponding to
* all even occurring numbers and will contain set bits only at positions
* where bit is set for only of the 2 numbers like 01^10 = 11, 111^100 = 011
*
* - Now we can find first(LSB) set bit in consolidated XOR using:
* xor & ~(xor - 1) like 6 = 110, set bit = 110 & ~(101) = 110 & 010 = 010
* which has second LSB as set and 6 also has first LSB set second position.
*
* Complexity:
* O(N)
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int num_of_elements = 0;
int *p_input = NULL;
int xor = 0;
int set_bit = 0;
int x = 0, y = 0;
printf("Enter number of elements: ");
scanf("%d", &num_of_elements);
p_input = (int *)malloc(sizeof(int) * num_of_elements);
for (i = 0; i < num_of_elements; i++) {
printf("Enter element[%d]: ", i);
scanf("%d",&p_input[i]);
}
xor = p_input[0];
for (i = 1; i < num_of_elements; i++)
xor ^= p_input[i];
set_bit = xor & ~(xor - 1);
for (i = 0; i < num_of_elements; i++) {
if (set_bit & p_input[i])
x ^= p_input[i];
else
y ^= p_input[i];
}
printf("Two numbers occurring odd number of times is: %d %d\n", x, y);
return 0;
}
/*
* Output:
* -----------------
* Enter number of elements: 6
* Enter element[0]: 7
* Enter element[1]: 7
* Enter element[2]: 7
* Enter element[3]: 7
* Enter element[4]: 15
* Enter element[5]: 31
* Two numbers occurring odd number of times is: 31 15
*/