BOJ 2230 : 수 고르기
2020. 9. 9. 02:58ㆍPS/Two Pointers
투 포인터 문제다.
두 포인터를 움직일 때 범위를 주의하자.
#include <bits/stdc++.h>
#define MEM 100003
#define sanic ios_base::sync_with_stdio(0)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pi;
const ll MOD = 1e9+7;
ll n,m;
ll a[MEM];
int main()
{
sanic; cin.tie(0);
cin >> n >> m;
for(int i=0; i<n; i++)
cin >> a[i];
sort(a,a+n);
ll l=0, r=0, ans=2e9+7;
while(l<=r && l<n && r<n){
ll f=a[r]-a[l];
//cout << l << ' ' << r << ' ' << f << '\n';
if(f<m) r++;
else if(f>=m) ans = min(ans, f), l++;
}
cout << ans;
}