# 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 implementation of this approach:

## C++

 `// C++ program to count of 6 values from an array ` `// that satisfy an equation with 6 variables ` `#include ` `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); ` ` `  `    ``cout << findSextuplets(arr, n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to count of 6 values from an array  ` `// that satisfy an equation with 6 variables  ` `import` `java.util.Arrays;  ` `class` `GFG{ ` `static` `int` `upper_bound(``int``[] array, ``int` `length, ``int` `value) { ` `        ``int` `low = ``0``; ` `        ``int` `high = length; ` `        ``while` `(low < high) { ` `            ``final` `int` `mid = (low + high) / ``2``; ` `            ``if` `(value >= array[mid]) { ` `                ``low = mid + ``1``; ` `            ``} ``else` `{ ` `                ``high = mid; ` `            ``} ` `        ``} ` `        ``return` `low; ` `    ``} ` `  ``static` `int` `lower_bound(``int``[] array, ``int` `length, ``int` `value) { ` `        ``int` `low = ``0``; ` `        ``int` `high = length; ` `        ``while` `(low < high) { ` `            ``final` `int` `mid = (low + high) / ``2``; ` `            ``if` `(value <= array[mid]) { ` `                ``high = mid; ` `            ``} ``else` `{ ` `                ``low = mid + ``1``; ` `            ``} ` `        ``} ` `        ``return` `low; ` `    ``}   ` `static` `int` `findSextuplets(``int``[] arr, ``int` `n) ` `{ ` `    ``// Generating possible values of RHS of the equation  ` `    ``int` `index = ``0``; ` `    ``int``[] RHS = ``new` `int``[n * n * n + ``1``]; ` `    ``for` `(``int` `i = ``0``; i < n; i++) ` `    ``{ ` `      ``if` `(arr[i] != ``0``) ``// 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.  ` `    ``Arrays.sort(RHS); ` ` `  `    ``// 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, index, val)-lower_bound(RHS, index, val)); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``return` `result; ` `} ` ` `  `// Driven Program  ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int``[] arr = {``2``, ``3``}; ` `    ``int` `n = arr.length; ` ` `  `    ``System.out.println(findSextuplets(arr, n)); ` ` `  `} ` `} ` `// This code is contributed by mits `

## Python3

 `# Python3 program to count of 6 values  ` `# from an array that satisfy an equation  ` `# with 6 variables  ` ` `  `def` `upper_bound(array, length, value): ` `    ``low ``=` `0``; ` `    ``high ``=` `length; ` `    ``while` `(low < high): ` `        ``mid ``=` `int``((low ``+` `high) ``/` `2``); ` `        ``if` `(value >``=` `array[mid]): ` `                ``low ``=` `mid ``+` `1``; ` `        ``else``: ` `            ``high ``=` `mid; ` `         `  `    ``return` `low; ` `     `  `def` `lower_bound(array, length, value): ` `    ``low ``=` `0``; ` `    ``high ``=` `length; ` `    ``while` `(low < high): ` `        ``mid ``=` `int``((low ``+` `high) ``/` `2``); ` `        ``if` `(value <``=` `array[mid]): ` `            ``high ``=` `mid; ` `        ``else``: ` `            ``low ``=` `mid ``+` `1``; ` `    ``return` `low; ` `     `  `def` `findSextuplets(arr, n): ` ` `  `    ``# Generating possible values of  ` `    ``# RHS of the equation  ` `    ``index ``=` `0``; ` `    ``RHS ``=` `[``0``] ``*` `(n ``*` `n ``*` `n ``+` `1``); ` `     `  `    ``for` `i ``in` `range``(n): ` `        ``if` `(arr[i] !``=` `0``): ` `             `  `            ``# Checking d should be non-zero. ` `            ``for` `j ``in` `range``(n): ` `                ``for` `k ``in` `range``(n): ` `                    ``RHS[index] ``=` `arr[i] ``*` `(arr[j] ``+`  `                                           ``arr[k]); ` `                    ``index ``+``=` `1``; ` ` `  `    ``# Sort RHS[] so that we can do ` `    ``# binary search in it.  ` `    ``RHS.sort(); ` ` `  `    ``# Generating all possible values of  ` `    ``# LHS of the equation and finding the ` `    ``# number of occurances of the value in RHS.  ` `    ``result ``=` `0``; ` `    ``for` `i ``in` `range``(n): ` `        ``for` `j ``in` `range``(n): ` `            ``for` `k ``in` `range``(n): ` `                ``val ``=` `arr[i] ``*` `arr[j] ``+` `arr[k]; ` `                ``result ``+``=` `(upper_bound(RHS, index, val) ``-` `                           ``lower_bound(RHS, index, val)); ` ` `  `    ``return` `result; ` ` `  `# Driver Code ` `arr ``=` `[``2``, ``3``]; ` `n ``=` `len``(arr); ` ` `  `print``(findSextuplets(arr, n)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# program to count of 6 values from an array  ` `// that satisfy an equation with 6 variables  ` `using` `System; ` `using` `System.Collections; ` ` `  `class` `GFG{ ` `static` `int` `upper_bound(``int``[] array, ``int` `length, ``int` `value) { ` `        ``int` `low = 0; ` `        ``int` `high = length; ` `        ``while` `(low < high) { ` `            ``int` `mid = (low + high) / 2; ` `            ``if` `(value >= array[mid]) { ` `                ``low = mid + 1; ` `            ``} ``else` `{ ` `                ``high = mid; ` `            ``} ` `        ``} ` `        ``return` `low; ` `    ``} ` `static` `int` `lower_bound(``int``[] array, ``int` `length, ``int` `value) { ` `        ``int` `low = 0; ` `        ``int` `high = length; ` `        ``while` `(low < high) { ` `            ``int` `mid = (low + high) / 2; ` `            ``if` `(value <= array[mid]) { ` `                ``high = mid; ` `            ``} ``else` `{ ` `                ``low = mid + 1; ` `            ``} ` `        ``} ` `        ``return` `low; ` `    ``}  ` `static` `int` `findSextuplets(``int``[] arr, ``int` `n) ` `{ ` `    ``// Generating possible values of RHS of the equation  ` `    ``int` `index = 0; ` `    ``int``[] RHS = ``new` `int``[n * n * n + 1]; ` `    ``for` `(``int` `i = 0; i < n; i++) ` `    ``{ ` `    ``if` `(arr[i] != 0) ``// 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.  ` `    ``Array.Sort(RHS); ` ` `  `    ``// 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, index, val)-lower_bound(RHS, index, val)); ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``return` `result; ` `} ` ` `  `// Driven Program  ` `static` `void` `Main() ` `{ ` `    ``int``[] arr = {2, 3}; ` `    ``int` `n = arr.Length; ` ` `  `    ``Console.WriteLine(findSextuplets(arr, n)); ` ` `  `} ` `} ` `// This code is contributed by mits `

## PHP

 `= ``\$array``[``\$mid``]) ` `                ``\$low` `= ``\$mid` `+ 1; ` `        ``else` `            ``\$high` `= ``\$mid``; ` `    ``} ` `    ``return` `\$low``; ` `} ` ` `  `function` `lower_bound(``\$array``, ``\$length``, ``\$value``) ` `{ ` `    ``\$low` `= 0; ` `    ``\$high` `= ``\$length``; ` `    ``while` `(``\$low` `< ``\$high``) ` `    ``{ ` `        ``\$mid` `= (int)((``\$low` `+ ``\$high``) / 2); ` `        ``if` `(``\$value` `<= ``\$array``[``\$mid``]) ` `            ``\$high` `= ``\$mid``; ` `        ``else` `            ``\$low` `= ``\$mid` `+ 1; ` `    ``} ` `    ``return` `\$low``; ` `} ` ` `  `// Returns count of 6 values from arr[] ` `// that satisfy an equation with 6 variables ` `function` `findSextuplets(``\$arr``, ``\$n``) ` `{ ` `    ``// Generating possible values of ` `    ``// RHS of the equation  ` `    ``\$index` `= 0; ` `    ``\$RHS` `= ``array_fill``(0, ``\$n` `* ``\$n` `* ``\$n` `+ 1, 0); ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `    ``if` `(``\$arr``[``\$i``] != 0) ``// Checking d should be non-zero. ` `        ``for` `(``\$j` `= 0; ``\$j` `< ``\$n``; ``\$j``++) ` `        ``for` `(``\$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``); ` ` `  `    ``// Generating all possible values of LHS  ` `    ``// of the equation and finding the number ` `    ``// of occurances of the value in RHS.  ` `    ``\$result` `= 0; ` `    ``for` `(``\$i` `= 0; ``\$i` `< ``\$n``; ``\$i``++) ` `        ``for` `(``\$j` `= 0; ``\$j` `< ``\$n``; ``\$j``++) ` `            ``for` `(``\$k` `= 0; ``\$k` `< ``\$n``; ``\$k``++) ` `            ``{ ` `                ``\$val` `= ``\$arr``[``\$i``] * ``\$arr``[``\$j``] + ``\$arr``[``\$k``]; ` `                ``\$result` `+= (upper_bound(``\$RHS``, ``\$index``, ``\$val``) -  ` `                            ``lower_bound(``\$RHS``, ``\$index``, ``\$val``)); ` `            ``} ` ` `  `    ``return` `\$result``; ` `} ` ` `  `// Driver Code ` `\$arr` `= ``array``(2, 3); ` `\$n` `= ``count``(``\$arr``); ` ` `  `print``(findSextuplets(``\$arr``, ``\$n``)); ` ` `  `// This code is contributed by mits ` `?> `

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.