Number of sextuplets (or six values) that satisfy an equation - GeeksforGeeks
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:
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.
Read full article from Number of sextuplets (or six values) that satisfy an equation - GeeksforGeeks
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 dFirst, 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.
Time Complexity : O(N3 log N)
// 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;
}