Skip to main content

Friends Of Appropriate Ages - Solution & Explanation

MediumArrayTwo PointersBinary SearchSorting17 min readAsked at: Amazon, Microsoft, Meta
Practice this problem

Problem Statement

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] + 7
  • age[y] > age[x]
  • age[y] > 100 && age[x] < 100

Otherwise, 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.length
  • 1 <= n <= 2 * 104
  • 1 <= ages[i] <= 120

Approach Overview

Problem 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 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.

Approach 1: Brute Force Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

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.

Try this approach in the editor →

Approach 2: Optimized Counting Approach

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(MAX_AGE^2) = O(121^2)
Space Complexity: O(MAX_AGE) = O(121)

Try this approach in the editor →

Approach 3: Counting + Enumeration

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.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Brute Force Approach

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.

Optimized Counting Approach

Time Complexity: O(MAX_AGE^2) = O(121^2)
Space Complexity: O(MAX_AGE) = O(121)

Counting + Enumeration

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair CheckO(n²)O(1)Good for understanding the request rules or when input size is very small
Sorting + Two PointersO(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

Video Solution

Leetcode 825 Friends Of Appropriate AgesRen Zhang5,707 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Friends Of Appropriate Ages easy or hard?
Friends Of Appropriate Ages is rated Medium difficulty. The rules for valid requests are straightforward, but the challenge is recognizing that checking every pair is inefficient. Efficient solutions rely on sorting, two pointers, or counting techniques.
Friends Of Appropriate Ages Python/Java solution
Python and Java implementations typically follow the same idea: either sort the ages and use two pointers to find valid ranges, or build a frequency array of size 121 and compute valid age combinations. Both approaches significantly reduce unnecessary pair comparisons.
How to solve Friends Of Appropriate Ages in O(n)?
A near-linear solution uses an age frequency array and prefix counts. Instead of evaluating each person pair, count how many people exist for each age and compute valid ranges that satisfy the request rules. Because the age range is limited to 120 values, the computation effectively behaves like O(n).
What is the best approach for Friends Of Appropriate Ages?
The most efficient approach counts how many people have each age and evaluates valid age pairs instead of individual pairs. Since ages range from 1 to 120, you can iterate through all age combinations and apply the friend request rules. This reduces the complexity to roughly O(n + A^2) where A is the age range (120), making it far faster than O(n^2).
Is Friends Of Appropriate Ages asked at Google/Amazon/Meta?
Friends Of Appropriate Ages appears in interview preparation sets for companies such as Amazon, Google, and Meta because it tests reasoning with constraints, counting optimizations, and range-based conditions. The problem also evaluates your ability to move from brute force to aggregated counting or two-pointer techniques.
What data structure is used in Friends Of Appropriate Ages?
The common solutions use arrays for age storage and a frequency array for counting how many people share the same age. Some implementations also use sorting combined with two pointers or binary search to find valid age ranges efficiently.
What is the time complexity of Friends Of Appropriate Ages?
The brute force solution runs in O(n^2) because every pair of people must be checked. The optimized counting approach runs in O(n + A^2), where A is the maximum possible age (120). Sorting-based solutions run in O(n log n) followed by linear scans with two pointers.

Ready to solve this problem?

Practice Friends Of Appropriate Ages with our built-in code editor and test cases.

Practice on FleetCode