FastPrep4Sum Index Quadruples

4Sum Index Quadruples

Airwallex logoAirwallex● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an integer array nums and an integer target, return every index quadruple [i, j, k, l] such that i < j < k < l and nums[i] + nums[j] + nums[k] + nums[l] == target.

Indices, not values, define a result. Distinct index combinations must therefore be retained even when their values are equal. Do not reorder or mutate nums. Return the quadruples in lexicographic order.

Function

fourSumIndices(nums: int[], target: long) → int[][]

Examples

Example 1

nums = [1,0,-1,0,-2,2]target = 0return = [[0,1,2,3],[0,2,4,5],[1,3,4,5]]

Each listed increasing index tuple selects four values whose sum is zero. No other index tuple qualifies.

Example 2

nums = [2,2,2,2,2]target = 8return = [[0,1,2,3],[0,1,2,4],[0,1,3,4],[0,2,3,4],[1,2,3,4]]

All five ways to choose four different indices are retained even though every selected value is identical.

Constraints

  • 0 <= nums.length <= 400.
  • -10^9 <= nums[i], target <= 10^9.
  • The number of returned quadruples is at most 100000.
  • Use 64-bit arithmetic for intermediate sums.

More Airwallex problems

See Airwallex hiring insights
public int[][] fourSumIndices(int[] nums, long target) {
    // Write your code here.
}
nums[1,0,-1,0,-2,2]
target0
expected[[0,1,2,3],[0,2,4,5],[1,3,4,5]]
Checking account…