Count Triplets With Sum at Most Target
Updated
Problem statement
Given an integer array nums and an integer target, return the number of index triplets (i, j, k) such that:
0 <= i < j < k < nums.length.nums[i] + nums[j] + nums[k] <= target.
Each distinct index combination counts separately, even when multiple indices contain the same value.
Use 64-bit arithmetic for every three-value sum and for the returned count.
Function
countTripletsAtMost(nums: int[], target: long) → longExamples
Example 1
nums = [1,2,3,4,5]target = 8return = 4The valid index triplets select values (1,2,3), (1,2,4), (1,2,5), and (1,3,4). Their sums are 6, 7, 8, and 8.
Example 2
nums = [-2,0,1,3]target = 2return = 3Three triplets have sums at most 2: (-2,0,1), (-2,0,3), and (-2,1,3). The remaining triplet sums to 4.
Example 3
nums = [1,1,1,1]target = 3return = 4Every choice of three distinct indices has sum 3. There are 4 such index combinations.
Constraints
3 <= nums.length <= 2000.-10^9 <= nums[i] <= 10^9.-10^9 <= target <= 10^9.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Increasing-Value Triplets Under a ThresholdIBM · OAIBM interview questions
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- 4SumAmazon · ONSITE INTERVIEW
- Sort ColorsCapital One · ONSITE INTERVIEWCapital One interview questions
- 3SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Reduce Array Size to At Most HalfMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Next and Previous Lexicographic PermutationAdobe · ONSITE INTERVIEWAdobe interview questions