825. Friends of Appropriate Ages
Problem Description
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] <= 0.5 * age[x] + 7
- age[y] > age[x]
- age[y] > 100 && age[x] < 100
Otherwise, x will send a friend request to y.
Return the total number of friend requests made.
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 themselves.
Examples:
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 * 10^4
- 1 ≤ ages[i] ≤ 120
Python Solution
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
def request(x: int, y: int) -> bool:
return not (y <= 0.5 * x + 7 or y > x or (y > 100 and x < 100))
count = Counter(ages)
total = 0
for x in count:
for y in count:
if request(x, y):
if x == y:
total += count[x] * (count[x] - 1)
else:
total += count[x] * count[y]
return total
Implementation Notes:
- Uses Counter to count age frequencies
- Checks friend request conditions for each age pair
- Time complexity: O(n + k²) where k is unique ages
- Space complexity: O(k)
Java Solution
class Solution {
public int numFriendRequests(int[] ages) {
int[] count = new int[121];
for (int age : ages) {
count[age]++;
}
int total = 0;
for (int x = 1; x <= 120; x++) {
for (int y = 1; y <= 120; y++) {
if (x != y && count[x] > 0 && count[y] > 0) {
if (!(y <= 0.5 * x + 7 || y > x || (y > 100 && x < 100))) {
total += count[x] * count[y];
}
} else if (x == y && count[x] > 1) {
if (!(y <= 0.5 * x + 7)) {
total += count[x] * (count[x] - 1);
}
}
}
}
return total;
}
}
C++ Solution
class Solution {
public:
int numFriendRequests(vector& ages) {
vector count(121);
for (int age : ages) {
count[age]++;
}
int total = 0;
for (int x = 1; x <= 120; x++) {
for (int y = 1; y <= 120; y++) {
if (x != y && count[x] > 0 && count[y] > 0) {
if (!(y <= 0.5 * x + 7 || y > x || (y > 100 && x < 100))) {
total += count[x] * count[y];
}
} else if (x == y && count[x] > 1) {
if (!(y <= 0.5 * x + 7)) {
total += count[x] * (count[x] - 1);
}
}
}
}
return total;
}
};
JavaScript Solution
/**
* @param {number[]} ages
* @return {number}
*/
var numFriendRequests = function(ages) {
const count = new Array(121).fill(0);
for (const age of ages) {
count[age]++;
}
let total = 0;
for (let x = 1; x <= 120; x++) {
for (let y = 1; y <= 120; y++) {
if (x !== y && count[x] > 0 && count[y] > 0) {
if (!(y <= 0.5 * x + 7 || y > x || (y > 100 && x < 100))) {
total += count[x] * count[y];
}
} else if (x === y && count[x] > 1) {
if (!(y <= 0.5 * x + 7)) {
total += count[x] * (count[x] - 1);
}
}
}
}
return total;
};
C# Solution
public class Solution {
public int NumFriendRequests(int[] ages) {
var count = new int[121];
foreach (var age in ages) {
count[age]++;
}
int total = 0;
for (int x = 1; x <= 120; x++) {
for (int y = 1; y <= 120; y++) {
if (x != y && count[x] > 0 && count[y] > 0) {
if (!(y <= 0.5 * x + 7 || y > x || (y > 100 && x < 100))) {
total += count[x] * count[y];
}
} else if (x == y && count[x] > 1) {
if (!(y <= 0.5 * x + 7)) {
total += count[x] * (count[x] - 1);
}
}
}
}
return total;
}
}
Implementation Notes:
- Uses array for counting ages
- Optimizes by using age range constraints
- Time complexity: O(n + k²)
- Space complexity: O(k)