Show problems as
Classic problem #825MediumArrayTwo PointersBinary SearchSorting

Friends Of Appropriate Ages

Real-world version: How Many Invitations Go Out
Tracked in this browser only
Read Friends Of Appropriate Ages on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

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 starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

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