target triplet sum of array

 // Online C++ compiler to run C++ program online

#include <iostream>

#include <bits/stdc++.h>

#include <climits>


using namespace std;




void display(int *arr, int n){


    for(int i=0;i<n;i++){


        cout<<arr[i];


    }


}






int main() {


    int arr[]={1,4,3,2,5,6,7,8};


    int n=sizeof(arr)/sizeof(int);

    int target=10;

    sort(arr,arr+n);//void type


    //cout<<n;

    int left=0;

    int right=n;

    for(int i=0;i<n;i++){

        left=i+1;

    while (left<right){

        if((arr[left]+arr[right])>(target-arr[i])){

            right--;

        }

        if((arr[left]+arr[right])<(target-arr[i])){

            left++;

        }

        if(arr[left]+arr[right]==(target-arr[i])){

            cout<<arr[i]<<" "<<arr[left]<<" "<<arr[right]<<endl;

            left++;

            right--;

            

        }

    }

    }

    


    return 0;


}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings