C Program
#include<stdio.h> #include<stdbool.h> int main() { int a[] = {-1, 0, 1, 2, -1, -4}, n = 6; 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(a[i]+a[j]+a[k]==0) printf("%d %d %d\n", a[i], a[j], a[k]); }
C Output
-1 0 1 -1 -1 2
C++ Program
#include<bits/stdc++.h> using namespace std; int main() { vector<int> a = {-1, 0, 1, 2, -1, -4}; sort(a.begin(), a.end()); int n = a.size(); for(int i=0; i<n-2; i++) { if(i>0 && a[i]==a[i-1]) continue; int l=i+1, r=n-1; while(l<r) { int s = a[i]+a[l]+a[r]; if(s==0) { cout << a[i] << " " << a[l] << " " << a[r] << "\n"; while(l<r && a[l]==a[l+1]) l++; while(l<r && a[r]==a[r-1]) r--; l++, r--; } else if(s<0) l++; else r--; } } }
C++ Output
-1 -1 2 -1 0 1
JAVA Program
import java.util.*; class ThreeSum { public static void main(String[] args) { int[] a = {-1, 0, 1, 2, -1, -4}; Arrays.sort(a); int n = a.length; for(int i=0; i<n-2; i++) { if(i>0 && a[i]==a[i-1]) continue; int l=i+1, r=n-1; while(l<r) { int s = a[i]+a[l]+a[r]; if(s==0) { System.out.println(a[i]+" "+a[l]+" "+a[r]); while(l<r && a[l]==a[l+1]) l++; while(l<r && a[r]==a[r-1]) r--; l++; r--; } else if(s<0) l++; else r--; } } } }
JAVA Output
-1 -1 2 -1 0 1
Python Program
a = [-1, 0, 1, 2, -1, -4] a.sort() n = len(a) for i in range(n-2): if i > 0 and a[i] == a[i-1]: continue l, r = i+1, n-1 while l < r: s = a[i] + a[l] + a[r] if s == 0: print(a[i], a[l], a[r]) while l < r and a[l] == a[l+1]: l += 1 while l < r and a[r] == a[r-1]: r -= 1 l += 1; r -= 1 elif s < 0: l += 1 else: r -= 1
Python Output
-1 -1 2 -1 0 1
In-Depth Learning – Complete Concept in Paragraphs
What Is the Triplet Sum Equals Zero Problem?
You're required to find all triplets in an array whose sum is zero. It's a standard extension of the 3Sum problem, which demands not only brute-force logic but also optimized search to satisfy time requirements, particularly when array sizes become large.
How the Code Works
Brute-force Approach (C code):
Scan all triplets of three elements through 3 nested loops.
Time Complexity: O(n³) → inefficient for big inputs.
Improved Solution (Python, Java, C++):
Sort the array.
Iterate through every number (as the first number of the triplet).
Implement two pointers (left, right) to locate two more numbers so that the sum becomes zero.
Skip duplicates to prevent duplicate triplets.
This two-pointer technique reduces the time complexity to O(n²), which is efficient and commonly used during interviews.
Example
Input Array:
[-1, 0, 1, 2, -1, -4]
Sorted:
[-4, -1, -1, 0, 1, 2]
Triplets:
-1 + 0 + 1 = 0
-1 + -1 + 2 = 0
Answer:
[-1, -1, 2]
[-1, 0, 1]
Real-Life Analogy
Consider this as a set of friends splitting the bill. If three individuals' contributions sum to zero, then the group is even in terms of money. In data science, you could apply similar reasoning to locate balancing data points that neutralize each other — such as profit/loss, supply/demand, or even forces in a physics simulation.
Where and When Is It Used?
This algorithm comes in handy in:
Interview preparation (particularly FAANG companies)
Subarray and set-based problems
Triplet finding in data mining
Target-sum problems
Financial calculations (such as finding neutralizing accounts)
It's a common pattern in solving numerous sum combination or target sum problems.
Time and Space Complexity
Operation\tComplexity
Time (Optimized)\tO(n²)
Space\tO(1) (in-place) or O(k) for result list
Brute-force has O(n³), which is unfeasible for n > 1000.
Python-Specific Tip
Utilize set or collections.defaultdict to track the duplicates or store combinations if you have to return results instead of printing.
You can also return list of lists:
python
res = []
res.append([a[i], a[l], a[r]])
SEO-Optimized Natural Paragraph for Ranking
Need to find Triplet Sum Equals Zero or 3Sum problem solution in C, C++, Java, or Python? This tutorial provides you with the most direct and effective method by using brute-force and optimized sorting + two-pointer methods. The task is to identify all distinct triplets in an array that add up to zero, which is a basic exercise in programming logic, interview practice, and algorithmic design. Discover how to work with duplicates, iterate efficiently, and apply this popularly-posed coding exercise in several languages with concise and clear instructions.
Social Plugin