Find the Number of Sextuplets that Satisfy an Equation using C++



In this article, we will describe an approach to find a number of sextuplets that satisfy an equation. So we are taking an equation as an example where we need to find the values of a, b, c, d, e, and f that satisfy the below equation.

( a + b + c ) * e / d = f

Let's reorder the equation −

( a + b + c ) = ( f * d ) / e

Here is a simple example for the given problem −

Input : arr [ ] = { 1, 3 }
Output : 4
Explanation : ( a, b, c, e, f ) = 1, d = 3
   ( a, b, c, d, e ) = 1, f = 3
   ( a, b, c ) = 1, ( d, e, f ) = 3
   ( a, b, c, d, f ) = 3, ( e ) = 1

Input : arr [ ] = { 2, 5 }
Output : 3

Approach to Find the Solution

We will use a Naive approach to find the solution to the given problem.

Naive Approach

In this problem, looking at LHS and RHS, we can find all the possible results of LHS and storing in an array, Similarly creating an array for RHS and filling it with all possible results of RHS.

Checking both the arrays for the same values and incrementing the count for each value found, and finally showing the result.

Example

#include<bits/stdc++.h>
using namespace std;
int findsamenumbers(int *arr1, int *arr2, int n){
    int i = 0, j = 0, k = 0, count=0;
    while(( i < n*n*n+1) && (j < n*n*n+1)){
        if(arr1[i] < arr2[j])
            i++;
        else if(arr1[i] == arr2[j]){
            count++;
        int temp = arr1[i];
        while(temp==arr1[++i]){
            count++;
        }
        while(temp==arr2[++j]){
            count++;
        }
    }
    else
        j++;
    }  
    return count;
}
int main(){
    int arr[] = {2,5};
    int n = sizeof(arr)/sizeof(arr[0]);
    // Generating all possible values of LHS array
    int index = 0,i;
    int LHS[n*n*n ];
    for ( i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            for(int k = 0; k < n; k++){
                LHS[index++] = (arr[i] * arr[j]) / arr[k];
            }
        }
    }
    // Generating all possible value of RHS array
    int RHS[n*n*n ];
    index=0;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            for (int k = 0; k < n; k++){
                RHS[index++] = (arr[i] + arr[j] + arr[k]);
            }
        }
    }
    sort(RHS, RHS + (n*n*n));
    sort(LHS, LHS + (n*n*n));
    int result = findsamenumbers(LHS, RHS, n);
    cout<<"Number of sextuplets that satisfy an equation: "<<result;
    return 0;
}

Output

Number of sextuplets that satisfy an equation: 3

Explanation of the Above Program

In this program, we are creating two arrays for keeping every result of LHS and RHS. We are using three nested loops for putting every possible value of (a, b, c) in LHS and (d, e, f) in RHS. After that, we are sorting both the arrays to compare both the arrays and find the same values in both the arrays, passing both the arrays to findsamenumber() function.

In the findsamenumber() function, we check for the same values by using two nested loops. When we found two elements the same, we check the frequency of that number in both the arrays so that every possible value counts.

if(arr1[i] == arr2[j]){
   count++;
   int temp = arr1[i];
   while(temp==arr1[++i]){
      count++;
   }
   while(temp==arr2[++j]){
      count++;
   }

Conclusion

In this article, we solved the number of sextuplets that satisfy an equation wherein the given array. We found every possible value of variables in 6 variable equation (a + b + c) * e / d = f. We can solve this problem in any other programming language like C, Java, and python.


Advertisements