Friends Of Appropriate Ages - Problem
Friends Of Appropriate Ages
You're building a social media platform where users can send friend requests to each other. However, you need to implement an age-appropriate friend matching system to ensure meaningful connections.
Given an array
Friend Request Rules:
Person
•
•
•
Otherwise, person
Important: Friend requests are unidirectional (x sending to y doesn't mean y sends to x), and people don't send requests to themselves.
Return the total number of friend requests that will be sent.
You're building a social media platform where users can send friend requests to each other. However, you need to implement an age-appropriate friend matching system to ensure meaningful connections.
Given an array
ages where ages[i] represents the age of the i-th person, you need to determine how many friend requests will be sent in total.Friend Request Rules:
Person
x will NOT send a friend request to person y if any of these conditions are true:•
age[y] <= 0.5 * age[x] + 7 (y is too young for x)•
age[y] > age[x] (y is older than x)•
age[y] > 100 && age[x] < 100 (special rule for users over 100)Otherwise, person
x will send a friend request to person y.Important: Friend requests are unidirectional (x sending to y doesn't mean y sends to x), and people don't send requests to themselves.
Return the total number of friend requests that will be sent.
Input & Output
example_1.py — Basic Case
$
Input:
[16,16]
›
Output:
2
💡 Note:
Both 16-year-olds can send friend requests to each other. Person 0 sends to Person 1, and Person 1 sends to Person 0. For age 16: valid range is (15.5, 16] = 16, so they can send requests to each other.
example_2.py — Mixed Ages
$
Input:
[16,17,18]
›
Output:
2
💡 Note:
Age 17 can send to 16 (17 > 15.5), Age 18 can send to both 16 and 17. Age 16 cannot send to anyone (16 ≤ 15 for 17, 16 ≤ 16 for 18 but 16 < 17 and 16 < 18). Total: 0 + 1 + 1 = 2 requests.
example_3.py — No Valid Requests
$
Input:
[20,30,100,110,120]
›
Output:
3
💡 Note:
Age 20: can't send to anyone (all others too old). Age 30: can send to 20. Age 100: can send to 20 and 30. Ages 110,120: can send to everyone except those over 100 when sender < 100. Total: 0 + 1 + 2 + 0 + 0 = 3.
Constraints
- n == ages.length
- 1 ≤ n ≤ 2 × 104
- 1 ≤ ages[i] ≤ 120
- All ages are positive integers between 1 and 120
Visualization
Tap to expand
Understanding the Visualization
1
Understand the Rules
Each person has three age-based restrictions that determine who they won't send requests to
2
Brute Force Check
The naive approach checks every possible sender-receiver pair individually
3
Optimize with Grouping
Group people by age and calculate valid ranges mathematically
4
Use Prefix Sums
Build cumulative counts to quickly determine how many people fall in any age range
Key Takeaway
🎯 Key Insight: By grouping people by age and using mathematical range calculations with prefix sums, we reduce complexity from O(n²) to O(A²) where A=120, making the solution much more efficient for large datasets with duplicate ages.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code