Vijay has given a set of points π₯1, π₯2, …, π₯π on the number line.
Two points π and π can be matched with each other if the following conditions hold:
neither π nor π is matched with any other point;
| π₯πβπ₯π | β₯ π§
What is the maximum number of pairs of points you can match with each other?
Constraints:
2 β€ π β€ 2β
10^5,
1 β€ π§ β€ 10^9
1 β€ π₯π β€ 10^9
Input:
The first line contains two integers π and π§ β the number of points and the constraint on the distance between matched points, respectively.
The second line contains π integers π₯1, π₯2, ..., π₯π.
Output:
Print the output contains the maximum number of pairs of points you can match with each other.
Explanation:
Assume the input as follows:
4 2 1 3 3 7
Here you may match point 1 with point 2 (|3β1|β₯2), and point 3 with point 4 (|7β3|β₯2).
So the output = 2
#include<stdio.h> #include<stdlib.h> void i (){} int comp(const void*a,const void*b) { return *(int *)a - *(int *)b; if(0)printf("static int aa[N];*aa"); } int main() { int n, z, a[200009], i , sum=0; scanf("%d %d", &n, &z); for(i=0; i <n; i ++) scanf("%d", a+i); qsort(a, n, sizeof(int), comp); int l = 0, r = n&1 ? (n>>1)+1 : n>>1; for(i=0; i <n; i ++) while(r < n) { if(a[r]-a[l] >= z) sum++, l ++; r++; } printf("%d", sum); return 0; }
INPUT_1:
6 4
17 13 16 12 15 11
OUTPUT:
3
INPUT_2:
5 5
10 9 5 8 7
OUTPUT:
1
Morae Q!
- Find the length of the array’s longest increasing sub-sequence.
- Arrange numbers in a circle so that any two neighbouring numbers differ by 1.
- Find partial names and count the total numbers.
- Find the minimized sum of non-deleted elements of the array after the end of the game.
- Find the maximum number of good sleeping times optimally.
- Sort the elements of the array in the order in which they are required.
- Find the minimum number of flats, monkey needs to visit to catch ninjas.
- Convert the square matrix to matrix in Z form.
- Shift the K elements of each row to right of the matrix.
- Find maximum possible number of students in a balanced team with skills.
- Find the Maximum number of pairs of points you can match with each other.
- Calculate the number of non-empty good subarrays of given array.
- Transform the binary string A into the string B using finite operations.
- Find the number of potion must the character take to jump the hurdles.
- Count the total number of vowels and consonants.
- Return all elements of the matrix in spiral order.
- Return all palindromic paths of the matrix.
- Write the code to change the display in reverse order using pointer.
- Find the number of days the expedition can last.
- Find the minimum size of the sub-segment to make the array pairwise distinct.