Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AINumber of sextuplets (or six values) that satisfy an equation

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

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<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 occurrences 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;
}


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 occurrences 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 occurrences 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 occurrences 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




<?php
// PHP program to count of 6 values from
// an array that satisfy an equation
// with 6 variables
 
function 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;
}
 
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 occurrences 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
?>


Javascript




<script>
 
// Javascript program to count of
// 6 values from an array
// that satisfy an equation with
// 6 variables
 
 
function upper_bound(array , length , value)
{
        var low = 0;
        var high = length;
        while (low < high) {
            var mid = parseInt((low + high) / 2);
            if (value >= array[mid]) {
                low = mid + 1;
            } else {
                high = mid;
            }
        }
        return low;
    }
  function lower_bound(array , length , value)
  {
        var low = 0;
        var high = length;
        while (low < high) {
            var mid = parseInt((low + high) / 2);
            if (value <= array[mid]) {
                high = mid;
            } else {
                low = mid + 1;
            }
        }
        return low;
    
function findSextuplets(arr , n)
{
    // Generating possible values of
    // RHS of the equation
    var index = 0;
    var RHS = Array.from({length: n * n * n + 1},
    (_, i) => 0);
    for (i = 0; i < n; i++)
    {
    // Checking d should be non-zero.
      if (arr[i] != 0)
      {
        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.
    RHS.sort((a,b)=>a-b);
 
    // Generating all possible values
    // of LHS of the equation
    // and finding the number of occurrences
    // of the value in RHS.
    var result = 0;
    for (i = 0; i < n; i++)
    {
        for (j = 0; j < n; j++)
        {
            for (k = 0; k < n; k++)
            {
                var val = arr[i] * arr[j] + arr[k];
                result += (upper_bound(RHS, index, val)-
                lower_bound(RHS, index, val));
            }
        }
    }
 
    return result;
}
 
// Driven Program
var arr = [2, 3];
var n = arr.length;
 
document.write(findSextuplets(arr, n));
 
 
// This code is contributed by 29AjayKumar
 
</script>


Output

4

Time Complexity : O(N3 log N)
Auxiliary Space: O(N3) as it is using extra space for array RHS

This article is contributed by Anuj Chauhan(anuj0503). If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments