You are given a list of integers A and an integer N. Implement a function
countTriplets(A, N) that returns the number of unique triplets that can be created from A whose elements sum to ≤ N.
Triplets are order-insensitive (so the order you choose the elements doesn’t matter) and elements may include duplicate values if A has elements with the same value, but they can only choose a given index once.
Note that countTriplets() returns a single integer. You should NOT return the entire list of triplets.
Example:
A = [-4, 2, 2, -7, 5, 3]
N = 4
countTriplets(A, N) == 16
(-4, 3, 5) == (5, 3, -4)
Examples of valid triplets:
(-4, 2, 5) // Sum = -4 + 2 + 5 = 3 <= N
(-4, 2, 2) // Sum = -4 + 2 + 2 = 0 <= N
Examples of invalid triplets:
(2, 5, 3) // Sum = 2 + 5 + 3 = 10 > N
- From array A, count how many unique index-based triplets have a sum ≤ N.
- Triplets are order-insensitive, and duplicates are allowed only if present in A.
- Return only the count, not the triplets.
- Usual approach: sort + 2-loops + binary search (O(n² log n)) or sort + 2-pointer optimization.
The VOprep team has long accompanied candidates through various major company OAs and VOs, including OpenAI, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Stripe or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.