Count friend requests based on age rules
Problem Description
You are given an array ages, where ages[i] represents the age of the i-th person. A person of age A will send a friend request to a person of age B if and only if all of the following conditions are met:
B > 0.5 * A + 7B <= A
Each request is directed (A → B). A person cannot send a request to themselves. This means if there are multiple people of the same age, they can send requests to each other, but a single individual cannot send a request to themselves.
Return the total number of friend requests that will be sent.
Input
ages: An integer array whereages[i]is the age of the i-th person.
Output
- An integer representing the total number of directed friend requests.
Constraints
1 <= n <= 2 * 10^4, wherenis the number of people (length of theagesarray).1 <= ages[i] <= 120, whereages[i]is the age of a person.
Examples
Example 1:
ages = [16, 16]
- A person of age 16 can send a request to another person of age 16 because:
16 > 0.5 * 16 + 7(which is16 > 8 + 7 = 15) - True16 <= 16- True
- Since there are two distinct 16-year-olds, the first can request the second, and the second can request the first.
- Total = 2
Example 2:
ages = [20, 30, 100, 110, 120]
- The total number of directed requests satisfying the rules needs to be computed.
AI interview prep
Core dilemma
Optimizing the counting of friend requests from O(N^2) to a more efficient solution by leveraging the extremely small age range constraint (1-120), while correctly handling the specific age rules and the 'distinct person' requirement.
2 tradeoffs and 2 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Count friend requests based on age rules
Problem Description
You are given an array ages, where ages[i] represents the age of the i-th person. A person of age A will send a friend request to a person of age B if and only if all of the following conditions are met:
B > 0.5 * A + 7B <= A
Each request is directed (A → B). A person cannot send a request to themselves. This means if there are multiple people of the same age, they can send requests to each other, but a single individual cannot send a request to themselves.
Return the total number of friend requests that will be sent.
Input
ages: An integer array whereages[i]is the age of the i-th person.
Output
- An integer representing the total number of directed friend requests.
Constraints
1 <= n <= 2 * 10^4, wherenis the number of people (length of theagesarray).1 <= ages[i] <= 120, whereages[i]is the age of a person.
Examples
Example 1:
ages = [16, 16]
- A person of age 16 can send a request to another person of age 16 because:
16 > 0.5 * 16 + 7(which is16 > 8 + 7 = 15) - True16 <= 16- True
- Since there are two distinct 16-year-olds, the first can request the second, and the second can request the first.
- Total = 2
Example 2:
ages = [20, 30, 100, 110, 120]
- The total number of directed requests satisfying the rules needs to be computed.