|
| 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 Statement |
| 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 | + |
| 59 | +- $1 ≤ N ≤ 10^5$ |
| 60 | +- $-10^3 ≤ arr[i] ≤ 10^3$ |
| 61 | + |
| 62 | +## Solution |
| 63 | + |
| 64 | +### Approach |
| 65 | + |
| 66 | +We can solve this problem using sorting and the two-pointer technique. Here's a step-by-step approach: |
| 67 | + |
| 68 | +1. Sort the array. |
| 69 | +2. Iterate through the array using a for loop, fixing one element at a time. |
| 70 | +3. Use two pointers to find the other two elements that sum up to zero with the fixed element: |
| 71 | + - One pointer starts from the next element after the fixed element. |
| 72 | + - The other pointer starts from the end of the array. |
| 73 | +4. Adjust the pointers based on the sum of the three elements: |
| 74 | + - If the sum is zero, return true. |
| 75 | + - If the sum is less than zero, move the left pointer to the right. |
| 76 | + - If the sum is greater than zero, move the right pointer to the left. |
| 77 | +5. If no triplet is found, return false. |
| 78 | + |
| 79 | +### Implementation |
| 80 | + |
| 81 | +<Tabs> |
| 82 | + <TabItem value="cpp" label="C++"> |
| 83 | + |
| 84 | +```cpp |
| 85 | +class Solution { |
| 86 | +public: |
| 87 | + bool findTriplet(int arr[], int n) { |
| 88 | + sort(arr, arr + n); |
| 89 | + for (int i = 0; i < n - 2; i++) { |
| 90 | + int left = i + 1, right = n - 1; |
| 91 | + while (left < right) { |
| 92 | + int sum = arr[i] + arr[left] + arr[right]; |
| 93 | + if (sum == 0) return true; |
| 94 | + if (sum < 0) left++; |
| 95 | + else right--; |
| 96 | + } |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | +}; |
| 101 | +``` |
| 102 | +
|
| 103 | + </TabItem> |
| 104 | + <TabItem value="javascript" label="JavaScript"> |
| 105 | +
|
| 106 | +```javascript |
| 107 | +function findTriplet(arr) { |
| 108 | + arr.sort((a, b) => a - b); |
| 109 | + for (let i = 0; i < arr.length - 2; i++) { |
| 110 | + let left = i + 1, right = arr.length - 1; |
| 111 | + while (left < right) { |
| 112 | + const sum = arr[i] + arr[left] + arr[right]; |
| 113 | + if (sum === 0) return true; |
| 114 | + if (sum < 0) left++; |
| 115 | + else right--; |
| 116 | + } |
| 117 | + } |
| 118 | + return false; |
| 119 | +} |
| 120 | +``` |
| 121 | + |
| 122 | + </TabItem> |
| 123 | + <TabItem value="typescript" label="TypeScript"> |
| 124 | + |
| 125 | +```typescript |
| 126 | +function findTriplet(arr: number[]): boolean { |
| 127 | + arr.sort((a, b) => a - b); |
| 128 | + for (let i = 0; i < arr.length - 2; i++) { |
| 129 | + let left = i + 1, right = arr.length - 1; |
| 130 | + while (left < right) { |
| 131 | + const sum = arr[i] + arr[left] + arr[right]; |
| 132 | + if (sum === 0) return true; |
| 133 | + if (sum < 0) left++; |
| 134 | + else right--; |
| 135 | + } |
| 136 | + } |
| 137 | + return false; |
| 138 | +} |
| 139 | +``` |
| 140 | + |
| 141 | + </TabItem> |
| 142 | + <TabItem value="python" label="Python"> |
| 143 | + |
| 144 | +```python |
| 145 | +class Solution: |
| 146 | + def findTriplet(self, arr: List[int], n: int) -> bool: |
| 147 | + arr.sort() |
| 148 | + for i in range(n - 2): |
| 149 | + left, right = i + 1, n - 1 |
| 150 | + while left < right: |
| 151 | + total = arr[i] + arr[left] + arr[right] |
| 152 | + if total == 0: |
| 153 | + return True |
| 154 | + if total < 0: |
| 155 | + left += 1 |
| 156 | + else: |
| 157 | + right -= 1 |
| 158 | + return False |
| 159 | +``` |
| 160 | + |
| 161 | + </TabItem> |
| 162 | + <TabItem value="java" label="Java"> |
| 163 | + |
| 164 | +```java |
| 165 | +class Solution { |
| 166 | + public boolean findTriplet(int[] arr, int n) { |
| 167 | + Arrays.sort(arr); |
| 168 | + for (int i = 0; i < n - 2; i++) { |
| 169 | + int left = i + 1, right = n - 1; |
| 170 | + while (left < right) { |
| 171 | + int sum = arr[i] + arr[left] + arr[right]; |
| 172 | + if (sum == 0) return true; |
| 173 | + if (sum < 0) left++; |
| 174 | + else right--; |
| 175 | + } |
| 176 | + } |
| 177 | + return false; |
| 178 | + } |
| 179 | +} |
| 180 | +``` |
| 181 | + |
| 182 | + </TabItem> |
| 183 | +</Tabs> |
| 184 | + |
| 185 | +### Complexity Analysis |
| 186 | + |
| 187 | +- **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. |
| 188 | +- **Space Complexity:** $O(1)$, as we only use a constant amount of extra space for variables. |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## References |
| 193 | + |
| 194 | +- **GeeksforGeeks Problem:** [Find Triplet with Zero Sum](https://www.geeksforgeeks.org/find-triplet-sum-zero/) |
| 195 | +- **Authors GeeksforGeeks Profile:** [Vipul lakum](https://www.geeksforgeeks.org/user/lakumvipwjge/) |
0 commit comments