Skip to content

Commit d0094cd

Browse files
author
Ayush Singh
committed
Add Day-07 solution for Number of Pairs
1 parent c2c8b8f commit d0094cd

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Problem: C. Number of Pairs
3+
Link: https://codeforces.com/problemset/problem/1535/C
4+
5+
Short Problem Statement:
6+
Given an array of integers, count the number of index pairs (i, j)
7+
such that i < j and the sum a[i] + a[j] lies between l and r (inclusive).
8+
9+
Approach:
10+
- Sort the array.
11+
- For each index i, we want to find how many indices j > i satisfy:
12+
l - a[i] <= a[j] <= r - a[i]
13+
- Since the array is sorted, binary search can be used to find
14+
the valid range of j efficiently.
15+
- Sum the counts for all i.
16+
17+
Time Complexity:
18+
O(n log n) per test case
19+
20+
Space Complexity:
21+
O(n)
22+
23+
Example:
24+
Input:
25+
3 4 7
26+
5 1 2
27+
28+
Output:
29+
2
30+
31+
Submission Link:
32+
https://codeforces.com/contest/1538/submission/356210806
33+
"""
34+
35+
import sys
36+
from bisect import bisect_left, bisect_right
37+
38+
input = sys.stdin.readline
39+
40+
t = int(input())
41+
42+
for _ in range(t):
43+
n, l, r = map(int, input().split())
44+
a = list(map(int, input().split()))
45+
46+
a.sort()
47+
ans = 0
48+
49+
for i in range(n):
50+
left = bisect_left(a, l - a[i], i + 1)
51+
right = bisect_right(a, r - a[i], i + 1)
52+
ans += (right - left)
53+
54+
print(ans)

0 commit comments

Comments
 (0)