There are n persons 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 a person y (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] < 100Otherwise, x will send a friend request to 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 themself.
Return the total number of friend requests made.
Example 1:
Input: ages = [16,16] Output: 2 Explanation: 2 people friend request each other.
Example 2:
Input: ages = [16,17,18] Output: 2 Explanation: Friend requests are made 17 -> 16, 18 -> 17.
Example 3:
Input: ages = [20,30,100,110,120] Output: 3 Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
Constraints:
n == ages.length1 <= n <= 2 * 1041 <= ages[i] <= 120Problem Overview: You receive an array where ages[i] represents a person's age. A person A can send a friend request to B only if specific age constraints are satisfied. The task is to count how many valid friend requests are sent across the entire group. The challenge is avoiding unnecessary pair checks when the input size grows.
Approach 1: Brute Force Pair Checking (Time: O(n2), Space: O(1))
Iterate through every pair of people using two nested loops. For each pair (i, j), verify the three rules: age[j] > 0.5 * age[i] + 7, age[j] <= age[i], and the special constraint involving ages above 100. If the conditions pass, count the request. This approach directly models the problem logic and is easy to implement, but it performs a comparison for every pair. With up to thousands of people, the O(n²) time complexity becomes expensive. It works well only for small inputs or when validating the rules during initial problem exploration using basic array traversal.
Approach 2: Optimized Counting with Age Frequency (Time: O(n + A2), Space: O(A))
Instead of checking every pair of individuals, count how many people have each age. Since ages range only from 1 to 120, build a frequency array where count[a] stores how many people are age a. Then iterate over all age pairs (ageA, ageB). If the friend request rules allow someone of age ageA to send a request to ageB, add count[ageA] * count[ageB] to the result. When ageA == ageB, subtract self-requests because a person cannot send a request to themselves.
This optimization dramatically reduces work because you evaluate at most 120 × 120 age combinations instead of n² people combinations. The idea relies on grouping values and applying conditions to aggregated counts. You can also improve the constant factor by sorting the ages and using two pointers or binary search to locate valid ranges for each age. All these variants rely on the same insight: friend requests depend only on age ranges, not individual identities.
Recommended for interviews: Interviewers expect you to move beyond the brute force check quickly. Starting with the O(n²) approach shows you understand the request rules. The optimized counting or sorted two-pointer method demonstrates algorithmic thinking and reduces the complexity to roughly O(n + A²) (with A = 120) or near-linear time after sorting. That improvement is the key signal of strong problem-solving skill.
In this approach, we will use two nested loops to check all possible pairs of people. For each pair, we will check the conditions provided in the problem to determine if a friend request should be sent. Though simple, this approach can be inefficient for larger datasets.
The code iterates through each pair of ages and evaluates the given conditions. If none of the conditions are met, a friend request is considered valid and the counter is incremented. Note the nested looping, which results in O(n^2) time complexity.
Time Complexity: O(n^2), where n is the number of people (ages).
Space Complexity: O(1), as no additional space beyond variables is used.
To optimize the previous brute force solution, we can use counting to reduce the number of comparisons. By first counting the occurrences of each age, we minimize unnecessary comparisons through sorted age analysis.
We use a count array to store the frequency of each age. Then, we compute the number of friend requests by iterating over possible age pairs and applying the given conditions, multiplying applicable pairs by their frequencies.
Time Complexity: O(MAX_AGE^2) = O(121^2)
Space Complexity: O(MAX_AGE) = O(121)
We can use an array cnt of length 121 to record the number of people of each age.
Next, we enumerate all possible age pairs (ax, ay). If ax and ay satisfy the conditions given in the problem, these age pairs (ax, ay) can send friend requests to each other.
If ax = ay, meaning the ages are the same, then the number of friend requests between ax and ay is cnt[ax] times (cnt[ax] - 1). Otherwise, if the ages are different, the number of friend requests between ax and ay is cnt[ax] times cnt[ay]. We accumulate these friend request counts into the answer.
The time complexity is O(n + m^2), where n is the length of the array ages, and m is the maximum age, which is 121 in this problem.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Brute Force Approach | Time Complexity: O(n^2), where n is the number of people (ages). |
| Optimized Counting Approach | Time Complexity: O(MAX_AGE^2) = O(121^2) |
| Counting + Enumeration | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Pair Check | O(n²) | O(1) | Good for understanding the request rules or when input size is very small |
| Sorting + Two Pointers | O(n log n) | O(1) | Useful when working directly on the sorted ages array and finding valid ranges efficiently |
| Counting by Age Frequency (Optimal) | O(n + A²) | O(A) | Best when ages are within a small fixed range (1–120), allowing aggregated counting |
Leetcode 825 Friends Of Appropriate Ages • Ren Zhang • 5,602 views views
Watch 9 more video solutions →Practice Friends Of Appropriate Ages with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor