GIVEN A RANDOM INTEGER ARRAY AND A NUMBER X. FIND AND PRINT THE TRIPLETS OF ELEMENTS IN THE ARRAY THAT SUM TO X. WHILE PRINTING A TRIPLET, PRINT THE SMALLEST ELEMENT FIRST. THAT IS IF A VALID TRIPLET IS (6, 5, 10) PRINT “5 6 10”. THERE IS NO CONSTRAINT THAT OUT OF 5 TRIPLETS WHICH HAVE TO BE PRINTED ON 1ST LINE. YOU CAN PRINT TRIPLETS IN ANY ORDER, JUST BE CAREFUL ABOUT THE ORDER OF ELEMENTS IN A TRIPLET.

TITLE:-Triplet sum
PROBLEM:-

Given a random integer array and a number x. Find and print the triplets of elements in the array which sum to x.

While printing a triplet, print the smallest element first.

That is, if a valid triplet is (6, 5, 10) print “5 6 10”. There is no constraint that out of 5 triplets which have to be printed on 1st line. You can print triplets in any order, just be careful about the order of elements in a triplet.

Input format :
Line 1 : Integer N (Array Size)
Line 2 : Array elements (separated by space)
Line 3 : Integer x
Output format :
Line 1 : Triplet 1 elements (separated by space)
Line 2 : Triplet 3 elements (separated by space)
Line 3 : and so on
Constraints :

1 <= N <= 1000

1 <= x <= 100

Sample Input:
7
1 2 3 4 5 6 7 
12
Sample Output ;
1 4 7
1 5 6
2 3 7
2 4 6
3 4 5

SOLUTION:-

#include <iostream>
using namespace std;
#include<bits/stdc++.h>
void FindTriplet(int arr[], int n, int x) 
{
    sort(arr,arr+n);
    for(int i=0;i<n;i++)
    {
            for(int j=i+1;j<n;j++)
            {
                    for(int k=j+1;k<n;k++)
                    {
                        if(arr[i]+arr[j]+arr[k]==x)
                        {
                            cout<<arr[i]<<" "<<arr[j]<<" "<<arr[k]<<endl;
                        }
                    }

            }

    }

}


int main() {

	int size;

	int x;
	cin>>size;
	
	int *input=new int[1+size];	
	
	for(int i=0;i<size;i++)
		cin>>input[i];
	cin>>x;

	FindTriplet(input,size,x);
		
	return 0;
}

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *