# 825. Friends Of Appropriate Ages

![](/files/hDkYSzjk1IZ9UtBtiaoJ)

```
```

time: O(121\*121), O(1)

space:O(121), O(1)

```java
class Solution {
    public int numFriendRequests(int[] ages) {
        int count[] = new int[121];
        for (int age : ages) {
            count[age]++;
        }
        
        int res = 0;
        for (int ageA = 0; ageA <= 120; ageA++) {
            int countA = count[ageA];
            for (int ageB = 0; ageB <= 120; ageB++) {
                
                int countB = count[ageB];
                if (notAbleToSendRequest(ageA, ageB)) {
                    continue;
                }
                
                countB = (ageA == ageB) ? countB-1 : countB; // 兩年齡相同時, should 扣掉自己的一個個數, (不能對自己發邀請)
                res += countA*countB;

            }
        }
        return res;
    }
    
    private boolean notAbleToSendRequest(int ageX, int ageY) {
        return ageY <= 0.5*ageX+7 || ageY > ageX || (ageY > 100 && ageX < 100);
    }
}

/*

16, 17, 17

1*2 = 2
1


16 17 18  => in no limitation: 6 why?

16 -> 17 18

17 -> 16,18

18 -> 16,17

so exclude yourself
(age, count)

countA * countB
ageA == ageB, countA * (countA - 1)


*/
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/couting-sort/825.-friends-of-appropriate-ages.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
