-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch-Pairs.py
More file actions
41 lines (30 loc) · 820 Bytes
/
Search-Pairs.py
File metadata and controls
41 lines (30 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#Problem Link : https://www.hackerrank.com/challenges/pairs/problem?isFullScreen=true&h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=search
#Ans:
#!/bin/python3
import math
import os
import random
import re
import sys
#
# Complete the 'pairs' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
# 1. INTEGER k
# 2. INTEGER_ARRAY arr
#
from bisect import bisect_left
def main():
n, k = map(int, input().split())
a = sorted(map(int, input().split()))
count = 0
for i in range(n):
x = a[i] + k
position = bisect_left(a, x, i + 1)
while position < n and a[position] == x:
count += 1
position += 1
print(count)
if __name__ == '__main__':
main()