LeetCode - Friends Of Appropriate Ages Solution - The Coding Shala
Home >> LeetCode >> Friend of Appropriate Ages
In this post, we will see how to solve LeetCode's Friends Of Appropriate Ages Problem and its solution in Java.
Friends Of Appropriate Ages
Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ith person.
Person A will NOT friend request person B (B != A) if any of the following conditions are true:
age[B] <= 0.5 * age[A] + 7
age[B] > age[A]
age[B] > 100 && age[A] < 100
Otherwise, A will friend request B.
Note that if A requests B, B does not necessarily request A. Also, people will not friend request themselves.
How many total friend requests are made?
Explanation: 2 people friend request each other.
Explanation: Friend requests are made 17 -> 16, 18 -> 17.
Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
Friends Of Appropriate Ages Java Solution
We can use binary search here. If person B's range comes in 0.5 * age[A] + 7 then person A can sent friend request. Other two conditions became true if we sort the array.
Other Posts You May Like
- LeetCode - Kids with greatest number of candies
- LeetCode - Next greater Element 1
- LeetCode - Running Sum of 1d array
- LeetCode - Shuffle the Array
- LeetCode - Number of good pairs