-
Notifications
You must be signed in to change notification settings - Fork 40
/
number_of_distinct_pairs.c
82 lines (76 loc) · 1.81 KB
/
number_of_distinct_pairs.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
77
78
79
80
81
82
/*
* Date: 2018-10-06
*
* Description:
* Given an unsorted array, find the number of distinct pairs possible.
*
* Approach:
* Used a temp array to store elements already found in original array. Also
* used 2 pointers left and right, always maintained unique elements between
* left and right pointers, so that count is updated as right - left.
*
* Limitation:
* Will not work if range of elements is more than number of element as temp
* array is taken of size N.
*
* Complexity:
* O(N) Time and space
*/
#include "stdio.h"
#include "stdlib.h"
int main() {
int i = 0;
int n = 0, num_of_pairs = 0;
int *A = NULL, *p_tmp = NULL;
int left = 0, right = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
A = (int *)malloc(sizeof(int) * n);
p_tmp = (int *)malloc(sizeof(int) * n);
for (i = 0; i < n; i++) {
printf("Enter element[%d]: ", i);
scanf("%d", &A[i]);
p_tmp[i] = 0;
}
while (right < n) {
while ((right < n) && (!p_tmp[A[right]])) {
num_of_pairs += (right - left);
p_tmp[A[right]] = 1;
right++;
}
// Move left pointer ahead as many times as there are repeated elements.
while (p_tmp[A[left]]) {
p_tmp[A[left]] = 0;
left++;
}
}
printf("Number of pairs: %d\n", num_of_pairs);
return 0;
}
/*
* Output:
* -----------------------
* Enter number of elements: 2
* Enter element[0]: 1
* Enter element[1]: 1
* Number of pairs: 0
*
* Enter number of elements: 3
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 1
* Number of pairs: 1
*
* Enter number of elements: 4
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 3
* Enter element[3]: 1
* Number of pairs: 3
*
* Enter number of elements: 3
* Enter element[0]: 1
* Enter element[1]: 2
* Enter element[2]: 3
* Number of pairs: 3
*/