프로그래밍/코딩테스트 문제풀이

[Python] Hackerrank. Pairs (Medium)

inspirit941 2025. 7. 29. 21:03
반응형

https://www.hackerrank.com/challenges/pairs/problem?isFullScreen=false

 

Pairs | HackerRank

Given N numbers, count the total pairs of numbers that have a difference of K.

www.hackerrank.com

 

 

이게 왜 medium 난이도인지 알 수 없는 문제.

 

k값이 주어지면, array에서 값 a를 뽑았을 때 a+b = k 인 b가 array에 있는지 개수를 세면 된다.

b가 array에 있는지는 python set으로 변환하면 O(1)로 확인할 수 있으니, 단순 for문으로 a+b = k인지 확인하면 된다.

 

 

 

반응형