Skip to content

Instantly share code, notes, and snippets.

@cypher-nullbyte
Last active May 18, 2020 18:16
Show Gist options
  • Save cypher-nullbyte/a0daab2b475c539bac24c0df1dd4b151 to your computer and use it in GitHub Desktop.
Save cypher-nullbyte/a0daab2b475c539bac24c0df1dd4b151 to your computer and use it in GitHub Desktop.
Vpropel VIT | POD | 17/05/2020 | Triplet Sum | 38
Triplet Sum
Given an array of numbers find unique triplets such that the sum of the triplets is zero.
The solution set must not contain duplicate triplets.Find the number of such triplets in the array
Example:-
Given array nums = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
Therefore the required output is 2
Input format:-
Number of elements in an array
Next line –n elements of array separated by a comma
Output format:-
Number of triplets
#include<iostream>
#include<string>
#include<vector>
#include<sstream>
#include<algorithm>
#include<iterator>
void No_triplets(std::vector<int> &v)
{
int n=v.size();
std::vector<std::vector<int>>result;
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(v[i]+v[j]+v[k]==0)
{
std::vector<int> temp={v[i],v[j],v[k]};
std::vector<int>::iterator itr=temp.begin();
//std::sort(itr,itr+sizeof(temp)/sizeof(temp[0])); //##It will get wrong, because sizeof(vector) creates problem -:( remember:)
std::sort(itr,itr+temp.size());
//std::sort(temp.begin(),temp.end());
bool chk=false;
for(std::vector<int> t:result)
if(t==temp)
chk=true;
if(chk==false)
result.push_back(temp);
}
/* # To see our triplets :)
for(auto i:result)
{
for(auto j:i)
cout<<j<<'\t';
cout<<endl;
}
*/
std::cout<<result.size();
}
int main()
{
int n;
std::cin>>n;
std::string st;
std::cin>>st;
std::vector<int> v;
std::stringstream ss(st);
std::cin.ignore();
while(ss.good())
{
std::string substr;
std::getline(ss, substr, ',');
int temp;
std::stringstream ss_temp(substr);
ss_temp>>temp;
v.push_back(temp);
}
/*
for(int &i:v)
cout<<i<<'\t';
*/
No_triplets(v);
return 0;
}
// ### Thank YOu :)
// @cs_jawanda
// .Chiranjeet Singh Jawanda.
// VIT VELLORE
import java.util.Scanner;
import java.util.Arrays;
public class Main
{
public static void NumTriplets(int arr[],int n)
{
int container[][]=new int[30][3]; // assume big size initially
int count=0;
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(arr[i]+arr[j]+arr[k]==0)
{
int temp[]={arr[i],arr[j],arr[k]};
Arrays.sort(temp);
boolean chk=false;
for(int []t:container)
{
if(Arrays.equals(temp,t))
chk=true;
}
if(chk==false)
{
for(int t=0;t<3;t++)
{
container[count][t]=temp[t];
}
count++;
}
}
}
System.out.print(count);
}
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String str=s.next();
String []nums=str.split(",");
int number[]=new int[n];
for(int i=0;i<n;i++)
number[i]=Integer.parseInt(nums[i]);
NumTriplets(number,n);
}
}
// ### Thank YOu :)
// @cs_jawanda
// .Chiranjeet Singh Jawanda.
// VIT VELLORE
n=int(input())
l=[]
m=list()
l=input().split(",")
l=[int(x) for x in l]
for i in range(n-2):
for j in range(i+1,n-1):
for k in range(j+1,n):
if l[i]+l[j]+l[k]==0:
temp=[l[i],l[j],l[k]]
temp.sort()
if temp not in m:
m.append(temp)
print(len(m))
'''
// ### Thank YOu :)
// @cs_jawanda
// .Chiranjeet Singh Jawanda.
// VIT VELLORE
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment