Friends Of Appropriate Ages
Real-world version: How Many Invitations Go OutThe statement here is our own: the same inputs, outputs and technique.
A club's members have the ages ages. A member x sends an invitation to a different member y exactly when both of these hold:
yis no older thanx;y's age is more than half ofx's age plus seven.
Invitations go one way only, so x inviting y and y inviting x are counted separately, and nobody invites themselves.
Return how many invitations go out altogether.
Examples
Example 1
- Input
- ages = [15, 15]
- Output
- 2
Neither member is older than the other, and fifteen is more than half of fifteen plus seven, which is fourteen and a half, so both invitations go out.
Example 2
- Input
- ages = [100, 100, 100]
- Output
- 6
A hundred is more than half of a hundred plus seven, so every member invites every other: three members with two others apiece.
Example 3
- Input
- ages = [1]
- Output
- 0
A single member has nobody else to invite, and nobody invites themselves.
Constraints
- 1 <= ages.length <= 2 * 10^4
- 1 <= ages[i] <= 120
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def num_friend_requests(ages: list[int]) -> int:public int numFriendRequests(int[] ages)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Intersection of Two ArraysReal-world version: Shared Scan CodesEasy
- Earliest Finish Time for Land and Water Rides IReal-world version: Forge and Kiln BookingEasy
- Intersection of Two Arrays IIReal-world version: Shared Spare PartsEasy
- Check If N and Its Double ExistReal-world version: Twice Some Other EntryEasy