-
Notifications
You must be signed in to change notification settings - Fork 40
/
find_repeating_elements.c
76 lines (69 loc) · 1.65 KB
/
find_repeating_elements.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
70
71
72
73
74
75
76
/*
* Date: 2018-09-27
*
* Description:
* Given an array of n elements which contains elements from 0 to n-1, with any
* of these numbers appearing any number of times. Find these repeating numbers
* in O(n) and using only constant memory space.
*
* For example, let n be 7 and array be {1, 2, 3, 1, 3, 6, 6}, the answer
* should be 1, 3 and 6.
*
* Approach:
* - Traverse the given array from i= 0 to n-1 elements.
* - Go to index arr[i]%n and increment its value by n.
* - Now traverse the array again and print all those indexes i for which
* arr[i]/n is greater than 1.
*
* This approach works because all elements are in range from 0 to n-1 and
* arr[i]/n would be greater than 1 only if a value "i" has appeared more than
* once.
*
* Complexity:
* O(N) time
* O(1) space
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int idx = 0;
int n = 0;
int *a = NULL;
printf("Enter number of elements: ");
scanf("%d", &n);
a = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &a[i]);
}
for (i = 0; i < n; i++) {
idx = a[i] % n;
a[idx] += n;
}
for (i = 0; i < n; i++) {
if (a[i] / n > 1)
printf("Repeated number: %d\n", i);
}
return 0;
}
/*
* Output:
* -------------------
* Enter number of elements: 4
* Enter element[0]: 2
* Enter element[1]: 2
* Enter element[2]: 4
* Enter element[3]: 4
* Repeated number: 0
* Repeated number: 2
*
* Enter number of elements: 5
* Enter element[0]: 3
* Enter element[1]: 3
* Enter element[2]: 2
* Enter element[3]: 2
* Enter element[4]: 4
* Repeated number: 2
* Repeated number: 3
*/