FastPrepFriend Requests by Age

Friend Requests by Age

Bloomberg LP logoBloomberg LP● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Each value in ages is one person's age. A person of age A sends a directed friend request to a different person of age B exactly when both conditions hold:

  • B > 0.5 * A + 7.
  • B <= A.

Return the total number of directed friend requests. People of the same age may request each other, but nobody may request themself.

Function

numFriendRequests(ages: int[]) → int

Examples

Example 1

ages = [16,16]return = 2

Each sixteen-year-old sends one request to the other.

Example 2

ages = [16,17,18]return = 2

The seventeen-year-old may request the sixteen-year-old, and the eighteen-year-old may request the seventeen-year-old.

Example 3

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

The valid directed requests are 110 -> 100, 120 -> 110, and 120 -> 100.

Constraints

  • 1 <= ages.length <= 20000.
  • 1 <= ages[i] <= 120.

More Bloomberg LP problems

See Bloomberg LP hiring insights
public int numFriendRequests(int[] ages) {
    // Write your code here.
}
ages[16,16]
expected2
Checking account…