Given an array S of n integers, are there elements a, b, c in S such that a + b
ID: 3866574 • Letter: G
Question
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? write a function to Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[ [-1, 0, 1], [-1, -1, 2] ]
return the set for this as an array or arrays
return null for fail cases
keep in mind time comlpexity and explain if using a hashset is in the better interest for time complexity
write written algorithm to how you solved it with words DO THIS PART TOO PLEASE!
type please
comment each line of code
c++
give test cases
Explanation / Answer
C++ code:
#include<bits/stdc++.h>
using namespace std;
// function to print triplets with 0 sum
vector <set < int > > Triplets(int arr[], int n)
{
bool found = false;
vector< set<int> > output;
for (int i=0; i<n-1; i++)
{
// Find all pairs with sum equals to
// "-arr[i]"
vector <int> s;
for (int j=i+1; j<n; j++)
{
int x = -(arr[i] + arr[j]);
if (find(s.begin(),s.end(), x) != s.end())
{
set<int> myset2; myset2.insert(x);myset2.insert(arr[i]);myset2.insert(arr[j]);
bool flag = true;
for (int w = 0; w < output.size(); ++w)
{
set<int> myset1 = output[w];
if(myset2 == myset1) //check duplicates
{
flag = false;
break;
}
}
if(flag == true)
{
output.push_back(myset2);
printf("Possible Triplet %d %d %d ", x, arr[i], arr[j]);
}
found = true;
}
else
s.push_back(arr[j]);
}
}
if (found == false)
{
cout << " No Triplet with sum equal to 0 exist in this array!" << endl;
}
return output;
}
int main()
{
int arr[] = {-1, 0, 1, 2, -1, -4}; //Input array
int n = 6; //size of the array
Triplets(arr, n);
return 0;
}
Sample Output:
Possible Triplet 0 -1 1
Possible Triplet 2 -1 -1
Time complexity: O(n^2)
For each element in the input array x, we are finding a pair of other two elements which have sum equal to -x.So, this is O(n^2).
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.