
투포인터 알고리즘을 사용했다.
투포인터 알고리즘은
1. start = end = 0
2. 항상 start <= end를 만족한다.
start=0, end=0부터 시작해서
1 2 3 5에서
3 - 1 = 2
5 - 1 = 4 이므로
굳이 start를 가만히 놔둘 필요가 없다.
1 2 3 4 5
m = 1000
라는 케이스인 경우, end가 계속 증가하기 때문에 배열 범위를 넘어갈 수 있다. 이를 방지하기 위함
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
	ios::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	int n, m, ans = 2000000000;
	cin >> n >> m;
	vector<int> v(n);
	for (int i = 0; i < n; i++)
		cin >> v[i];
	sort(v.begin(), v.end());
	int start = 0, end = 0;
	while (start <= end && end < n)
	{
		int sub = v[end] - v[start];
		if (sub < m) // 답이 아님
			end++;
		else
		{
			ans = min(ans, sub);
			start++;
		}
	}
	cout << ans;
	return 0;
}