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 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
🎯 Friend Request Age MatchingAge Rules Visualization25SenderValid Recipients for Age 25:• Must be > 0.5 × 25 + 7 = 19.5 (so ≥ 20)• Must be ≤ 25 (not older than sender)✓ Ages 20-25 are valid✗ Ages 1-19, 26+ invalidOptimization StrategyInstead of checking each person individually:1. Count frequency of each age (age → count mapping)2. Calculate valid age ranges mathematically3. Use prefix sums for instant range countingExample: Ages [25, 25, 25, 20, 18]Age 25Count: 3Range: [20,25]Age 20Count: 1Range: [17,20]Age 18Count: 1Range: [16,18]Mathematical range calculation🎯 Key Insight: Group by age + math beats checking every pair!
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.
Asked in
Facebook 35 Google 28 Amazon 22 Microsoft 15
28.5K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen