Ctrl K

Requests Under an Age Rule

Medium
A social network has n users, and ages[i] is the age of user i. Every user x sends a friend request to every other user y that meets both conditions: - ages[y] ≤ ages[x]: y is not older than x; - ages[y] > ages[x] / 2 + 7: y is not too young for x (this is ordinary division, not integer division). Nobody sends a request to themselves. A request from x to y does not imply one from y to x. How many requests are sent in total?
Example 1
ages=[30, 30, 15] → 2

The two 30-year-olds write to each other: 2 requests. Nobody writes to the 15-year-old, since a 30-year-old only writes to people older than 30 / 2 + 7 = 22, and he writes to nobody because everyone else is older.

Example 2
ages=[40, 25, 34, 14] → 2

40 → 34 (bound 27) and 34 → 25 (bound 24). No other pair works: 25 and 14 are too young for 40, and 14 is too young for 25 (bound 19.5).

Example 3
ages=[14, 14] → 0

For age 14 the bound is 14 / 2 + 7 = 14 and the recipient must be strictly older than that, so there are no requests.

Constraints
  • 1 ≤ len(ages) ≤ 2 · 10⁴
  • 1 ≤ ages[i] ≤ 120
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTriples Under a Limit MediumWarm Every Cabin MediumPairs With a Fixed Gap MediumStick Triples That Form a Triangle MediumNearest k Values Medium
loading Python…
Input
ages=[30, 30, 15]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 105 tests on the server, hidden ones included.