Skip to content

Commit 48eced0

Browse files
committed
Find Triplet with zero sum solution added
1 parent 5043238 commit 48eced0

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
---
2+
id: Find-triplet-with-zero-sum
3+
title: Find Triplet with Zero Sum
4+
sidebar_label: 0023 Find Triplet with Zero Sum
5+
tags:
6+
- Array
7+
- Sorting
8+
- Two Pointers
9+
- JavaScript
10+
- TypeScript
11+
- Python
12+
- Java
13+
- C++
14+
description: "This document explores different approaches to solving the problem of finding a triplet in an array that sums up to zero, including solutions in JavaScript, TypeScript, Python, Java, and C++."
15+
---
16+
17+
## Problem
18+
19+
Given an array `arr[]` of distinct integers of size N, the task is to find if there are any three elements in `arr[]` whose sum is equal to zero.
20+
21+
### Examples
22+
23+
**Example 1:**
24+
25+
```
26+
Input:
27+
N = 5
28+
arr[] = {-1, 0, 1, 2, -1, -4}
29+
30+
Output:
31+
True
32+
33+
Explanation:
34+
The triplets {-1, 0, 1} and {-1, -1, 2} both have a sum of zero.
35+
```
36+
37+
**Example 2:**
38+
39+
```
40+
Input:
41+
N = 3
42+
arr[] = {1, 2, 3}
43+
44+
Output:
45+
False
46+
47+
Explanation:
48+
No triplet has a sum of zero.
49+
```
50+
51+
### Your Task
52+
You don't need to read input or print anything. Your task is to complete the function `findTriplet()` which takes the integer `N` and integer array `arr[]` as parameters and returns true if there is a triplet with a sum of zero, otherwise false.
53+
54+
**Expected Time Complexity:** O(N^2)
55+
**Expected Auxiliary Space:** O(1)
56+
57+
### Constraints
58+
- 1 ≤ N ≤ 10^5
59+
- -10^3 ≤ arr[i] ≤ 10^3
60+
61+
## Solution
62+
63+
### Approach
64+
65+
We can solve this problem using sorting and the two-pointer technique. Here's a step-by-step approach:
66+
67+
1. Sort the array.
68+
2. Iterate through the array using a for loop, fixing one element at a time.
69+
3. Use two pointers to find the other two elements that sum up to zero with the fixed element:
70+
- One pointer starts from the next element after the fixed element.
71+
- The other pointer starts from the end of the array.
72+
4. Adjust the pointers based on the sum of the three elements:
73+
- If the sum is zero, return true.
74+
- If the sum is less than zero, move the left pointer to the right.
75+
- If the sum is greater than zero, move the right pointer to the left.
76+
5. If no triplet is found, return false.
77+
78+
### Implementation
79+
80+
<Tabs>
81+
<TabItem value="cpp" label="C++">
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
bool findTriplet(int arr[], int n) {
87+
sort(arr, arr + n);
88+
for (int i = 0; i < n - 2; i++) {
89+
int left = i + 1, right = n - 1;
90+
while (left < right) {
91+
int sum = arr[i] + arr[left] + arr[right];
92+
if (sum == 0) return true;
93+
if (sum < 0) left++;
94+
else right--;
95+
}
96+
}
97+
return false;
98+
}
99+
};
100+
```
101+
102+
</TabItem>
103+
<TabItem value="javascript" label="JavaScript">
104+
105+
```javascript
106+
function findTriplet(arr) {
107+
arr.sort((a, b) => a - b);
108+
for (let i = 0; i < arr.length - 2; i++) {
109+
let left = i + 1, right = arr.length - 1;
110+
while (left < right) {
111+
const sum = arr[i] + arr[left] + arr[right];
112+
if (sum === 0) return true;
113+
if (sum < 0) left++;
114+
else right--;
115+
}
116+
}
117+
return false;
118+
}
119+
```
120+
121+
</TabItem>
122+
<TabItem value="typescript" label="TypeScript">
123+
124+
```typescript
125+
function findTriplet(arr: number[]): boolean {
126+
arr.sort((a, b) => a - b);
127+
for (let i = 0; i < arr.length - 2; i++) {
128+
let left = i + 1, right = arr.length - 1;
129+
while (left < right) {
130+
const sum = arr[i] + arr[left] + arr[right];
131+
if (sum === 0) return true;
132+
if (sum < 0) left++;
133+
else right--;
134+
}
135+
}
136+
return false;
137+
}
138+
```
139+
140+
</TabItem>
141+
<TabItem value="python" label="Python">
142+
143+
```python
144+
class Solution:
145+
def findTriplet(self, arr: List[int], n: int) -> bool:
146+
arr.sort()
147+
for i in range(n - 2):
148+
left, right = i + 1, n - 1
149+
while left < right:
150+
total = arr[i] + arr[left] + arr[right]
151+
if total == 0:
152+
return True
153+
if total < 0:
154+
left += 1
155+
else:
156+
right -= 1
157+
return False
158+
```
159+
160+
</TabItem>
161+
<TabItem value="java" label="Java">
162+
163+
```java
164+
class Solution {
165+
public boolean findTriplet(int[] arr, int n) {
166+
Arrays.sort(arr);
167+
for (int i = 0; i < n - 2; i++) {
168+
int left = i + 1, right = n - 1;
169+
while (left < right) {
170+
int sum = arr[i] + arr[left] + arr[right];
171+
if (sum == 0) return true;
172+
if (sum < 0) left++;
173+
else right--;
174+
}
175+
}
176+
return false;
177+
}
178+
}
179+
```
180+
181+
</TabItem>
182+
</Tabs>
183+
184+
### Complexity Analysis
185+
186+
- **Time Complexity:** $O(N^2)$, where N is the length of the array. We iterate through the array and for each element, we use the two-pointer technique which takes linear time.
187+
- **Space Complexity:** $O(1)$, as we only use a constant amount of extra space for variables.
188+
189+
---
190+
191+
## References
192+
193+
- **GeeksforGeeks Problem:** [Find Triplet with Zero Sum](https://www.geeksforgeeks.org/find-triplet-sum-zero/)
194+
- **Author GeeksforGeeks Profile:** [GeeksforGeeks](https://www.geeksforgeeks.org/user/GeeksforGeeks/)

0 commit comments

Comments
 (0)