Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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] <= 120Problem summary: 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. 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Binary Search
[16,16]
[16,17,18]
[20,30,100,110,120]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #825: Friends Of Appropriate Ages
class Solution {
public int numFriendRequests(int[] ages) {
final int m = 121;
int[] cnt = new int[m];
for (int x : ages) {
++cnt[x];
}
int ans = 0;
for (int ax = 1; ax < m; ++ax) {
for (int ay = 1; ay < m; ++ay) {
if (!(ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100))) {
ans += cnt[ax] * (cnt[ay] - (ax == ay ? 1 : 0));
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #825: Friends Of Appropriate Ages
func numFriendRequests(ages []int) (ans int) {
cnt := [121]int{}
for _, x := range ages {
cnt[x]++
}
for ax, x := range cnt {
for ay, y := range cnt {
if ay <= ax/2+7 || ay > ax || (ay > 100 && ax < 100) {
continue
}
if ax == ay {
ans += x * (x - 1)
} else {
ans += x * y
}
}
}
return
}
# Accepted solution for LeetCode #825: Friends Of Appropriate Ages
class Solution:
def numFriendRequests(self, ages: List[int]) -> int:
cnt = [0] * 121
for x in ages:
cnt[x] += 1
ans = 0
for ax, x in enumerate(cnt):
for ay, y in enumerate(cnt):
if not (ay <= 0.5 * ax + 7 or ay > ax or (ay > 100 and ax < 100)):
ans += x * (y - int(ax == ay))
return ans
// Accepted solution for LeetCode #825: Friends Of Appropriate Ages
struct Solution;
use std::collections::HashMap;
impl Solution {
fn num_friend_requests(ages: Vec<i32>) -> i32 {
let mut hm: HashMap<i32, i32> = HashMap::new();
for age in ages {
*hm.entry(age).or_default() += 1;
}
let mut res = 0;
for (&a, v) in &hm {
for (&b, u) in &hm {
if !(b > a || 2 * b <= a + 14) {
res += v * u;
if a == b {
res -= v;
}
}
}
}
res
}
}
#[test]
fn test() {
let ages = vec![16, 16];
let res = 2;
assert_eq!(Solution::num_friend_requests(ages), res);
let ages = vec![16, 17, 18];
let res = 2;
assert_eq!(Solution::num_friend_requests(ages), res);
let ages = vec![20, 30, 100, 110, 120];
let res = 3;
assert_eq!(Solution::num_friend_requests(ages), res);
let ages = vec![73, 106, 39, 6, 26, 15, 30, 100, 71, 35, 46, 112, 6, 60, 110];
let res = 29;
assert_eq!(Solution::num_friend_requests(ages), res);
}
// Accepted solution for LeetCode #825: Friends Of Appropriate Ages
function numFriendRequests(ages: number[]): number {
const m = 121;
const cnt = Array(m).fill(0);
for (const x of ages) {
cnt[x]++;
}
let ans = 0;
for (let ax = 0; ax < m; ax++) {
for (let ay = 0; ay < m; ay++) {
if (ay <= 0.5 * ax + 7 || ay > ax || (ay > 100 && ax < 100)) {
continue;
}
ans += cnt[ax] * (cnt[ay] - (ax === ay ? 1 : 0));
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Setting `lo = mid` or `hi = mid` can stall and create an infinite loop.
Usually fails on: Two-element ranges never converge.
Fix: Use `lo = mid + 1` or `hi = mid - 1` where appropriate.