Balanced Round
You are the author of a Codeforces round and have prepared $$$n$$$ problems you are going to set, problem $$$i$$$ having difficulty $$$a_i$$$. You will do the following process:   remove some (possibly zero) problems from the list;  rearrange the remaining problems in any order you wish. A round is considered balanced if and only if the absolute difference between the difficulty of any two consecutive problems is at most $$$k$$$ (less or equal than $$$k$$$).What is the minimum number of problems you have to remove so that an arrangement of problems is balanced?
For the first test case, we can remove the first $$$2$$$ problems and construct a set using problems with the difficulties $$$[4, 5, 6]$$$, with difficulties between adjacent problems equal to $$$|5 - 4| = 1 \leq 1$$$ and $$$|6 - 5| = 1 \leq 1$$$.For the second test case, we can take the single problem and compose a round using the problem with difficulty $$$10$$$.
The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — the number of test cases.The first line of each test case contains two positive integers $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) and $$$k$$$ ($$$1 \leq k \leq 10^9$$$) — the number of problems, and the maximum allowed absolute difference between consecutive problems.The second line of each test case contains $$$n$$$ space-separated integers $$$a_i$$$ ($$$1 \leq a_i \leq 10^9$$$) — the difficulty of each problem.Note that the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.
For each test case, output a single integer — the minimum number of problems you have to remove so that an arrangement of problems is balanced.
def min_problems_to_remove(n, k, difficulties):
    difficulties.sort()
    max_length = 0
    current_length = 1
    for i in range(1, n):
        if difficulties[i] - difficulties[i - 1] <= k:
            current_length += 1
        else:
            max_length = max(max_length, current_length)
            current_length = 1
    max_length = max(max_length, current_length)
    return n - max_length
t = int(input())
for _ in range(t):
    (n, k) = map(int, input().split())
    difficulties = list(map(int, input().split()))
    print(min_problems_to_remove(n, k, difficulties))
from sys import stdin, stdout, setrecursionlimit
read = stdin.readline
write = stdout.write

def solve():
    (n, k) = map(int, read().split())
    nums = list(map(int, input().split()))
    nums.sort()
    (ans1, ans2) = (1, 1)
    maxi = -1
    for i in range(1, n):
        if nums[i] - nums[i - 1] <= k:
            ans1 += 1
        else:
            maxi = max(maxi, ans1)
            ans1 = 1
    maxi = max(maxi, ans1)
    print(n - maxi)

def main():
    t = int(input())
    for _ in range(t):
        solve()
main()
import sys
input = sys.stdin.readline
n = int(input())

def proc(arr, k):
    n = len(arr)
    arr.sort()
    maxlen = 0
    curlen = 1
    for i in range(1, n):
        if arr[i] - arr[i - 1] <= k:
            curlen += 1
        else:
            maxlen = max(curlen, maxlen)
            curlen = 1
    maxlen = max(curlen, maxlen)
    return n - maxlen
for i in range(n):
    (a, b) = map(int, input().split())
    arr = list(map(int, input().split()))
    print(proc(arr, b))