RUNCOMPARE - Editorial

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2
Contest: Division 3
Contest: Division 4

Author: iceknight1093
Testers: wuhudsm, satyam_343
Editorialist: iceknight1093

DIFFICULTY:

TBD

PREREQUISITES:

None

PROBLEM:

Alice and Bob ran for N days each, with distances A_i and B_i on the i-th day, respectively.
On a given day, each person is unhappy if the other one ran more than twice their distance; and happy otherwise.

On how many of the days were both Alice and Bob happy?

EXPLANATION:

On the i-th day,

  • Alice is happy if B_i \leq 2\cdot A_i
  • Bob is happy if A_i \leq 2\cdot B_i

So, they’re both happy only when both conditions are true simultaneously.

This leads to a rather simple solution: using a loop and a conditional statement, simply count the number of i such that (A_i \leq 2\cdot B_i) and (B_i \leq 2\cdot A_i).

TIME COMPLEXITY

\mathcal{O}(N) per test case.

CODE:

Editorialist's code (Python)
for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = 0
	for i in range(n):
	    if a[i] <= 2*b[i] and b[i] <= 2*a[i]: ans += 1
	print(ans)