# Number of sextuplets (or six values) that satisfy an equation

Given an array of n elements. The task is to find number of sextuplets that satisfy the below equation such that a, b, c, d, e and f belong to the given array:

```a * b + c - e = f
d
```

Examples:

```Input :  arr[] = { 1 }.
Output : 1
a = b = c = d = e = f = 1 satisfy
the equation.

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

Input :  arr[] = { 1, -1 }
Output : 24
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

First, reorder the equation, a * b + c = (f + e) * d.
Now, make two arrays, one for LHS (Left Hand Side) of the equation and one for the RHS (Right Hand Side) of the equation. Search each element of RHS’s array in the LHS’s array. Whenever you find a value of RHS in LHS, check how many times it is repeated in LHS and add that count to the total. Searching can be done using binary search, by sorting the LHS array.

Below is the C++ implementation of this approach:

```// C++ program to count of 6 values from an array
// that satisfy an equation with 6 variables
#include<bits/stdc++.h>
using namespace std;

// Returns count of 6 values from arr[]
// that satisfy an equation with 6 variables
int findSextuplets(int arr[], int n)
{
// Generating possible values of RHS of the equation
int index = 0;
int RHS[n*n*n + 1];
for (int i = 0; i < n; i++)
if (arr[i])  // Checking d should be non-zero.
for (int j = 0; j < n; j++)
for (int k = 0; k < n; k++)
RHS[index++] = arr[i] * (arr[j] + arr[k]);

// Sort RHS[] so that we can do binary search in it.
sort(RHS, RHS + n);

// Generating all possible values of LHS of the equation
// and finding the number of occurances of the value in RHS.
int result = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
for(int k = 0; k < n; k++)
{
int val = arr[i] * arr[j] + arr[k];
result += (upper_bound(RHS, RHS + index, val) -
lower_bound(RHS, RHS + index, val));
}
}
}

return result;
}

// Driven Program
int main()
{
int arr[] = {2, 3};
int n = sizeof(arr)/sizeof(arr[0]);

cout << findSextuplets(arr, n) << endl;
return 0;
}
```

Output:

```4
```

Time Complexity : O(N3 log N)

This article is contributed by Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
2.3 Average Difficulty : 2.3/5.0
Based on 3 vote(s)