
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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.
- Related Articles
- Program to find number of subsequences that satisfy the given sum condition using Python
- C++ program to find out the number of pairs in an array that satisfy a given condition
- How to count the number of values that satisfy a condition in an R vector?
- Find n positive integers that satisfy the given equations in C++
- Find numbers a and b that satisfy the given condition in C++
- Count subsets that satisfy the given condition in C++
- Find number of pairs in an array such that their XOR is 0 using C++.
- Find the number of solutions to the given equation in C++
- Find the Number of solutions for the equation x + y + z
- Find the frequency of a number in an array using C++.
- Find the Number of Prime Pairs in an Array using C++
- Find the Number of Unique Pairs in an Array using C++
- Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python
- How to find the serial number of an Android device using Kotlin?
- Find maximum number that can be formed using digits of a given number in C++
