AfterAcademy Tech
•
30 Mar 2020

Difficulty Medium
Companies: Facebook, Amazon, Microsoft
Problem Description: Given an array arr[ ] of n integers, are there elements x, y, z in arr such that x + y + z = 0? Find all unique triplets in the array which gives the sum of zero.
Problem Note
Example 1
Input: A[]= [-1, 0, 1, 2, -1, -4]
Output: [[-1, 0, 1],[-1, -1, 2]]
Example 2
Input: A[] = [1, -2, 1, 0, 5]
Output: [[-2, 1, 1]]
You may try to solve this problem here first.
We will be discussing three different approaches to solve this problem
The naive approach is that we could create three nested loops and check one by one that the sum of all the three elements is zero or not. If the sum of the three elements is zero then print elements.
Solution Step
Pseudo Code
int[][] find_triplets(int arr[], int n){
res[]
sort(arr)
for (int i = 0; i < n – 2; i++) {
for (int j = i + 1; j < n – 1; j++) {
for (int k = j + 1; k < n; k++) {
if (arr[i] + arr[j] + arr[k] == 0) {
res.add([arr[i], arr[j], arr[k]])
}
}
}
}
}
Complexity Analysis
Time Complexity: O(n³)
Space Complexity: O(1)
Critical Ideas to Think
We could use an unordered set data structure to store each value of the array. Set provides the benefit of searching an element in O(1) time. So, for each pair in the array, we will look for the negative of their sum that might exist in the set. If such an element is found then we could print the triplet which will be the pair of integers and the negative value of their sum.
Solution steps
(0-(arr[i] + arr[j])) is present in the set then print arr[i], arr[j] and -(arr[i]+arr[j])int[][] findTriplets(int arr[], int n) {
res[]
for (int i = 0 to n-1) {
// find all pairs with sum equals to -arr[i]
set s
for (int j = i+1 to n) {
x = -(arr[i] + arr[j])
if (x is in s){
temp[] = {x, arr[i], arr[j]}
temp.sort()
res.append(temp)
}
else
s.insert(arr[j])
}
}
return res
}
Complexity Analysis
Time Complexity: O(n²)
Space Complexity: O(n) (How?)
Critical Ideas to Think
i loop?The main idea is to iterate every number in arr.
We use the number as a target to find two other numbers which make total zero.
For those two other numbers, we move pointers, l and r, to try them.l start from left to right.r start from right to left.
First, we sort the array, so we can easily move i around and know how to adjust l and r.
i+1 because the combination of 0~i has already been tried.i after arr[i] > 0, since the sum of 3 positive will be always greater than zero.l and r pointers.Pseudo Code
int[][] findTriplets(int arr[], int length) {
res = []
arr.sort()
for(i = 0 to i < length - 2 ){ // #[8]
if(arr[i] > 0)
break // #[7]
if( i > 0 and arr[i] == arr[i-1])
continue // #[1]
l, r = i+1, length-1 // #[2]
while( l < r ){
total = arr[i]+arr[l]+arr[r]
if(total < 0) // #[3]
l += 1
else if (total>0) // #[4]
r-=1
else: // #[5]
res.append([arr[i], arr[l], arr[r]])
while(l<r and arr[l]==arr[l+1]){ // #[6]
l += 1
}
while(l<r and arr[r]==arr[r-1]){ // #[6]
r -= 1
}
l += 1
r -= 1
}
}
return res
}
Complexity Analysis
Time Complexity: O(n²) (Why?)
Space Complexity: O(1)
Critical Ideas to Think
l and r pointers?
If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given an array of integers arr[] and a target number k, write a program to find all unique combinations in arr[] such that the sum of all integers in the combination is equal to k. This famous backtracking problem has previously been asked in Adobe, Amazon, Facebook.

AfterAcademy Tech
Given a binary tree and a sum, write a program to determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Its an easy problem based on Binary tree traversal and a famous interview question

AfterAcademy Tech
Given an array A[] of n elements filled with several integers, some of them being zeroes, you need to move all the zeroes to the end.

AfterAcademy Tech
You are given an array A[] with n elements. You need to find the maximum sum of a subarray among all subarrays of that array. A subarray of array A[] of length n is a contiguous segment from A[i] through A[j] where 0<= i <= j <= n.
