Skip to main content

Count all distinct pairs with product equal to k

Here given code implementation process.

import java.util.HashMap;
// Java program for
// Count all distinct pairs with product equal to k
public class DistinctProduct
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			System.out.print(" " + arr[i]);
		}
	}
	public void distinctPairOfKProduct(int[] arr, int n, int k)
	{
		int count = 0;
		int zero = 0;
		// Use to collect frequency of array elements
		HashMap < Integer, Integer > record = 
          new HashMap < Integer, Integer > ();
		for (int i = 0; i < n; ++i)
		{
			if (arr[i] == 0)
			{
				zero = 1;
			}
			else
			{
				if (record.containsKey(arr[i]))
				{
					// When key exists then increase the counter frequency
					record.put(arr[i], record.get(arr[i]) + 1);
				}
				else
				{
					record.put(arr[i], 1);
				}
			}
		}
		if (k == 0)
		{
			// When k is zero 
			if (zero == 1 && record.size() > 0)
			{
				// In case K is zero and zero exist in array
				count = record.size();
			}
		}
		else
		{
			for (int key: record.keySet())
			{
				if (record.get(key) > 1 && (key * key) == k)
				{
					// When key value is more than one. 
					// And its product is equal to k
					count += 2;
				}
				else if (record.containsKey(k / key))
				{
					// When product pair exists
					count += 1;
				}
			}
			if (count > 0)
			{
				count = count / 2;
			}
		}
		// Display given array
		printArray(arr, n);
		// Display calculated result
		System.out.println("\n Resultant distinct pair of product (" + 
                           k + ") is :  " + count);
	}
	public static void main(String[] args)
	{
		DistinctProduct task = new DistinctProduct();
		int[] arr1 = {
			-3 , 2 , 1 , 3 , 6 , 0 , -2 , -1
		};
		int[] arr2 = {
			4 , -2 , 0 , 9 , -1 , 9 , 0
		};
		int n = arr1.length;
		// Test A
		int k = 6;
		/*
		    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]

		    product k = 6   

		    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
		    -------------------------

		   Resultant pair : 3
		*/
		task.distinctPairOfKProduct(arr1, n, k);
		// Test B
		k = 2;
		/*
		    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]

		    product k = 2   

		    (2 ☓ 1)  (-2 ☓ -1)
		    -------------------------

		   Resultant pair : 2
		*/
		task.distinctPairOfKProduct(arr1, n, k);
		// Test C
		// Get length of arr2 
		n = arr2.length;
		k = 0;
		/*
		    arr = [  4, -2 , 0, 9 , -1, 9, 0]

		    product k = 0   

		    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
		    -------------------

		    Resultant pair : 4
		*/
		task.distinctPairOfKProduct(arr2, n, k);
		// Test D
		k = 2;
		/*
		    arr = [ 4, -2 , 0, 9 , -1, 9, 0]

		    product k = 2   

		    (-2 ☓ -1)
		    -------------------

		    Resultant pair : 1
		*/
		task.distinctPairOfKProduct(arr2, n, k);
	}
}

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
// Include header file
#include <iostream>
#include <unordered_map>

using namespace std;
// C++ program for
// Count all distinct pairs with product equal to k
class DistinctProduct
{
    public:
        // Display array elements
        void printArray(int arr[], int n)
        {
            for (int i = 0; i < n; ++i)
            {
                cout << " " << arr[i];
            }
        }
    void distinctPairOfKProduct(int arr[], int n, int k)
    {
        int count = 0;
        int zero = 0;
        // Use to collect frequency of array elements
        unordered_map < int, int > record;
        for (int i = 0; i < n; ++i)
        {
            if (arr[i] == 0)
            {
                zero = 1;
            }
            else
            {
                if (record.find(arr[i]) != record.end())
                {
                    // When key exists then increase the counter frequency
                    record[arr[i]] = record[arr[i]] + 1;
                }
                else
                {
                    record[arr[i]] = 1;
                }
            }
        }
        if (k == 0)
        {
            // When k is zero 
            if (zero == 1 && record.size() > 0)
            {
                // In case K is zero and zero exist in array
                count = record.size();
            }
        }
        else
        {
            for (auto &key: record)
            {
                if (key.second > 1 && (key.first *key.first) == k)
                {
                    // When key.first value is more than one. 
                    // And its product is equal to k
                    count += 2;
                }
                else if (record.find(k / key.first) != record.end())
                {
                    // When product pair exists
                    count += 1;
                }
            }
            if (count > 0)
            {
                count = count / 2;
            }
        }
        // Display given array
        this->printArray(arr, n);
        // Display calculated result
        cout << "\n Resultant distinct pair of product (" 
             << k << ") is :  " << count << endl;
    }
};
int main()
{
    DistinctProduct *task = new DistinctProduct();
    int arr1[] = {
        -3 , 2 , 1 , 3 , 6 , 0 , -2 , -1
    };
    int arr2[] = {
        4 , -2 , 0 , 9 , -1 , 9 , 0
    };
    int n = sizeof(arr1) / sizeof(arr1[0]);
    // Test A
    int k = 6;
    /*
        arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]

        product k = 6   

        (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
        -------------------------

       Resultant pair : 3
    */
    task->distinctPairOfKProduct(arr1, n, k);
    // Test B
    k = 2;
    /*
        arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]

        product k = 2   

        (2 ☓ 1)  (-2 ☓ -1)
        -------------------------

       Resultant pair : 2
    */
    task->distinctPairOfKProduct(arr1, n, k);
    // Test C
    // Get length of arr2 
    n = sizeof(arr2) / sizeof(arr2[0]);
    k = 0;
    /*
        arr = [  4, -2 , 0, 9 , -1, 9, 0]

        product k = 0   

        (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
        -------------------

        Resultant pair : 4
    */
    task->distinctPairOfKProduct(arr2, n, k);
    // Test D
    k = 2;
    /*
        arr = [ 4, -2 , 0, 9 , -1, 9, 0]

        product k = 2   

        (-2 ☓ -1)
        -------------------

        Resultant pair : 1
    */
    task->distinctPairOfKProduct(arr2, n, k);
    return 0;
}

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
// Include namespace system
using System;
using System.Collections.Generic;
// Csharp program for
// Count all distinct pairs with product equal to k
public class DistinctProduct
{
	// Display array elements
	public void printArray(int[] arr, int n)
	{
		for (int i = 0; i < n; ++i)
		{
			Console.Write(" " + arr[i]);
		}
	}
	public void distinctPairOfKProduct(int[] arr, int n, int k)
	{
		int count = 0;
		int zero = 0;
		// Use to collect frequency of array elements
		Dictionary < int, int > record = 
            new Dictionary < int, int > ();
		for (int i = 0; i < n; ++i)
		{
			if (arr[i] == 0)
			{
				zero = 1;
			}
			else
			{
				if (record.ContainsKey(arr[i]))
				{
					// When key exists then increase the counter frequency
					record[arr[i]] = record[arr[i]] + 1;
				}
				else
				{
					record.Add(arr[i], 1);
				}
			}
		}
		if (k == 0)
		{
			// When k is zero
			if (zero == 1 && record.Count > 0)
			{
				// In case K is zero and zero exist in array
				count = record.Count;
			}
		}
		else
		{
			foreach(KeyValuePair < int, int > info in record)
			{
				if (info.Value > 1 && (info.Key * info.Key) == k)
				{
					// When key value is more than one.
					// And its product is equal to k
					count += 2;
				}
				else if (record.ContainsKey(k / info.Key))
				{
					// When product pair exists
					count += 1;
				}
			}
			if (count > 0)
			{
				count = count / 2;
			}
		}
		// Display given array
		this.printArray(arr, n);
		// Display calculated result
		Console.WriteLine("\n Resultant distinct pair of product (" + 
                          k + ") is :  " + count);
	}
	public static void Main(String[] args)
	{
		DistinctProduct task = new DistinctProduct();
		int[] arr1 = {
			-3 , 2 , 1 , 3 , 6 , 0 , -2 , -1
		};
		int[] arr2 = {
			4 , -2 , 0 , 9 , -1 , 9 , 0
		};
		int n = arr1.Length;
		// Test A
		int k = 6;
		/*
		    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
		    product k = 6   
		    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
		    -------------------------
		   Resultant pair : 3
		*/
		task.distinctPairOfKProduct(arr1, n, k);
		// Test B
		k = 2;
		/*
		    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
		    product k = 2   
		    (2 ☓ 1)  (-2 ☓ -1)
		    -------------------------
		   Resultant pair : 2
		*/
		task.distinctPairOfKProduct(arr1, n, k);
		// Test C
		// Get length of arr2
		n = arr2.Length;
		k = 0;
		/*
		    arr = [  4, -2 , 0, 9 , -1, 9, 0]
		    product k = 0   
		    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
		    -------------------
		    Resultant pair : 4
		*/
		task.distinctPairOfKProduct(arr2, n, k);
		// Test D
		k = 2;
		/*
		    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
		    product k = 2   
		    (-2 ☓ -1)
		    -------------------
		    Resultant pair : 1
		*/
		task.distinctPairOfKProduct(arr2, n, k);
	}
}

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
package main
import "fmt"
// Go program for
// Count all distinct pairs with product equal to k

// Display array elements
func printArray(arr[] int, n int) {
	for i := 0 ; i < n ; i++ {
		fmt.Print(" ", arr[i])
	}
}
func distinctPairOfKProduct(arr[] int, n int, k int) {
	var count int = 0
	var zero int = 0
	// Use to collect frequency of array elements
	var record = make(map[int] int)
	for i := 0 ; i < n ; i++ {
		if arr[i] == 0 {
			zero = 1
		} else {
			if _, found := record[arr[i]] ; found {
				// When key exists then increase the counter frequency
				record[arr[i]] = record[arr[i]] + 1
			} else {
				record[arr[i]] = 1
			}
		}
	}
	if k == 0 {
		// When k is zero
		if zero == 1 && len(record) > 0 {
			// In case K is zero and zero exist in array
			count = len(record)
		}
	} else {
		for key, value := range record {
			if value > 1 && (key * key) == k {
				// When key value is more than one.
				// And its product is equal to k
				count += 2
			} else if _, found := record[k / key] ; found {
				// When product pair exists
				count += 1
			}
		}
		if count > 0 {
			count = count / 2
		}
	}
	// Display given array
	printArray(arr, n)
	// Display calculated result
	fmt.Println("\n Resultant distinct pair of product (", k, ") is :  ", count)
}
func main() {
	
	var arr1 = [] int {- 3 , 2, 1, 3, 6 , 0, -2 , -1 }
	var arr2 = [] int {4, -2 , 0, 9 , -1, 9, 0}
	var n int = len(arr1)
	// Test A
	var k int = 6
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 6   
	    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
	    -------------------------
	   Resultant pair : 3
	*/
	distinctPairOfKProduct(arr1, n, k)
	// Test B
	k = 2
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 2   
	    (2 ☓ 1)  (-2 ☓ -1)
	    -------------------------
	   Resultant pair : 2
	*/
	distinctPairOfKProduct(arr1, n, k)
	// Test C
	// Get length of arr2
	n = len(arr2)
	k = 0
	/*
	    arr = [  4, -2 , 0, 9 , -1, 9, 0]
	    product k = 0   
	    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
	    -------------------
	    Resultant pair : 4
	*/
	distinctPairOfKProduct(arr2, n, k)
	// Test D
	k = 2
	/*
	    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
	    product k = 2   
	    (-2 ☓ -1)
	    -------------------
	    Resultant pair : 1
	*/
	distinctPairOfKProduct(arr2, n, k)
}

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
<?php
// Php program for
// Count all distinct pairs with product equal to k
class DistinctProduct
{
	// Display array elements
	public	function printArray($arr, $n)
	{
		for ($i = 0; $i < $n; ++$i)
		{
			echo(" ".$arr[$i]);
		}
	}
	public	function distinctPairOfKProduct($arr, $n, $k)
	{
		$count = 0;
		$zero = 0;
		// Use to collect frequency of array elements
		$record = array();
		for ($i = 0; $i < $n; ++$i)
		{
			if ($arr[$i] == 0)
			{
				$zero = 1;
			}
			else
			{
				if (array_key_exists($arr[$i], $record))
				{
					// When key exists then increase the counter frequency
					$record[$arr[$i]] = $record[$arr[$i]] + 1;
				}
				else
				{
					$record[$arr[$i]] = 1;
				}
			}
		}
		if ($k == 0)
		{
			// When k is zero
			if ($zero == 1 && count($record) > 0)
			{
				// In case K is zero and zero exist in array
				$count = count($record);
			}
		}
		else
		{
			foreach($record as $key => $value)
			{
				if ($record[$key] > 1 && ($key * $key) == $k)
				{
					// When key value is more than one.
					// And its product is equal to k
					$count += 2;
				}
				else if (array_key_exists((int)($k / $key), $record))
				{
					// When product pair exists
					$count += 1;
				}
			}
			if ($count > 0)
			{
				$count = (int)($count / 2);
			}
		}
		// Display given array
		$this->printArray($arr, $n);
		// Display calculated result
		echo("\n Resultant distinct pair of product (".$k.
			") is :  ".$count.
			"\n");
	}
}

function main()
{
	$task = new DistinctProduct();
	$arr1 = array(-3, 2, 1, 3, 6, 0, -2, -1);
	$arr2 = array(4, -2, 0, 9, -1, 9, 0);
	$n = count($arr1);
	// Test A
	$k = 6;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 6   
	    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
	    -------------------------
	   Resultant pair : 3
	*/
	$task->distinctPairOfKProduct($arr1, $n, $k);
	// Test B
	$k = 2;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 2   
	    (2 ☓ 1)  (-2 ☓ -1)
	    -------------------------
	   Resultant pair : 2
	*/
	$task->distinctPairOfKProduct($arr1, $n, $k);
	// Test C
	// Get length of arr2
	$n = count($arr2);
	$k = 0;
	/*
	    arr = [  4, -2 , 0, 9 , -1, 9, 0]
	    product k = 0   
	    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
	    -------------------
	    Resultant pair : 4
	*/
	$task->distinctPairOfKProduct($arr2, $n, $k);
	// Test D
	$k = 2;
	/*
	    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
	    product k = 2   
	    (-2 ☓ -1)
	    -------------------
	    Resultant pair : 1
	*/
	$task->distinctPairOfKProduct($arr2, $n, $k);
}
main();

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
// Node JS program for
// Count all distinct pairs with product equal to k
class DistinctProduct
{
	// Display array elements
	printArray(arr, n)
	{
		for (var i = 0; i < n; ++i)
		{
			process.stdout.write(" " + arr[i]);
		}
	}
	distinctPairOfKProduct(arr, n, k)
	{
		var count = 0;
		var zero = 0;
		// Use to collect frequency of array elements
		var record = new Map();
		for (var i = 0; i < n; ++i)
		{
			if (arr[i] == 0)
			{
				zero = 1;
			}
			else
			{
				if (record.has(arr[i]))
				{
					// When key exists then increase the counter frequency
					record.set(arr[i], record.get(arr[i]) + 1);
				}
				else
				{
					record.set(arr[i], 1);
				}
			}
		}
		if (k == 0)
		{
			// When k is zero
			if (zero == 1 && record.size > 0)
			{
				// In case K is zero and zero exist in array
				count = record.size;
			}
		}
		else
		{
			for (let [key, value] of record)
			{
				if (record.get(key) > 1 && (key * key) == k)
				{
					// When key value is more than one.
					// And its product is equal to k
					count += 2;
				}
				else if (record.has(parseInt(k / key)))
				{
					// When product pair exists
					count += 1;
				}
			}
			if (count > 0)
			{
				count = parseInt(count / 2);
			}
		}
		// Display given array
		this.printArray(arr, n);
		// Display calculated result
		console.log("\n Resultant distinct pair of product (" + 
                    k + ") is :  " + count);
	}
}

function main()
{
	var task = new DistinctProduct();
	var arr1 = [-3, 2, 1, 3, 6, 0, -2, -1];
	var arr2 = [4, -2, 0, 9, -1, 9, 0];
	var n = arr1.length;
	// Test A
	var k = 6;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 6   
	    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
	    -------------------------
	   Resultant pair : 3
	*/
	task.distinctPairOfKProduct(arr1, n, k);
	// Test B
	k = 2;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 2   
	    (2 ☓ 1)  (-2 ☓ -1)
	    -------------------------
	   Resultant pair : 2
	*/
	task.distinctPairOfKProduct(arr1, n, k);
	// Test C
	// Get length of arr2
	n = arr2.length;
	k = 0;
	/*
	    arr = [  4, -2 , 0, 9 , -1, 9, 0]
	    product k = 0   
	    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
	    -------------------
	    Resultant pair : 4
	*/
	task.distinctPairOfKProduct(arr2, n, k);
	// Test D
	k = 2;
	/*
	    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
	    product k = 2   
	    (-2 ☓ -1)
	    -------------------
	    Resultant pair : 1
	*/
	task.distinctPairOfKProduct(arr2, n, k);
}
main();

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
#  Python 3 program for
#  Count all distinct pairs with product equal to k
class DistinctProduct :
	#  Display list elements
	def printArray(self, arr, n) :
		i = 0
		while (i < n) :
			print(" ", arr[i], end = "")
			i += 1
		
	
	def distinctPairOfKProduct(self, arr, n, k) :
		count = 0
		zero = 0
		#  Use to collect frequency of list elements
		record = dict()
		i = 0
		while (i < n) :
			if (arr[i] == 0) :
				zero = 1
			else :
				if ((arr[i] in record.keys())) :
					#  When key exists then increase the counter frequency
					record[arr[i]] = record.get(arr[i]) + 1
				else :
					record[arr[i]] = 1
				
			
			i += 1
		
		if (k == 0) :
			#  When k is zero
			if (zero == 1 and len(record) > 0) :
				#  In case K is zero and zero exist in list
				count = len(record)
			
		else :
			for key, value in record.items() :
				if (record.get(key) > 1 and(key * key) == k) :
					#  When key value is more than one.
					#  And its product is equal to k
					count += 2
				elif ((int(k / key) in record.keys())) :
					#  When product pair exists
					count += 1
				
			
			if (count > 0) :
				count = int(count / 2)
			
		
		#  Display given list
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Resultant distinct pair of product (", 
              k ,") is :  ", count)
	

def main() :
	task = DistinctProduct()
	arr1 = [-3, 2, 1, 3, 6, 0, -2, -1]
	arr2 = [4, -2, 0, 9, -1, 9, 0]
	n = len(arr1)
	#  Test A
	k = 6
	#    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	#    product k = 6   
	#    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
	#    -------------------------
	#   Resultant pair : 3
	task.distinctPairOfKProduct(arr1, n, k)
	#  Test B
	k = 2
	#    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	#    product k = 2   
	#    (2 ☓ 1)  (-2 ☓ -1)
	#    -------------------------
	#   Resultant pair : 2
	task.distinctPairOfKProduct(arr1, n, k)
	#  Test C
	#  Get length of arr2
	n = len(arr2)
	k = 0
	#    arr = [  4, -2 , 0, 9 , -1, 9, 0]
	#    product k = 0   
	#    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
	#    -------------------
	#    Resultant pair : 4
	task.distinctPairOfKProduct(arr2, n, k)
	#  Test D
	k = 2
	#    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
	#    product k = 2   
	#    (-2 ☓ -1)
	#    -------------------
	#    Resultant pair : 1
	task.distinctPairOfKProduct(arr2, n, k)

if __name__ == "__main__": main()

Output

  -3  2  1  3  6  0  -2  -1
 Resultant distinct pair of product ( 6 ) is :   3
  -3  2  1  3  6  0  -2  -1
 Resultant distinct pair of product ( 2 ) is :   2
  4  -2  0  9  -1  9  0
 Resultant distinct pair of product ( 0 ) is :   4
  4  -2  0  9  -1  9  0
 Resultant distinct pair of product ( 2 ) is :   1
#  Ruby program for
#  Count all distinct pairs with product equal to k
class DistinctProduct 
	#  Display array elements
	def printArray(arr, n) 
		i = 0
		while (i < n) 
			print(" ", arr[i])
			i += 1
		end

	end

	def distinctPairOfKProduct(arr, n, k) 
		count = 0
		zero = 0
		#  Use to collect frequency of array elements
		record = Hash.new()
		i = 0
		while (i < n) 
			if (arr[i] == 0) 
				zero = 1
			else
 
				if (record.key?(arr[i])) 
					#  When key exists then increase the counter frequency
					record[arr[i]] = record[arr[i]] + 1
				else
 
					record[arr[i]] = 1
				end

			end

			i += 1
		end

		if (k == 0) 
			#  When k is zero
			if (zero == 1 && record.size() > 0) 
				#  In case K is zero and zero exist in array
				count = record.size()
			end

		else
 
			record.each { | key, value |
				if (value > 1 && (key * key) == k) 
					#  When key value is more than one.
					#  And its product is equal to k
					count += 2
				elsif (record.key?(k / key)) 
				#  When product pair exists
				count += 1
			end

			}
			if (count > 0) 
				count = count / 2
			end

		end

		#  Display given array
		self.printArray(arr, n)
		#  Display calculated result
		print("\n Resultant distinct pair of product (", k ,") is :  ", count, "\n")
	end

end

def main() 
	task = DistinctProduct.new()
	arr1 = [-3, 2, 1, 3, 6, 0, -2, -1]
	arr2 = [4, -2, 0, 9, -1, 9, 0]
	n = arr1.length
	#  Test A
	k = 6
	#    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	#    product k = 6   
	#    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
	#    -------------------------
	#   Resultant pair : 3
	task.distinctPairOfKProduct(arr1, n, k)
	#  Test B
	k = 2
	#    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	#    product k = 2   
	#    (2 ☓ 1)  (-2 ☓ -1)
	#    -------------------------
	#   Resultant pair : 2
	task.distinctPairOfKProduct(arr1, n, k)
	#  Test C
	#  Get length of arr2
	n = arr2.length
	k = 0
	#    arr = [  4, -2 , 0, 9 , -1, 9, 0]
	#    product k = 0   
	#    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
	#    -------------------
	#    Resultant pair : 4
	task.distinctPairOfKProduct(arr2, n, k)
	#  Test D
	k = 2
	#    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
	#    product k = 2   
	#    (-2 ☓ -1)
	#    -------------------
	#    Resultant pair : 1
	task.distinctPairOfKProduct(arr2, n, k)
end

main()

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
import scala.collection.mutable._;
// Scala program for
// Count all distinct pairs with product equal to k
class DistinctProduct()
{
	// Display array elements
	def printArray(arr: Array[Int], n: Int): Unit = {
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr(i));
			i += 1;
		}
	}
	def distinctPairOfKProduct(arr: Array[Int], n: Int, k: Int): Unit = {
		var count: Int = 0;
		var zero: Int = 0;
		// Use to collect frequency of array elements
		var record = Map[Int, Int]();
		var i: Int = 0;
		while (i < n)
		{
			if (arr(i) == 0)
			{
				zero = 1;
			}
			else
			{
				if (record.contains(arr(i)))
				{
					// When key exists then increase the counter frequency
					record.addOne(arr(i), record.get(arr(i)).get + 1);
				}
				else
				{
					record.addOne(arr(i), 1);
				}
			}
			i += 1;
		}
		if (k == 0)
		{
			// When k is zero
			if (zero == 1 && record.size > 0)
			{
				// In case K is zero and zero exist in array
				count = record.size;
			}
		}
		else
		{
			for ((key,value) <- record)
			{
				if (key > 1 && (key * key) == k)
				{
					// When key value is more than one.
					// And its product is equal to k
					count += 2;
				}
				else if (record.contains(k / key))
				{
					// When product pair exists
					count += 1;
				}
			}
			if (count > 0)
			{
				count = count / 2;
			}
		}
		// Display given array
		printArray(arr, n);
		// Display calculated result
		println("\n Resultant distinct pair of product (" + k + ") is :  " + count);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DistinctProduct = new DistinctProduct();
		var arr1: Array[Int] = Array(-3, 2, 1, 3, 6, 0, -2, -1);
		var arr2: Array[Int] = Array(4, -2, 0, 9, -1, 9, 0);
		var n: Int = arr1.length;
		// Test A
		var k: Int = 6;
		/*
		    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
		    product k = 6   
		    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
		    -------------------------
		   Resultant pair : 3
		*/
		task.distinctPairOfKProduct(arr1, n, k);
		// Test B
		k = 2;
		/*
		    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
		    product k = 2   
		    (2 ☓ 1)  (-2 ☓ -1)
		    -------------------------
		   Resultant pair : 2
		*/
		task.distinctPairOfKProduct(arr1, n, k);
		// Test C
		// Get length of arr2
		n = arr2.length;
		k = 0;
		/*
		    arr = [  4, -2 , 0, 9 , -1, 9, 0]
		    product k = 0   
		    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
		    -------------------
		    Resultant pair : 4
		*/
		task.distinctPairOfKProduct(arr2, n, k);
		// Test D
		k = 2;
		/*
		    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
		    product k = 2   
		    (-2 ☓ -1)
		    -------------------
		    Resultant pair : 1
		*/
		task.distinctPairOfKProduct(arr2, n, k);
	}
}

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1
import Foundation;
// Swift 4 program for
// Count all distinct pairs with product equal to k
class DistinctProduct
{
	// Display array elements
	func printArray(_ arr: [Int], _ n: Int)
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" ", arr[i], terminator: "");
			i += 1;
		}
	}
	func distinctPairOfKProduct(_ arr: [Int], _ n: Int, _ k: Int)
	{
		var count: Int = 0;
		var zero: Int = 0;
		// Use to collect frequency of array elements
		var record = [Int : Int]();
		var i: Int = 0;
		while (i < n)
		{
			if (arr[i] == 0)
			{
				zero = 1;
			}
			else
			{
				if (record.keys.contains(arr[i]))
				{
					// When key exists then increase the counter frequency
					record[arr[i]] = record[arr[i]]! + 1;
				}
				else
				{
					record[arr[i]] = 1;
				}
			}
			i += 1;
		}
		if (k == 0)
		{
			// When k is zero
			if (zero == 1 && record.count > 0)
			{
				// In case K is zero and zero exist in array
				count = record.count;
			}
		}
		else
		{
			for (key, value) in record
			{
				if (value > 1 && (key * key) == k)
				{
					// When key value is more than one.
					// And its product is equal to k
					count += 2;
				}
				else if (record.keys.contains(k / key))
				{
					// When product pair exists
					count += 1;
				}
			}
			if (count > 0)
			{
				count = count / 2;
			}
		}
		// Display given array
		self.printArray(arr, n);
		// Display calculated result
		print("\n Resultant distinct pair of product (", k ,") is :  ", count);
	}
}
func main()
{
	let task: DistinctProduct = DistinctProduct();
	let arr1: [Int] = [-3, 2, 1, 3, 6, 0, -2, -1];
	let arr2: [Int] = [4, -2, 0, 9, -1, 9, 0];
	var n: Int = arr1.count;
	// Test A
	var k: Int = 6;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 6   
	    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
	    -------------------------
	   Resultant pair : 3
	*/
	task.distinctPairOfKProduct(arr1, n, k);
	// Test B
	k = 2;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 2   
	    (2 ☓ 1)  (-2 ☓ -1)
	    -------------------------
	   Resultant pair : 2
	*/
	task.distinctPairOfKProduct(arr1, n, k);
	// Test C
	// Get length of arr2
	n = arr2.count;
	k = 0;
	/*
	    arr = [  4, -2 , 0, 9 , -1, 9, 0]
	    product k = 0   
	    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
	    -------------------
	    Resultant pair : 4
	*/
	task.distinctPairOfKProduct(arr2, n, k);
	// Test D
	k = 2;
	/*
	    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
	    product k = 2   
	    (-2 ☓ -1)
	    -------------------
	    Resultant pair : 1
	*/
	task.distinctPairOfKProduct(arr2, n, k);
}
main();

Output

  -3  2  1  3  6  0  -2  -1
 Resultant distinct pair of product ( 6 ) is :   3
  -3  2  1  3  6  0  -2  -1
 Resultant distinct pair of product ( 2 ) is :   2
  4  -2  0  9  -1  9  0
 Resultant distinct pair of product ( 0 ) is :   4
  4  -2  0  9  -1  9  0
 Resultant distinct pair of product ( 2 ) is :   1
// Kotlin program for
// Count all distinct pairs with product equal to k
class DistinctProduct
{
	// Display array elements
	fun printArray(arr: Array < Int > , n: Int): Unit
	{
		var i: Int = 0;
		while (i < n)
		{
			print(" " + arr[i]);
			i += 1;
		}
	}
	fun distinctPairOfKProduct(arr: Array < Int > , n: Int, k: Int): Unit
	{
		var count: Int = 0;
		var zero: Int = 0;
		// Use to collect frequency of array elements
		val record = mutableMapOf < Int, Int > ();
		var i: Int = 0;
		while (i < n)
		{
			if (arr[i] == 0)
			{
				zero = 1;
			}
			else
			{
				if (record.containsKey(arr[i]))
				{
					// When key exists then increase the counter frequency
					record.put(arr[i], record.getValue(arr[i]) + 1);
				}
				else
				{
					record.put(arr[i], 1);
				}
			}
			i += 1;
		}
		if (k == 0)
		{
			// When k is zero
			if (zero == 1 && record.count() > 0)
			{
				// In case K is zero and zero exist in array
				count = record.count();
			}
		}
		else
		{
			for ((key, value) in record)
			{
				if (value > 1 && (key * key) == k)
				{
					// When key value is more than one.
					// And its product is equal to k
					count += 2;
				}
				else if (record.containsKey(k / key))
				{
					// When product pair exists
					count += 1;
				}
			}
			if (count > 0)
			{
				count = count / 2;
			}
		}
		// Display given array
		this.printArray(arr, n);
		// Display calculated result
		println("\n Resultant distinct pair of product (" + k + ") is :  " + count);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: DistinctProduct = DistinctProduct();
	val arr1: Array < Int > = arrayOf(-3, 2, 1, 3, 6, 0, -2, -1);
	val arr2: Array < Int > = arrayOf(4, -2, 0, 9, -1, 9, 0);
	var n: Int = arr1.count();
	// Test A
	var k: Int = 6;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 6   
	    (-3 ☓ -2) (2 ☓ 3) (1 ☓ 6)
	    -------------------------
	   Resultant pair : 3
	*/
	task.distinctPairOfKProduct(arr1, n, k);
	// Test B
	k = 2;
	/*
	    arr = [ - 3 , 2, 1, 3, 6 , 0, -2 , -1]
	    product k = 2   
	    (2 ☓ 1)  (-2 ☓ -1)
	    -------------------------
	   Resultant pair : 2
	*/
	task.distinctPairOfKProduct(arr1, n, k);
	// Test C
	// Get length of arr2
	n = arr2.count();
	k = 0;
	/*
	    arr = [  4, -2 , 0, 9 , -1, 9, 0]
	    product k = 0   
	    (4 ☓ 0) (-2 ☓ 0) (9 ☓ 0)  (-1 ☓ 0) 
	    -------------------
	    Resultant pair : 4
	*/
	task.distinctPairOfKProduct(arr2, n, k);
	// Test D
	k = 2;
	/*
	    arr = [ 4, -2 , 0, 9 , -1, 9, 0]
	    product k = 2   
	    (-2 ☓ -1)
	    -------------------
	    Resultant pair : 1
	*/
	task.distinctPairOfKProduct(arr2, n, k);
}

Output

 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (6) is :  3
 -3 2 1 3 6 0 -2 -1
 Resultant distinct pair of product (2) is :  2
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (0) is :  4
 4 -2 0 9 -1 9 0
 Resultant distinct pair of product (2) is :  1




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment