πDay 3. Find All Triplets with Zero Sum π§
The problem can be found at the following link: Problem Link
π‘ Problem Description:
Given an array arr[] of size n, the task is to find all possible triplets i, j, k such that arr[i] + arr[j] + arr[k] = 0, and the triplets should be returned in a sorted form, i.e., i < j < k.
π Example Walkthrough:
Input:
arr[] = [0, -1, 2, -3, 1]
Output:
[[0, 1, 4], [2, 3, 4]]
Explanation:
The triplets with a sum of zero are:
arr[0] + arr[1] + arr[4] = 0 + (-1) + 1 = 0arr[2] + arr[3] + arr[4] = 2 + (-3) + 1 = 0
Input:
arr[] = [1, -2, 1, 0, 5]
Output:
[[0, 1, 2]]
Explanation:
Only one triplet satisfies the condition: arr[0] + arr[1] + arr[2] = 1 + (-2) + 1 = 0
Input:
arr[] = [2, 3, 1, 0, 5]
Output:
[]
Explanation:
No triplet with sum 0.
Constraints:
$
3 <= arr.size() <= 10^3$$
-10^4 <= arr[i] <= 10^4$
π― My Approach:
Optimized Approach using Hash Map:
We use a hash map to store the sum of pairs of elements in the array.
For each element
arr[i], check if the negative of that element exists as a sum of some pair(arr[j] + arr[k]). If so, it's a valid triplet.Ensure that no index is repeated by sorting and using a set to store the triplets in a sorted manner.
Steps:
Traverse the array and for each element
arr[i], calculate the pair sumtarget = -arr[i].Use a hash map to find if a pair
(arr[j] + arr[k])exists wherej != iandk != i.Add the triplet
(i, j, k)into the result after ensuring it's sorted.
π Time and Auxiliary Space Complexity
Expected Time Complexity: $O(n^2)$, where
nis the size of the array. This is because for each element, we check pairs of the remaining elements.Expected Auxiliary Space Complexity: $O(n^2)$, where
nis the size of the array. We use additional space to store the hash map and results.
π Solution Code
Code (C++)
Code (Java)
Code (Python)
π― Contribution and Support:
For discussions, questions, or doubts related to this solution, please visit my LinkedIn: Any Questions. Thank you for your input; together, we strive to create a space where learning is a collaborative endeavor.
β Star this repository if you find it helpful or intriguing! β
πVisitor Count
Last updated