Search code examples
phpgroupingelementmultiplicationfactors

How to group factors by product and display if a product has more than one factor-pair?


Using a predefined array of numbers, how can I use PHP to generate a multi-dimensional array which groups all factor-pairs by their product?

Input array:

$array = array(1,2,3,4,5,6,7,8);
  • I want to display all factor-pairs for each product group that have more than one factor-pair.

  • In a case where there are no product groups that have more than one factor-pair, No pairs Found should be displayed.

Given the above input, this is my expected result:

1 6 and 2 3  // product group = 6
1 8 and 2 4  // product group = 8
2 6 and 3 4  // product group = 12
3 8 and 4 6  // product group = 24

*note as the input array increases in size, the output will display more than two factor-pairs per group.

This is my code from C++:

 void findPairs(int arr[], int n)
{
    bool found = false;
    unordered_map<int, pair < int, int > > H;
    for (int i=0; i<n; i++)
    {
        for (int j=i+1; j<n; j++)
        {
            // If product of pair is not in hash table,
            // then store it
            int prod = arr[i]*arr[j];
            if (H.find(prod) == H.end())
                H[prod] = make_pair(i,j);

            // If product of pair is also available in
            // then print current and previous pair
            else
            {
                pair<int,int> pp = H[prod];
                cout << arr[pp.first] << " " << arr[pp.second]
                     << " and " << arr[i]<<" "<<arr[j]<<endl;
                found = true;
            }
        }
    }
    // If no pair find then print not found
    if (found == false)
        cout << "No pairs Found" << endl;
}

Solution

  • This is your C++ code "translated" to PHP (mostly by search & replace).

    90% of the translation was achieved by removing the variable types and prepending the variable names with $. The array PHP type is a mixture of array, list and map (aka hash, dictionary) and can be used for both $H and the values it contains (pairs of values).

    function findPairs(array $arr, $n)
    {
        $found = false;
        $H = array();
        for ($i=0; $i<$n; $i++)
        {
            for ($j=$i+1; $j<$n; $j++)
            {
                // If product of pair is not in hash table,
                // then store it
                $prod = $arr[$i]*$arr[$j];
                if (! array_key_exists($prod, $H))
                    $H[$prod] = array($i,$j);
    
                // If product of pair is also available in
                // then print current and previous pair
                else
                {
                    $pp = $H[$prod];
                    echo $arr[$pp[0]], " ", $arr[$pp[1]]
                         , " and ", $arr[$i], " ", $arr[$j], "\n";
                    $found = true;
                }
            }
        }
        // If no pair find then print not found
        if ($found == false)
            echo "No pairs Found\n";
    }
    
    $array = array(1,2,3,4,5,6,7,8);
    findPairs($array, count($array));
    

    And this is its output:

    1 6 and 2 3
    1 8 and 2 4
    2 6 and 3 4
    3 8 and 4 6