Keys by Value Range
Python
Hard
3 views
Problem Description
Read n pairs key value. Then two integers L and R. Output how many keys have value in range [L,R].
Input Format
First line n. Next n lines: key value. Last line: L R.
Output Format
One integer count.
Sample Test Case
Input:
5
a 3
b 10
c -2
d 7
e 7
3 7
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
n=int(lines[0].strip())
vals=[]
for i in range(1,1+n):
parts=(lines[i] if i<len(lines) else '').split()
if len(parts)<2: continue
vals.append(int(parts[1]))
L,R=map(int,(lines[1+n] if 1+n<len(lines) else '0 0').split())
ans=0
for v in vals:
if L<=v<=R:
ans+=1
sys.stdout.write(str(ans))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!