> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Friends Of Appropriate Ages Python Solution

> Tested Python solution for LeetCode 825 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 825, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Two Pointers](/catalog/topics/two-pointers), [Binary Search](/catalog/topics/binary-search), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/friends-of-appropriate-ages/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 825   # by problem number
lcpy gen -s friends_of_appropriate_ages   # by problem name
```

## Problem

\<p>There are \<code>n\</code> persons on a social media website. You are given an integer array \<code>ages\</code> where \<code>ages\[i]\</code> is the age of the \<code>i\<sup>th\</sup>\</code> person.\</p>

\<p>A Person \<code>x\</code> will not send a friend request to a person \<code>y\</code> (\<code>x != y\</code>) if any of the following conditions is true:\</p>

\<ul>
\<li>\<code>age\[y] \<= 0.5 \* age\[x] + 7\</code>\</li>
\<li>\<code>age\[y] > age\[x]\</code>\</li>
\<li>\<code>age\[y] > 100 && age\[x] \< 100\</code>\</li>
\</ul>

\<p>Otherwise, \<code>x\</code> will send a friend request to \<code>y\</code>.\</p>

\<p>Note that if \<code>x\</code> sends a request to \<code>y\</code>, \<code>y\</code> will not necessarily send a request to \<code>x\</code>. Also, a person will not send a friend request to themself.\</p>

\<p>Return \<em>the total number of friend requests made\</em>.\</p>

\<p> \</p>

### Examples

```
Input: ages = [16,16]
Output: 2
Explanation: 2 people friend request each other.
```

```
Input: ages = [16,17,18]
Output: 2
Explanation: Friend requests are made 17 -> 16, 18 -> 17.
```

```
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 \* 10^4
* 1 \<= ages\[i] \<= 120

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/friends_of_appropriate_ages/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/friends_of_appropriate_ages/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n + 120^2)
    # Space: O(120)
    def num_friend_requests(self, ages: list[int]) -> int:
        count = [0] * 121
        for age in ages:
            count[age] += 1
        total = 0
        for x in range(1, 121):
            if count[x] == 0:
                continue
            for y in range(1, 121):
                if count[y] == 0:
                    continue
                if y <= 0.5 * x + 7 or y > x:
                    continue
                total += count[x] * count[y]
                if x == y:
                    total -= count[x]
        return total
```

## Complexity

| Time | Space |
| - | - |
| O(n + 120^2) | O(120) |

## Tags


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.