Rudolf and the Ticket (Codeforces 1941A)
Tags: Math, brute force

Rudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed $$$k$$$.Rudolf has two pockets with coins. In the left pocket, there are $$$n$$$ coins with denominations $$$b_1, b_2, \dots, b_n$$$. In the right pocket, there are $$$m$$$ coins with denominations $$$c_1, c_2, \dots, c_m$$$. He wants to choose exactly one coin from the left pocket and exactly one coin from the right pocket (two coins in total).Help Rudolf determine how many ways there are to select indices $$$f$$$ and $$$s$$$ such that $$$b_f + c_s \le k$$$.

Input: The first line contains an integer $$$t$$$ ($$$1 \le t \le 100$$$) — the number of test cases. Then follows the description of each test case.The first line of each test case contains three natural numbers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n, m \le 100, 1 \le k \le 2000$$$) — the number of coins in the left and right pockets, and the maximum sum of two coins for the ticket payment at the counter, respectively.The second line of each test case contains $$$n$$$ integers $$$b_i$$$ ($$$1 \le b_i \le 1000$$$) — the denominations of coins in the left pocket.The third line of each test case contains $$$m$$$ integers $$$c_i$$$ ($$$1 \le c_i \le 1000$$$) — the denominations of coins in the right pocket.

Output: For each testcase, output a single integer — the number of ways Rudolf can select two coins, taking one from each pocket, so that the sum of the coins does not exceed $$$k$$$.

Note: Note that the pairs indicate the indices of the coins in the array, not their denominations.In the first test case, Rudolf can choose the following pairs of coins: $$$[1, 1], [1, 2], [1, 4], [2, 1], [2, 2], [2, 4]$$$.In the second test case, Rudolf cannot choose one coin from each pocket in any way, as the sum of any two elements from the first and second arrays will exceed the value of $$$k=4$$$.In the third test case, Rudolf can choose: $$$[1, 1], [2, 1], [3, 1], [4, 1]$$$.In the fourth test case, Rudolf can choose any coin from the left pocket and any coin from the right pocket.

Sample Inputs:
4
4 4 8
1 5 10 14
2 1 8 1
2 3 4
4 8
1 2 3
4 2 7
1 1 1 1
2 7
3 4 2000
1 1 1
1 1 1 1

Sample Outputs:
6
0
4
12

Solution (Python):
import math
from itertools import accumulate
from collections import Counter
from collections import defaultdict
import heapq
from math import e
import bisect
from collections import deque
import sys
from sys import stdout
for _ in range(int(input())):
    (n, m, k) = map(int, input().split())
    nums_b = list(map(int, input().split()))
    nums_c = list(map(int, input().split()))
    ans = 0
    nums_c.sort()
    for x in nums_b:
        j = bisect.bisect_right(nums_c, k - x)
        ans += j
    print(ans)

Solution (Python):
def count_ways(n, m, k, b, c):
    b.sort()
    c.sort()
    count = 0
    j = m - 1
    for i in range(n):
        while j >= 0 and b[i] + c[j] > k:
            j -= 1
        count += j + 1
    return count
t = int(input())
for _ in range(t):
    (n, m, k) = map(int, input().split())
    b = list(map(int, input().split()))
    c = list(map(int, input().split()))
    print(count_ways(n, m, k, b, c))

Solution (Python):
def count_ways(n, m, k, left_coins, right_coins):
    left_coins.sort()
    right_coins.sort()
    ways = 0
    j = m - 1
    for i in range(n):
        while j >= 0 and left_coins[i] + right_coins[j] > k:
            j -= 1
        ways += j + 1
    return ways
t = int(input().strip())
for _ in range(t):
    (n, m, k) = map(int, input().strip().split())
    left_coins = list(map(int, input().strip().split()))
    right_coins = list(map(int, input().strip().split()))
    print(count_ways(n, m, k, left_coins, right_coins))