Coding problem
Three adjustments that cancel out
The task
An accounting tool flags groups of three ledger adjustments that cancel each other out, because they often mean one entry was booked and reversed.
Complete ZeroTriples(int[] values). Return every triple of values, taken from three different positions, whose sum is 0. Two triples with the same three values are the same triple: return it once. Sort each triple in ascending order, and sort the list of triples in ascending order (compare the first values, then the second, then the third). The sum is the true sum: adding three large values must not wrap around. Do not change the array you are given. The array is never null.
Signature
public static List<int[]> ZeroTriples(int[] values)
Examples
[3, -1, -2, -1, 2, 0]→[[-2, -1, 3], [-2, 0, 2], [-1, -1, 2]]. The two -1 values are at different positions, so [-1, -1, 2] counts.[0, 0, 0, 0, 0]→[[0, 0, 0]]. Many sets of positions give the same three values, so the triple appears once.
Limits: Up to 8,000 values; a value can be any int.
The tests check: an empty array, too few values, no triple, a value used as often as it appears, the order of the triples, large values that would wrap in int, that your method leaves the array unchanged, and 8,000 values in time.
Hints
Your code
Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;
public static class Solution
{
// Every distinct triple of values from three different positions whose sum is 0, each sorted, the list sorted.
public static List<int[]> ZeroTriples(int[] values)
{
throw new NotImplementedException();
}
}This problem comes with TechInterviewNotes Full Access
Unlock all paid content on TechInterviewNotes.