how to write function condition properly so that it is equal to target
Target sum triplets
@kumarakash121005 you should store the sum of three numbers in a variable and then compare the variable value to that of the target if this value matches with the target value then we found a triplet that sums to target.
brute force implementation of above approach looks like
void findTriplets(int arr[], int n, int target)
{
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++) {
int tempSum = arr[i] + arr[j] + arr[k]; // tempSum variable stores the sum of
//the triplet
if (tempSum == target) { // check if target value equals sum of triplet
cout << arr[i] << " "<< arr[j] << " "<< arr[k] << endl;
}
}
}
}
}
This approach takes O(N^3) time.
However, there are other efficient solutions as well.
#include
using namespace std;
int pairsum(int a[],int n, int k,int &i,int &j ){
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
int i=0;
int j=n-1;
while(i<j){
if(a[i]+a[j]==k){
return a[i]+a[j];
i++;
j--;
}
else if(a[i]+a[j]<k){
i++;
}
else if(a[i]+a[j]>k){
j--;
}
}
}
int main() {
int n,x;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cin>>x;
int i=0;
int j=n-1;
for(int k=1;k<=n;k++){
if((pairsum(a,n,k,i,j)+x-k)==x){
cout<<a[i]<<" "<<a[j]<<" "<<"and"<<(x-k)<<endl;
}
}
return 0;
}
what is wrong in my code
- in pairsum function you are again taking array βaβ as input which is not required as you have already passed array a to pairsum function.
- pairsum function is not returning anything but return type is set as int .
- you are redeclaring i and j variable which are already passed into the function as arguments
- apart from all these sytax errors there are logical errors too in pairsum function you are comparing a[i]+a[j] with k but k is an index instead it should be a[i]+a[j] == target-a[k] similarly for other conditions as well
#include<bits/stdc++.h>
using namespace std;
int pairsum(int a[],int n, int k,int &i,int &j ,int x){
sort(a,a+n);
while(i<j){
if(a[i]+a[j]==x-a[k]){
break;
i++;
jβ;
}
else if(a[i]+a[j]<x-a[k]){
i++;
}
else if(a[i]+a[j]>x-a[k]){
jβ;
}
}
return a[i]+a[j];
}
int main() {
int n,x;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
cin>>x;
int i=0;
int j=n-1;
for(int k=1;k<=n;k++){
if((pairsum(a,n,k,i,j,x)+x-k)==x){
cout<<a[i]<<" β<<a[j]<<β "<<βandβ<<(x-k)<<endl;
}
}
return 0;
}
ab kya error hai isme
@kumarakash121005 in main function if((pairsum(a,n,k,i,j,x)+x-k)==x) condition is wrong and you are also not checking for the duplicate pairs.
reffer to below code for your errors and kidly rectify them.
the below code is your code edited by me to produce correct output.
vector<vector>ans;
void pairsum(vector&a,int n, int k,int tgt,int &i,int &j ){
while(i<j){
if(a[i]+a[j]<tgt){
i++;
}
else if(a[i]+a[j]>tgt){
j--;
}
else{
vector<int>b(3);
b[0]=a[i];
b[1]=a[j];
b[2]=a[k];
ans.push_back(b);
while( i<n-1 && a[i]==a[i+1]) i++;
while( j>0 && a[j]==a[j-1]) j--;
i++;
j--;
}
}
}
int main (vector& nums) {
int n=nums.size();
sort(nums.begin(),nums.end());
if(n <=2) return ans;
int x;
cin>>x;
for(int k=0;k<n;k++){
int i=k+1;
int j=n-1;
if(k>0 && nums[k] == nums[k-1]) // avoid duplicate triplets count
continue;
pairsum(nums,n,k,x-nums[k],i,j);
}
for(int t=0;t<ans.size();t++)
{
for(int tt=0;tt<ans[0].size();tt++)
cout<<ans[t][tt]<<" ";
cout<<endl;
}
}
Hope it will help