# Friends Of Appropriate Ages LeetCode Solution

Difficulty Level Medium
Frequently asked in FacebookViews 198

Table of Contents

## Problem Statement :

Friends Of Appropriate Ages LeetCode Solution – There are n persons on a social media website. You are given an integer array of 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 yy 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 :

### 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.```

## Explanation :

• Given an integer array of ages where ages[i] is the age of the ith person.
• If a person x satisfies all the given conditions, then person x can send a friend request to person y.
• We need to return the total number of friend requests made.

## Approach :

• In the given constraint, ages[i]>=1 && ages[i] <=120 so, we will make the array of size 121 to store the frequency of the person’s age.

int[]freqAges=new int[121];

• Iterate over the given array and store the frequency of the person’s age.

• After that, we will traverse from age 1 to age 120 and try to make a friend request to the person, whose age is less than x, as it is given in condition 2.
• To calculate total friend requests, suppose 2 people are 16 years old and 3 people are 15 years old, then the total friend requests made by people who are 16 years old to people who are 15 years old will be 6 (2* 3  = 6). If all the people who are 16 years old follow the remaining condition.

## Code for Friends Of Appropriate Ages :

### Java Code :

```class Solution {
public int numFriendRequests(int[] ages) {
int totalFriendRequest=0;
int[]freqAges=new int[121];

for(int age:ages){
freqAges[age]++;
}
for(int x=1;x<=120;x++){
for (int y = 0; y < x; ++y) {
if (y <= 0.5 * x + 7 || (y > 100 && x < 100)) { continue; }
totalFriendRequest += freqAges[x] * freqAges[y];
}
if (x > 0.5 * x + 7) {
totalFriendRequest+=freqAges[x]*(freqAges[x]-1);
}
}
return totalFriendRequest;
}

}```

### C++ Code :

```class Solution {
public:
int numFriendRequests(vector<int>& ages) {

vector<int>freqAges(121,0);
int totalFriendRequest=0;
for(int age:ages){
freqAges[age]++;
}
for(int x=1;x<=120;x++){
for (int y = 0; y < x; ++y) {
if (y <= 0.5 * x + 7 || (y > 100 && x < 100)) { continue; }
totalFriendRequest += freqAges[x] * freqAges[y];
}
if (x > 0.5 * x + 7) {
totalFriendRequest+=freqAges[x]*(freqAges[x]-1);
}
}
return totalFriendRequest;
}
};```

## Complexity Analysis for Friends Of Appropriate Ages Leetcode Solution :

### Time Complexity

`O(n),` as in the first loop we are traveling on the length of the age array, and in the second loop, we are traveling at most 120*120. So, the worst case will be O(n)

### Space Complexity

`O(1)` as we are using only 121 sizes of frequency array.

Translate »