Count pairs whose products exist in array in C++

C++Server Side ProgrammingProgramming

We are given an array of integer type elements and the task is to form the pairs from the given array and calculate the product of elements in the pair and check whether the given product is present in the given array or not.

Input − int arr[] = {6, 2, 3, 1, 5, 10}

Output − Count of pairs whose products exist in same array are − 7

Explanation − The pairs that can be formed from the given array are: (6, 2), (6, 3), (6, 1), (6, 5), (6, 10), (2, 3), (2, 1), (2, 5), (2, 10), (3, 1), (3, 5), (3, 10), (1, 5), (1, 10), (5, 10). So the pairs with the given product value in the same array are (2, 3) as 6, (6, 1) as 6, (3, 1) as 3, (2, 5) as 10, (1, 5) as 5, (2, 1) as 2, (1, 10 ) as 10.

Input − int arr[] = {2, 4, 8, 5, 10}

Output − Count of pairs whose products exist in same array are − 2

Explanation − The pairs that can be formed from the given array are: (2, 4), (2, 8), (2, 5), (2, 10), (4, 8), (4, 5), (4, 10), (8, 5), (8, 10), (5, 10). So the pairs with the given product value in the same array are (2, 4) as 8, (5, 2) as 10.

Approach used in the below program is as follows

There can be multiple approaches to solve the given problem i.e. naive approach and efficient approach. So let’s first look at the naive approach.

  • Input an array of integer elements and calculate the size of an array and pass the data to the function

  • Declare a temporary variable count to store the count of pairs with the product value to be available in the given array.

  • Start loop FOR from i to 0 till the size of an array

  • Inside the loop, start another loop FOR from j to i + 1 till the size of an array

  • Inside the loop calculate the product as arr[i] * arr[j]

  • Start another loop FOR from k to 0 till the size of an array

  • Inside the K loop, check IF product = arr[k] then increment the count by 1

  • Return the count

  • Print result.

Efficient approach

  • Input an array of integer elements and calculate the size of an array and pass the data to the function

  • Declare a temporary variable count to store the count of pairs with the product value to be available in the given array.

  • Create a variable of type STL set as pro

  • Start loop FOR from i to 0 till the size of an array

  • Inside the loop, insert arr[i] in the set variable pro

  • Start another loop FOR from i to 0 till the size of an array

  • Inside the loop, start another loop FOR from j to i + 1 till the size of an array

  • Set product as arr[i] * arr[j]

  • Check IF pro.find(product) != pro.end() then increment the count by 1

  • Return the count

  • Print the result.

Example (naive approach)

 Live Demo

#include <bits/stdc++.h>
using namespace std;
int product_pair(int arr[], int size){
   int product = 1;
   int count = 0;
   for(int i = 0 ; i<size ; i++){
      for(int j = i+1;j<size;j++){
         product = arr[i] * arr[j];
         for(int pro = 0 ; pro < size; pro++){
            if(product == arr[pro]){
               count++;
            }
         }
      }
   }
   return count;
}
int main(){
   int arr[] = {6, 2, 3, 1, 5, 10};
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Count of pairs whose products exist in same array are: "<<product_pair(arr,size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of pairs whose products exist in same array are: 7

Example (Efficient Approach)

 Live Demo

#include<bits/stdc++.h>
using namespace std;
int product_pair(int arr[], int size){
   set< int > pro;
   int count = 0;
   int product = 1;
   for (int i = 0 ; i < size; i++){
      pro.insert(arr[i]);
   }
   for (int i = 0 ; i < size; i++){
      for (int j = i + 1; j < size ; j++){
         product = arr[i] * arr[j];
         if(pro.find(product) != pro.end()){
            count++;
         }
      }
   }
   return count;
}
int main(){
   int arr[] = {6, 2, 3, 1, 5, 10};
   int size = sizeof(arr)/sizeof(arr[0]);
   cout<<"Count of pairs whose products exist in same array are: "<<product_pair(arr,size);
   return 0;
}

Output

If we run the above code it will generate the following output −

Count of pairs whose products exist in same array are: 7
raja
Published on 31-Oct-2020 05:57:16
Advertisements