Java Program to Find All Pairs of Elements in an Array Whose Product is Equal to a Specified Number

In the previous article, we have seen Java Program to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number

In this article we will see how to find all pairs of elements in an array whose product is equal to a specified number.

Java Program to Find All Pairs of Elements in an Array Whose Product is Equal to a Specified Number

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to Find All Pairs of Elements in an Array Whose product is Equal to a Specified Number.

Method-1: Java Program to Find All Pairs of Elements in an Array Whose Product is Equal to a Specified Number By Using Brute force approach and Static Input

Approach:

  • In this method we will use two nested loops, one for traversing the array and another to check if there’s another number in the array which can be added to get the product.
  • During iteration if any pairs is found whose product is equal to the specified number.
  • Then print that pair.

Program:

public class Array 
{
    public static void main(String[] args) 
    {
        //An array declared and initialized
        int[] arr = new int[] {10, 2, 5, 8, 11, 4, 3, 6, 15};
        //A number specified/declared
        int product = 30;
        //calling findPairs() user defined method
        findPairs(arr, product);
    }
    
    //findPairs() method
    //it will find all the pairs whose product is equal to a specified number
    static void findPairs(int inputArray[], int product) 
    {
        System.out.println("The pairs whose product are equal to "+ product+" are : ");
        for (int i = 0; i < inputArray.length; i++) 
        {
            for (int j = i + 1; j < inputArray.length; j++) 
            {
                if (inputArray[i] * inputArray[j] == product) 
                {
                    System.out.println(inputArray[i] + " " + inputArray[j]);
                }
            }
        }
    }
}
Output:

The pairs whose product are equal to 30 are : 
10 3
2 15
5 6

Method-2: Java Program to Find All Pairs of Elements in an Array Whose Product is Equal to a Specified Number By Using Brute force approach and Static Input

Approach:

  • First declare the size of array, then take the input of array elements and a specified number as product.
  • In this method we will use two nested loops, one for traversing the array and another to check if there’s another number in the array which can be added to get the product.
  • During iteration if any pairs is found whose product is equal to the specified number.
  • Then print that pair.

Program:

import java.util.Scanner;

public class FindPair  
{
    //Driver method
    public static void main(String[] args) 
    {
        java.util.Scanner sc = new Scanner(System.in);
        //Entering the size of array
        System.out.println("Enter number of elements in the array: ");
        int n = sc.nextInt();
        
        //array declared
        int[] arr = new int[n];
        
        //Entering array elements
        System.out.println("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
               arr[i] = sc.nextInt();
        }
        //Entering the product value, a specified number
        System.out.println("Enter the product value: ");
        int product = sc.nextInt();
        
        //calling the findPairs() user defined method
        findPairs(arr, product);
    }
    
    
    //findPairs() method
    //it will find all the pairs whose product is equal to a specified number
    static void findPairs(int inputArray[], int product)
    {
        System.out.println("The pairs whose product are equal to "+ product+" are : ");
        for (int i = 0; i < inputArray.length; i++) 
        {
            for (int j = i + 1; j < inputArray.length; j++) 
            {
                if (inputArray[i] * inputArray[j] == product) 
                {
                    System.out.println(inputArray[i] + " " + inputArray[j]);
                }
            }
        }
    }
}
Output:

Enter number of elements in the array: 7
Enter array elements: 10 3 2 6 5 18 11 
Enter the product value: 30
The pairs whose product are equal to 30 are : 
10 3
6 5

Method-3: Java Program to Find All Pairs of Elements in an Array Whose Sum is Equal to a Specified Number By Using HashMap

Approach:

  • Initialize the HashMap.
  • Iterate over the array.
  • Check in the HashMap if (product / arr[i]) is present as key and its value is 0(Value 0 to avoid getting same pair twice).
  • If yes, print the pair and update the value as 1.
  • Else, update the HashMap with key as the current element and value as 0.

Program:

import java.util.HashMap;

public class FindPairs 
{
    //Driver method
     public static void main(String[] args) 
    {
        //Array declared and initialized
        int[] arr = new int[] {2, 7, 5, 4, 10, 9, 15};
        //A number declared as product
        int product = 20;
        //findPairsHashing() method called
        findPairsHashing(arr, product);

    }
    
    //findPairsHashing() method 
    //it will find all the pairs whose product is equal to a specified number
    public static void findPairsHashing(int arr[], int product) 
    {
        System.out.println("The pairs whose product equal to "+ product+" are : ");
        HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
        for (int i = 0; i < arr.length; i++) 
        {
            for ( i = 0; i < arr.length; i++) 
            {
                if (product % arr[i] == 0) 
                {
                    if (map.containsKey(product / arr[i]) && map.get(product / arr[i]) == 0) 
                    {
                        System.out.println(arr[i] + " " + (product / arr[i]));
                        map.replace(product - arr[i], 1);
                    } else {
                        map.put(arr[i], 0);
                    }
                }
            }
        }

    }
}
Output:

The pairs whose product equal to 20 are : 
4 5
10 2

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Related Java Programs: