↓Skip to main content
  1. LeetCode/

LeetCode 825: Friends Of Appropriate Ages

·3 mins· ·
LeetCode Medium Two-Pointers Sorting Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-21

  • Submissions: Wrong Answer ×2 → Runtime Error → Wrong Answer ×2 → Accepted

Approach
#

x sends a request to y when:

  • age[y] > 0.5 * age[x] + 7
  • age[y] <= age[x]
  • The third rule (age[y] > 100 and age[x] < 100) can never apply once age[y] <= age[x], so ignore it.

So a person aged a can invite everyone aged in (0.5a + 7, a], except themself.

After sorting, that is a contiguous range [left, right). Processing people from youngest to oldest, both ends only move right, so two same-direction pointers maintain it:

  • left: skip everyone <= 0.5a + 7.
  • right: take in everyone <= a (people of the same age included).

When the range is non-empty the person is inside it (someone qualifies only if a > 0.5a + 7), so add right - left - 1.

My Attempts
#

VersionProblem
Attempts 1–2, WAMoved right forward and counted only the people left of right, so same-age pairs were counted in one direction, then patched with “+1 per repeated age”; the count was wrong. For [16, 16, 16] all three invite each other, so the answer is 6; both returned 4
Attempt 3, RESwitched to two pointers but computed the threshold from ages[right], which goes out of range once right reaches n. [20, 20] raises IndexError
Attempts 4–5, WAUsed ages[i] for the threshold, but the left loop ran while left < right, so too-young people were not all skipped. For [5, 5], the threshold for age 5 is 9.5 and nobody can be invited, so the answer is 0; this returned 1

Changing the left condition to left < n passed.

Solution
#

class Solution:
    def numFriendRequests(self, ages: List[int]) -> int:
        ages.sort()
        n = len(ages)
        ans = left = right = 0
        for age in ages:
            while left < n and ages[left] <= 0.5 * age + 7:
                left += 1                          # too young
            while right < n and ages[right] <= age:
                right += 1                         # not older than this person
            if right > left:
                ans += right - left - 1            # exclude this person
        return ans

Alternatively, ages are only 1 to 120, so a count array and an O(120^2) loop over age pairs also works.

Complexity
#

Time O(n log n), space O(1) apart from sorting.

Mistakes to Avoid
#

  • People of the same age invite each other: count both directions.
  • Bounding a pointer with left < right: too-young people may not all be skipped.
  • Subtract the person themself only when the range is non-empty.

Interview Explanation
#

After simplifying, a person aged a invites those aged in (0.5a + 7, a]; the third rule is implied by the second. I sort the ages and process people in order. Both ends of that range only move right, so two same-direction pointers maintain it, and the range size minus the person themself is their number of requests. Time O(n log n).

Related

LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 611: Valid Triangle Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Greedy
LeetCode 611 note: sort, fix the longest side k, and use two pointers for the shorter sides; when the sum is large enough every i in between works, adding j - i at once
LeetCode 287: Find the Duplicate Number
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search Bit-Manipulation
LeetCode 287 note: treat i -> nums[i] as a linked list; the duplicate is the cycle entry, found with Floyd’s algorithm in O(1) space without modifying the array
LeetCode 167: Two Sum II - Input Array Is Sorted
·2 mins
LeetCode Medium Array Two-Pointers Binary-Search
LeetCode 167 note: sorted, so move two pointers inward, shifting right when the sum is too large and left when too small; indices are 1-based
LeetCode 75: Sort Colors
·2 mins
LeetCode Medium Array Two-Pointers Sorting Quicksort
LeetCode 75 note: the Dutch flag, with 0s left of low and 2s right of high; after swapping a 2 to high, do not advance, since the swapped-in value is unchecked