Skip to the code
cf.
+25 XP

#115 Zero-Sum Discount Triples

medium

A pricing tool lists price adjustments in deltas: negative numbers are discounts and positive numbers are surcharges. An auditor wants every set of three adjustments that cancel out exactly.

Write zeroSumTriples(deltas) that returns every distinct triple of values [x, y, z], taken from three different positions, with x + y + z === 0. Write each triple in ascending order, list the triples in ascending order (compare first values, then second), and list each distinct triple once.

Example 1

Input: deltas = [-3,1,2,-1,-1,4]
Output: [[-3,-1,4],[-3,1,2],[-1,-1,2]]

Both -1s are used in [-1, -1, 2]; [-3, -1, 4] uses either -1, but it's listed once.

Example 2

Input: deltas = [1,2,-2,-1]
Output: []

No three values add up to zero.

Constraints

  • 0 <= deltas.length <= 3000
  • -10^5 <= deltas[i] <= 10^5
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).