-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_Triplets_With_0_Sum.cpp
91 lines (86 loc) · 2.24 KB
/
Find_Triplets_With_0_Sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//{ Driver Code Starts
#include<bits/stdc++.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
// } Driver Code Ends
/* You are required to complete the function below
* arr[]: input array
* n: size of array
*/
class Solution{
public:
//Function to find triplets with zero sum.
bool findTriplets(int arr[], int n)
{
sort(arr, arr+n);
int j, k;
for(int i = 0; i < n; i++)
{
j = i+1;
k = n-1;
while(j < k)
{
if(arr[i] + arr[k] + arr[j] == 0)
return true;
else if(arr[i] + arr[j] + arr[k] > 0)
k--;
else j++;
}
}
return false;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
int arr[n]={0};
for(int i=0;i<n;i++)
cin>>arr[i];
Solution obj;
if(obj.findTriplets(arr, n))
cout<<"1"<<endl;
else
cout<<"0"<<endl;
}
return 0;
}
// } Driver Code Ends
// Leet Code Similiar Question :- 3 sum
// class Solution {
// public:
// vector<vector<int>> threeSum(vector<int>& nums) {
// vector<vector<int>> ans;
// int n = nums.size();
// sort(nums.begin(), nums.end());
// for (int i = 0; i < n; i++) {
// if (i > 0 && nums[i] == nums[i - 1])
// continue;
// int j = i + 1;
// int k = n - 1;
// while (j < k) {
// int sum = nums[i] + nums[j] + nums[k];
// if (sum < 0)
// j++;
// else if (sum > 0)
// k--;
// else {
// vector<int> temp = {nums[i], nums[j], nums[k]};
// ans.push_back(temp);
// j++;
// k--;
// while (j < k && nums[j] == nums[j - 1])
// j++;
// while (j < k && nums[k] == nums[k + 1])
// k--;
// }
// }
// }
// return ans;
// }
// };