목차

수 고르기

ps
링크acmicpc.net/…
출처BOJ
문제 번호2230
문제명수 고르기
레벨골드 5
분류

투 포인터

시간복잡도O(nlogn)
인풋사이즈n<=100,000
사용한 언어Python
제출기록33052KB / 164ms
최고기록136ms
해결날짜2021/12/07

풀이

코드

"""Solution code for "BOJ 2230. 수 고르기".

- Problem link: https://www.acmicpc.net/problem/2230
- Solution link: http://www.teferi.net/ps/problems/boj/2230

Tags: [Two Pointer]
"""

import sys

INF = float('inf')


def main():
    N, M = [int(x) for x in sys.stdin.readline().split()]
    nums = [int(sys.stdin.readline()) for _ in range(N)]

    nums.sort()
    min_diff = INF
    l_iter, r_iter = iter(nums), iter(nums)
    left, right = next(l_iter), next(r_iter)
    while right is not None:
        diff = right - left
        if M <= diff < min_diff:
            min_diff = diff
        if diff > M:
            left = next(l_iter)
        else:
            right = next(r_iter, None)

    print(min_diff)


if __name__ == '__main__':
    main()