-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalancedRound.c
More file actions
44 lines (40 loc) · 1.12 KB
/
BalancedRound.c
File metadata and controls
44 lines (40 loc) · 1.12 KB
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
#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
return (*(long long int *)a - *(long long int *)b);
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
long long int numElements, threshold;
scanf("%lld %lld", &numElements, &threshold);
long long int arr[numElements];
for (long long int i = 0; i < numElements; i++)
scanf("%lld", &arr[i]);
// Using qsort for sorting the array
qsort(arr, numElements, sizeof(long long int), compare);
long long int i = 0;
long long int maxConsecutive = 0;
while (i < numElements)
{
long long int j = i + 1;
while (j < numElements)
{
if (arr[j] - arr[j - 1] > threshold)
{
break;
}
j++;
}
long long int consecutive = j - i;
maxConsecutive = (maxConsecutive > consecutive) ? maxConsecutive : consecutive;
i = j;
}
printf("%lld\n", numElements - maxConsecutive);
}
return 0;
}