Friends Of Appropriate Ages - Problem
There are n people on a social media website. You are given an integer array ages where ages[i] is the age of the ith person.
A person x will not send a friend request to person y (where x != y) if any of the following conditions is true:
age[y] <= 0.5 * age[x] + 7age[y] > age[x]age[y] > 100 && age[x] < 100
Otherwise, person x will send a friend request to person y.
Note that if x sends a request to y, y will not necessarily send a request to x. Also, a person will not send a friend request to themselves.
Return the total number of friend requests made.
Input & Output
Example 1 — Basic Case
$
Input:
ages = [16,16]
›
Output:
2
💡 Note:
Both 16-year-olds can send requests to each other. Person 0 sends to person 1, and person 1 sends to person 0. Total: 2 requests.
Example 2 — Multiple Ages
$
Input:
ages = [16,17,18]
›
Output:
2
💡 Note:
Person aged 17 can send to 16 (17 > 0.5×17+7=15.5). Person aged 18 can send to 17 (18 > 0.5×18+7=16). Total: 2 requests.
Example 3 — No Valid Requests
$
Input:
ages = [20,30,100,110,120]
›
Output:
3
💡 Note:
Only age 100 people can send requests among themselves. With 1 person of age 100, no requests are possible between different people.
Constraints
- 1 ≤ ages.length ≤ 2 × 104
- 1 ≤ ages[i] ≤ 120
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array of ages: [16, 17, 18]
2
Apply Rules
Check three conditions for each pair
3
Output
Count total valid friend requests: 2
Key Takeaway
🎯 Key Insight: Sort ages first, then use binary search to efficiently find valid age ranges for each person
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code