forked from Bassem-ElHusseiny/HackeRrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nearlySimilarRectangles.c
49 lines (39 loc) · 1.1 KB
/
nearlySimilarRectangles.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
#include <stdio.h>
#include <stdlib.h>
// -------------------------------------- O(n log n) Using Quick sort ---------------------------------------------------//
long gcd(long a, long b) // Greatest Common Divisor Function
{
return b > 0 ? gcd(b, a % b) : a;
}
long compare(const long *a, const long *b) // Compare Function To Sort 2D Array
{
return (*a - *b) ? (*a - *b) : (*(a + 1) - *(b + 1));
};
long nearlySimilarRectangles(int sides_rows, int sides_columns, long **sides)
{
long arr[sides_rows][2];
for (int i = 0; i < sides_rows; i++)
{
long z = gcd(sides[i][0], sides[i][1]);
arr[i][0] = sides[i][0] / z;
arr[i][1] = sides[i][1] / z;
}
qsort(arr, sides_rows, sizeof(arr[0]), compare);
long count = 1;
long sum = 0;
long acc = 0;
for (int i = 0; i < sides_rows - 1; i++)
{
if (arr[i][0] == arr[i + 1][0] && arr[i][1] == arr[i + 1][1])
{
count++;
acc = ((count * (count - 1)) / 2); // n(n-1)/2
continue;
}
sum += acc;
count = 1;
acc = 0;
}
sum += acc;
return sum;
};