Horje
Count number of triplets with product equal to given number | Set 2

Given an array of distinct integers(considering only positive numbers) and a number ‘m’, find the number of triplets with the product equal to ‘m’.

Examples:

Input: arr[] = { 1, 4, 6, 2, 3, 8}  
            m = 24
Output: 3

Input: arr[] = { 0, 4, 6, 2, 3, 8}  
            m = 18
Output: 0

An approach with O(n) extra space has already been discussed in previous post. In this post an approach with O(1) space complexity will be discussed.

Approach: The idea is to use Three-pointer technique:

  1. Sort the input array.
  2. Fix the first element as A[i] where i is from 0 to array size – 2.
  3. After fixing the first element of triplet, find the other two elements using 2 pointer technique.

Below is the implementation of above approach:

C++

<?php
// PHP  implementation of above approach
 
// Function to count such triplets
 
function  countTriplets($arr, $n, $m)
{
     $count = 0;
 
    // Sort the array
    sort($arr);
    $end; $start; $mid;
 
    // three pointer technique
    for ($end = $n - 1; $end >= 2; $end--) {
         $start = 0;
         $mid = $end - 1;
        while ($start < $mid) {
 
            // Calculate the product of a triplet
            $prod = $arr[$end] * $arr[$start] * $arr[$mid];
 
            // Check if that product is greater than m,
            // decrement mid
            if ($prod > $m)
                $mid--;
 
            // Check if that product is smaller than m,
            // increment start
            else if ($prod < $m)
                $start++;
 
            // Check if that product is equal to m,
            // decrement mid, increment start and
            // increment the count of pairs
            else if ($prod == $m) {
                $count++;
                $mid--;
                $start++;
            }
        }
    }
 
    return $count;
}
 
// Drivers code
  
    $arr = array( 1, 1, 1, 1, 1, 1 );
     $n = sizeof($arr) / sizeof($arr[0]);
    $m = 1;
 
    echo  countTriplets($arr, $n, $m);
 
 
#This Code is Contributed by ajit
?>

Javascript

<script>
 
    // Javascript implementation of above approach
     
    // Function to count such triplets
    function countTriplets(arr, n, m)
    {
        let count = 0;
 
        // Sort the array
        arr.sort(function(a, b){return a - b});
        let end, start, mid;
 
        // three pointer technique
        for (end = n - 1; end >= 2; end--)
        {
            start = 0; mid = end - 1;
            while (start < mid)
            {
 
                // Calculate the product
                // of a triplet
                let prod = arr[end] * arr[start] * arr[mid];
 
                // Check if that product
                // is greater than m,
                // decrement mid
                if (prod > m)
                    mid--;
 
                // Check if that product
                // is smaller than m,
                // increment start
                else if (prod < m)
                    start++;
 
                // Check if that product
                // is equal to m,
                // decrement mid, increment
                // start and increment the
                // count of pairs
                else if (prod == m)
                {
                    count++;
                    mid--;
                    start++;
                }
            }
        }
 
        return count;
    }
     
    let arr = [ 1, 1, 1, 1, 1, 1 ];
    let n = arr.length;
    let m = 1;
       
    document.write(countTriplets(arr, n, m));
 
</script>

Output

6

Complexity Analysis:

  • Time complexity: O(N^2) 
  • Space Complexity: O(1)



Reffered: https://www.geeksforgeeks.org


Arrays

Related
Sum of bitwise OR of all subarrays Sum of bitwise OR of all subarrays
Majority element in a circular array of 0&#039;s and 1&#039;s Majority element in a circular array of 0&#039;s and 1&#039;s
Count elements in the given range which have maximum number of divisors Count elements in the given range which have maximum number of divisors
Elements that occurred only once in the array Elements that occurred only once in the array
Longest subarray having average greater than or equal to x | Set-2 Longest subarray having average greater than or equal to x | Set-2

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
9