Computer Science, asked by Ujagar77241, 11 months ago

Given 3 unsorted arrays a, b and c you need to find all possible combinations such that a[i] b[j]

Answers

Answered by jashandeepsingh029
1

Explanation:

Given an array and a value, find if there is a triplet in array whose sum is equal to the given value. If there is such a triplet present in array, then print the triplet and return true. Else return false. For example, if the given array is {12, 3, 4, 1, 6, 9} and given sum is 24, then there is a triplet (12, 3 and 9) present in array whose sum is 24.

Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

Method 1 (Naive)

A simple method is to generate all possible triplets and compare the sum of every triplet with the given value. The following code implements this simple method using three nested loops.

#include <bits/stdc++.h>

using namespace std;

// returns true if there is triplet with sum equal

// to 'sum' present in A[]. Also, prints the triplet

bool find3Numbers(int A[], int arr_size, int sum)

{

int l, r;

// Fix the first element as A[i]

for (int i = 0; i < arr_size - 2; i++)

{

// Fix the second element as A[j]

for (int j = i + 1; j < arr_size - 1; j++)

{

// Now look for the third number

for (int k = j + 1; k < arr_size; k++)

{

if (A[i] + A[j] + A[k] == sum)

{

cout << "Triplet is " << A[i] <<

", " << A[j] << ", " << A[k];

return true;

}

}

}

}

// If we reach here, then no triplet was found

return false;

}

/* Driver code */

int main()

{

int A[] = { 1, 4, 45, 6, 10, 8 };

int sum = 22;

int arr_size = sizeof(A) / sizeof(A[0]);

find3Numbers(A, arr_size, sum);

return 0;

}

Similar questions