FastPrepTarget Triplet Sum

Target Triplet Sum

Motive logoMotive● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Given an integer array nums and an integer target, return whether three distinct indices have values whose sum equals target.

Your algorithm may reorder nums, but it must use only constant auxiliary space apart from the sorting implementation's call stack.

Function

hasTargetTriplet(nums: int[], target: int) → boolean

Examples

Example 1

nums = [12,3,4,1,6,9]target = 24return = true

The values 12, 3, and 9 sum to 24.

Example 2

nums = [1,2,4,8]target = 20return = false

No three distinct indices produce 20.

Constraints

  • 0 <= nums.length <= 100000.
  • -10^9 <= nums[i], target <= 10^9.

More Motive problems

See Motive hiring insights
public boolean hasTargetTriplet(int[] nums, int target) {
    // Find three distinct values whose sum is target.
}
nums[12,3,4,1,6,9]
target24
expectedtrue
Checking account…