3 Sum – Find All Triplets with Zero Sum
Anonymous
Input: arr[] = {0, -1, 2, -3, 1} Output: {{0, 1, 4}, {2, 3, 4}} Explanation: Two triplets with sum 0 are: arr[0] + arr[1] + arr[4] = 0 + (-1) + 1 = 0 arr[2] + arr[3] + arr[4] = 2 + (-3) + 1 = 0 Input: arr[] = {1, -2, 1, 0, 5} Output: {{0, 1, 2}} Explanation: Only triplet which satisfies the condition is arr[0] + arr[1] + arr[2] = 1 + (-2) + 1 = 0 Input: arr[] = {2, 3, 1, 0, 5} Output: {{}} Explanation: There is no triplet with sum 0
Check out your Company Bowl for anonymous work chats.