825. Friends Of Appropriate Ages LeetCode Solution
In this guide, you will get 825. Friends Of Appropriate Ages LeetCode Solution with the best time and space complexity. The solution to Friends Of Appropriate Ages problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Friends Of Appropriate Ages solution in C++
- Friends Of Appropriate Ages solution in Java
- Friends Of Appropriate Ages solution in Python
- Additional Resources
Problem Statement of Friends Of Appropriate Ages
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] 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
Complexity Analysis
- Time Complexity: O(n^2)
- Space Complexity: O(n)
825. Friends Of Appropriate Ages LeetCode Solution in C++
class Solution {
public:
int numFriendRequests(vector<int>& ages) {
int ans = 0;
unordered_map<int, int> count;
for (const int age : ages)
++count[age];
for (const auto& [ageA, countA] : count)
for (const auto& [ageB, countB] : count)
if (request(ageA, ageB))
if (ageA == ageB)
ans += countA * (countB - 1);
else
ans += countA * countB;
return ans;
}
private:
bool request(int ageA, int ageB) {
return !(ageB <= 0.5 * ageA + 7 || ageB > ageA || ageB > 100 && ageA < 100);
}
};
/* code provided by PROGIEZ */
825. Friends Of Appropriate Ages LeetCode Solution in Java
class Solution {
public int numFriendRequests(int[] ages) {
int ans = 0;
int[] count = new int[121];
for (final int age : ages)
++count[age];
for (int ageA = 1; ageA <= 120; ++ageA)
for (int ageB = 1; ageB <= 120; ++ageB) {
final int countA = count[ageA];
final int countB = count[ageB];
if (countA > 0 && countB > 0 && request(ageA, ageB))
if (ageA == ageB)
ans += countA * (countB - 1);
else
ans += countA * countB;
}
return ans;
}
private boolean request(int ageA, int ageB) {
return !(ageB <= 0.5 * ageA + 7 || ageB > ageA || ageB > 100 && ageA < 100);
}
}
// code provided by PROGIEZ
825. Friends Of Appropriate Ages LeetCode Solution in Python
class Solution:
def numFriendRequests(self, ages: list[int]) -> int:
ans = 0
count = [0] * 121
for age in ages:
count[age] += 1
for i in range(15, 121):
ans += count[i] * (count[i] - 1)
for i in range(15, 121):
for j in range(i // 2 + 8, i):
ans += count[i] * count[j]
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.