All problems
1045MediumArrayTwo PointersBinary SearchSorting

How Many Invitations Go Out

Tracked in this browser only
Write code

Trains the technique from

LeetCode 825Friends Of Appropriate Ages

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A club's members have the ages ages. A member x sends an invitation to a different member y exactly when both of these hold:

  • y is no older than x;
  • y's age is more than half of x's age plus seven.

Invitations go one way only, so x inviting y and y inviting x are counted separately, and nobody invites themselves.

Return how many invitations go out altogether.

Examples

Example 1

Input
ages = [15, 15]
Output
2

Neither member is older than the other, and fifteen is more than half of fifteen plus seven, which is fourteen and a half, so both invitations go out.

Example 2

Input
ages = [100, 100, 100]
Output
6

A hundred is more than half of a hundred plus seven, so every member invites every other: three members with two others apiece.

Example 3

Input
ages = [1]
Output
0

A single member has nobody else to invite, and nobody invites themselves.

Constraints

  • 1 <= ages.length <= 2 * 10^4
  • 1 <= ages[i] <= 120

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def num_friend_requests(ages: list[int]) -> int:
Java
public int numFriendRequests(int[] ages)
September 7
Apply