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] + 7age[y] > age[x]age[y] > 100 && age[x] < 100Otherwise, 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.length1 <= n <= 2 * 1041 <= ages[i] <= 120When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force strategy involves checking every single possible pairing of people to see if a friend request would be sent. We consider each person individually and then, for each person, check all other people to determine if a request would be sent according to the age rules.
Here's how the algorithm would work step-by-step:
def friends_of_appropriate_ages(ages):
total_friend_requests = 0
number_of_people = len(ages)
for age_of_person_a in ages:
for age_of_person_b in ages:
# Do not allow someone to friend themselves
if age_of_person_a == age_of_person_b:
continue
# Rule: age[B] <= 0.5 * age[A] + 7
if age_of_person_b <= 0.5 * age_of_person_a + 7:
continue
# Rule: age[B] > age[A]
if age_of_person_b > age_of_person_a:
continue
# Rule: age[B] > 100 and age[A] < 100
if age_of_person_b > 100 and age_of_person_a < 100:
continue
# If all conditions are met, increment the friend request count
total_friend_requests += 1
return total_friend_requestsThe key is to count how many people there are of each age. Then, for each age, figure out how many people of that age can send friend requests to others, including themselves, based on the age rules. This avoids checking every possible pair of people.
Here's how the algorithm would work step-by-step:
def friends_of_appropriate_ages(ages):
age_counts = [0] * 121
for age in ages:
age_counts[age] += 1
total_requests = 0
for age_a in range(1, 121):
count_a = age_counts[age_a]
if count_a == 0:
continue
# Iterate through all possible ages to consider
for age_b in range(1, 121):
count_b = age_counts[age_b]
if count_b == 0:
continue
# Rule condition check
if age_b <= 0.5 * age_a + 7:
continue
if age_b > age_a:
continue
if age_b > 100 and age_a < 100:
continue
# Accumulate number of requests
if age_a == age_b:
total_requests += count_a * (count_b - 1)
# Avoiding double counting if same person
else:
total_requests += count_a * count_b
return total_requests| Case | How to Handle |
|---|---|
| Empty or null ages array | Return 0 immediately, as no requests can be made. |
| Ages array with only one element | Return 0 immediately since a single person cannot send a request to themselves. |
| All ages are identical | The count of requests will be n * (n - 1) if all people are of valid age to send requests to each other. |
| Ages array with very large numbers | Ensure that calculations, particularly comparisons, do not overflow integer limits by using 32 or 64 bit integers |
| Ages array is sorted in ascending order | The algorithm should still correctly compute the number of valid friend requests despite the order. |
| Ages array is sorted in descending order | The algorithm should still correctly compute the number of valid friend requests despite the order. |
| Ages array contains ages near the lower boundary (e.g., 14, 15) | Ensure the age check 'age[y] <= 0.5 * age[x] + 7' correctly handles ages at the lower limit |
| Maximum array size for a large number of ages | Consider using a counting sort approach if the range of ages is relatively small compared to the number of ages, to improve performance to linear time. |