FastPrepCount Centaur Friend Requests

Count Centaur Friend Requests

Meta logoMeta● MediumINTERNPHONE SCREEN
Learn

Problem statement

A group of centaurs sign up for accounts and may send directed friend requests to one another. You are given their integer ages in ages.

A centaur with age x sends a request to a different centaur with age y only when all of these rules hold:

  1. y >= x / 2 + 7, where the division is real-number division.
  2. y <= x.
  3. If x > 100, then y >= 100.

Count the total number of directed friend requests. Centaurs with the same age are still distinct people, and no centaur may send a request to themself.

Function

countCentaurFriendRequests(ages: int[]) → long

Examples

Example 1

ages = [120,110]return = 1

The 120-year-old centaur sends one request to the 110-year-old centaur. The reverse request is disallowed because the recipient would be older.

Example 2

ages = [120,110,99]return = 1

The 99-year-old recipient is disallowed for both senders over 100, so only the request from age 120 to age 110 is sent.

Example 3

ages = [120,45,230,400,88,300,101]return = 4

Exactly four ordered sender-recipient pairs satisfy every age rule.

Example 4

ages = [101,120,230,400,88,45,55,300]return = 6

Counting valid ordered pairs across all eight distinct centaurs gives six requests.

Example 5

ages = [100,110,120]return = 3

The valid requests are from age 110 to 100 and from age 120 to both 100 and 110.

Constraints

  • 1 <= ages.length <= 2 * 10^5.
  • 1 <= ages[i] <= 10^9.
  • Each position in ages represents a distinct centaur.

More Meta problems

See Meta hiring insights
public long countCentaurFriendRequests(int[] ages) {
  // write your code here
}
ages[120,110]
expected1
Checking account…