Open In App

Product of all the pairs from the given array

Last Updated : 15 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given an array arr[] of N integers, the task is to find the product of all the pairs possible from the given array such as: 

  • (arr[i], arr[i]) is also considered as a valid pair.
  • (arr[i], arr[j]) and (arr[j], arr[i]) are considered as two different pairs.

Print the resultant answer modulus 10^9+7.

Examples:  

Input: arr[] = {1, 2} 
Output: 16 
Explanation: 
All valid pairs are (1, 1), (1, 2), (2, 1) and (2, 2). 
Hence, 1 * 1 * 1 * 2 * 2 * 1 * 2 * 2 = 16

Input: arr[] = {1, 2, 3} 
Output: 46656 
Explanation: 
All valid pairs are (1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2) and (3, 3). 
Hence the product is 1*1*1**2*1*3*2*1*2*2*2*3*3*1*3*2*3*3 = 46656 

Naive Approach: 

  • Initialize the product variable.
  • Run a two loops to find all the possible pairs.
  • Calculate of the elements of each pair.
  • Return the final product.

Below is the implementation of the above approach:  

C++




// C++ implementation to find the
// product of all the pairs from
// the given array
 
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
 
// Function to return the product of
// the elements of all possible pairs
// from the array
int productPairs(int arr[], int n)
{
 
    // To store the required product
    int product = 1;
 
    // Nested loop to calculate all
    // possible pairs
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
 
            // Multiply the product of
            // the elements of the
            // current pair
            product *= (arr[i] % mod
                        * arr[j] % mod)
                       % mod;
            product = product % mod;
        }
    }
 
    // Return the final result
    return product % mod;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 3 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << productPairs(arr, n);
 
    return 0;
}


Java




// Java implementation to find the
// product of all the pairs from
// the given array
import java.util.*;
 
class GFG{
     
static final int mod = 1000000007;
 
// Function to return the product of
// the elements of all possible pairs
// from the array
static int productPairs(int arr[], int n)
{
 
    // To store the required product
    int product = 1;
 
    // Nested loop to calculate all
    // possible pairs
    for(int i = 0; i < n; i++)
    {
       for(int j = 0; j < n; j++)
       {
           
          // Multiply the product
          // of the elements of the
          // current pair
          product *= (arr[i] % mod *
                      arr[j] % mod) % mod;
          product = product % mod;
       }
    }
 
    // Return the final result
    return product % mod;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
 
    System.out.print(productPairs(arr, n));
}
}
 
// This code is contributed by sapnasingh4991


Python3




# Python3 implementation to find the
# product of all the pairs from
# the given array
mod = 1000000007;
 
# Function to return the product of
# the elements of all possible pairs
# from the array
def productPairs(arr, n):
   
    # To store the required product
    product = 1;
 
    # Nested loop to calculate all
    # possible pairs
    for i in range(n):
        for j in range(n):
           
            # Multiply the product
            # of the elements of the
            # current pair
            product *= (arr[i] % mod *
                        arr[j] % mod) % mod;
            product = product % mod;
 
    # Return the final result
    return product % mod;
 
# Driver code
if __name__ == '__main__':
    arr = [1, 2, 3];
    n = len(arr);
 
    print(productPairs(arr, n));
 
# This code is contributed by 29AjayKumar


C#




// C# implementation to find the
// product of all the pairs from
// the given array
using System;
class GFG{
     
static readonly int mod = 1000000007;
 
// Function to return the product of
// the elements of all possible pairs
// from the array
static int productPairs(int []arr, int n)
{
 
    // To store the required product
    int product = 1;
 
    // Nested loop to calculate all
    // possible pairs
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
        {
                 
            // Multiply the product
            // of the elements of the
            // current pair
            product *= (arr[i] % mod *
                        arr[j] % mod) % mod;
            product = product % mod;
        }
    }
 
    // Return the readonly result
    return product % mod;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
 
    Console.Write(productPairs(arr, n));
}
}
 
// This code is contributed by sapnasingh4991


Javascript




<script>
 
//Javascript implementation to find the
// product of all the pairs from
// the given array
 
mod = 1000000007
 
// Function to return the product of
// the elements of all possible pairs
// from the array
function productPairs(arr, n)
{
 
    // To store the required product
    let product = 1;
 
    // Nested loop to calculate all
    // possible pairs
    for (let i = 0; i < n; i++) {
        for (let j = 0; j < n; j++) {
 
            // Multiply the product of
            // the elements of the
            // current pair
            product *= (arr[i] % mod
                        * arr[j] % mod)
                    % mod;
            product = product % mod;
        }
    }
 
    // Return the final result
    return product % mod;
}
 
// Driver code
    let arr = [ 1, 2, 3 ];
 
    let n = arr.length;
 
    document.write(productPairs(arr, n));
 
// This code is contributed by Mayank Tyagi
 
</script>


Output

46656

Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient approach:

We can observe that each element appears exactly (2 * N) times as one of the elements of a pair (X, Y). Exactly N times as X and exactly N times as Y.

Below is the implementation of the above approach:  

C++




// C++ implementation to Find the product
// of all the pairs from the given array
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
 
// Function to calculate
// (x^y)%1000000007
int power(int x, unsigned int y)
{
    int p = 1000000007;
 
    // Initialize result
    int res = 1;
 
    // Update x if it is more than
    // or equal to p
    x = x % p;
 
    while (y > 0) {
        // If y is odd, multiply x
        // with result
        if (y & 1)
            res = (res * x) % p;
 
        y = y >> 1;
        x = (x * x) % p;
    }
 
    // Return the final result
    return res;
}
 
// Function to return the product
// of the elements of all possible
// pairs from the array
ll productPairs(ll arr[], ll n)
{
 
    // To store the required product
    ll product = 1;
 
    // Iterate for every element
    // of the array
    for (int i = 0; i < n; i++) {
 
        // Each element appears (2 * n) times
        product
            = (product
               % mod
               * (int)power(
                     arr[i], (2 * n))
               % mod)
              % mod;
    }
 
    return product % mod;
}
 
// Driver code
int main()
{
    ll arr[] = { 1, 2, 3 };
    ll n = sizeof(arr) / sizeof(arr[0]);
 
    cout << productPairs(arr, n);
 
    return 0;
}


Java




// Java implementation to Find the product
// of all the pairs from the given array
import java.util.*;
 
class GFG{
static final int mod = 1000000007;
 
// Function to calculate
// (x^y)%1000000007
static int power(int x, int y)
{
    int p = 1000000007;
 
    // Initialize result
    int res = 1;
 
    // Update x if it is more than
    // or equal to p
    x = x % p;
 
    while (y > 0)
    {
         
        // If y is odd, multiply x
        // with result
        if (y % 2 == 1)
            res = (res * x) % p;
 
        y = y >> 1;
        x = (x * x) % p;
    }
 
    // Return the final result
    return res;
}
 
// Function to return the product
// of the elements of all possible
// pairs from the array
static int productPairs(int arr[], int n)
{
 
    // To store the required product
    int product = 1;
 
    // Iterate for every element
    // of the array
    for (int i = 0; i < n; i++)
    {
 
        // Each element appears (2 * n) times
        product = (product % mod *
                  (int)power(arr[i],
                            (2 * n)) % mod) % mod;
    }
 
    return product % mod;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3 };
    int n = arr.length;
 
    System.out.print(productPairs(arr, n));
}
}
 
// This code is contributed by amal kumar choubey


Python3




# Python3 implementation to Find the product
# of all the pairs from the given array
mod = 1000000007
 
# Function to calculate
# (x^y)%1000000007
def power(x, y):
 
    p = 1000000007
 
    # Initialize result
    res = 1
 
    # Update x if it is more than
    # or equal to p
    x = x % p
 
    while (y > 0):
         
        # If y is odd, multiply x
        # with result
        if ((y & 1) != 0):
            res = (res * x) % p
 
        y = y >> 1
        x = (x * x) % p
 
    # Return the final result
    return res
 
# Function to return the product
# of the elements of all possible
# pairs from the array
def productPairs(arr, n):
 
    # To store the required product
    product = 1
 
    # Iterate for every element
    # of the array
    for i in range(n):
 
        # Each element appears (2 * n) times
        product = (product % mod *
          (int)(power(arr[i], (2 * n))) %
                            mod) % mod
 
    return (product % mod)
     
# Driver code
arr = [ 1, 2, 3 ]
n = len(arr)
 
print(productPairs(arr, n))
 
# This code is contributed by divyeshrabadiya07


C#




// C# implementation to Find the product
// of all the pairs from the given array
using System;
class GFG{
const int mod = 1000000007;
 
// Function to calculate
// (x^y)%1000000007
static int power(int x, int y)
{
    int p = 1000000007;
 
    // Initialize result
    int res = 1;
 
    // Update x if it is more than
    // or equal to p
    x = x % p;
 
    while (y > 0)
    {
         
        // If y is odd, multiply x
        // with result
        if (y % 2 == 1)
            res = (res * x) % p;
 
        y = y >> 1;
        x = (x * x) % p;
    }
 
    // Return the final result
    return res;
}
 
// Function to return the product
// of the elements of all possible
// pairs from the array
static int productPairs(int []arr, int n)
{
 
    // To store the required product
    int product = 1;
 
    // Iterate for every element
    // of the array
    for (int i = 0; i < n; i++)
    {
 
        // Each element appears (2 * n) times
        product = (product % mod *
                  (int)power(arr[i],
                            (2 * n)) % mod) % mod;
    }
 
    return product % mod;
}
 
// Driver code
public static void Main()
{
    int []arr = { 1, 2, 3 };
    int n = arr.Length;
 
    Console.Write(productPairs(arr, n));
}
}
 
// This code is contributed by Code_Mech


Javascript




<script>
 
// Javascript implementation to Find the product
// of all the pairs from the given array
  
let mod = 1000000007;
 
// Function to calculate
// (x^y)%1000000007
function power(x, y)
{
    let p = 1000000007;
  
    // Initialize result
    let res = 1;
  
    // Update x if it is more than
    // or equal to p
    x = x % p;
  
    while (y > 0)
    {
          
        // If y is odd, multiply x
        // with result
        if (y % 2 == 1)
            res = (res * x) % p;
  
        y = y >> 1;
        x = (x * x) % p;
    }
  
    // Return the final result
    return res;
}
  
// Function to return the product
// of the elements of all possible
// pairs from the array
function productPairs(arr, n)
{
  
    // To store the required product
    let product = 1;
  
    // Iterate for every element
    // of the array
    for (let i = 0; i < n; i++)
    {
  
        // Each element appears (2 * n) times
        product = (product % mod *
                  power(arr[i],
                            (2 * n)) % mod) % mod;
    }
  
    return product % mod;
}
  
  // Driver Code
     
    let arr = [ 1, 2, 3 ];
    let n = arr.length;
  
    document.write(productPairs(arr, n));
     
</script>


Output

46656

Time Complexity: O(N)

Space Complexity: O(1)
 



Similar Reads

Find indices of K largest pairs in decreasing order of product from given Array of Pairs
Given an array arr[] of N integer pairs and an integer K, the task is to find the indices of the K largest pairs in decreasing order of the product. Example: Input: arr[] = {{9, 1}, {6, 3}, {6, 8}, {4, 5}, {1, 8}}, K = 1Output: 2Explanation: The pair with the largest product is at index 2 i.e, {6, 8}. Input: arr[] = {{1, 2}, {1, 4}, {1, 3}, {1, 5}}
6 min read
Given an array of pairs, find all symmetric pairs in it
Two pairs (a, b) and (c, d) are said to be symmetric if c is equal to b and a is equal to d. For example, (10, 20) and (20, 10) are symmetric. Given an array of pairs find all symmetric pairs in it. It may be assumed that the first elements of all pairs are distinct.Example: Input: arr[] = {{11, 20}, {30, 40}, {5, 10}, {40, 30}, {10, 5}}Output: Fol
14 min read
Count all possible pairs in given Array with product K
Given an integer array arr[] of size N and a positive integer K, the task is to count all the pairs in the array with a product equal to K. Examples: Input: arr[] = {1, 2, 16, 4, 4, 4, 8 }, K=16Output: 5Explanation: Possible pairs are (1, 16), (2, 8), (4, 4), (4, 4), (4, 4) Input: arr[] = {1, 10, 20, 10, 4, 5, 5, 2 }, K=20Output: 5Explanation: Poss
11 min read
Count pairs from a given array whose product lies in a given range
Given an array arr[] of size N, and integers L and R, the task is to count the number of pairs [arri , arrj] such that i &lt; j and the product of arr[i] * arr[j] lies in the given range [L, R], i.e. L ? arr[i] * arr[j] ? R. Examples: Input: arr[ ] = {4, 1, 2, 5}, L = 4, R = 9Output: 3Explanation: Valid pairs are {4, 1}, {1, 5} and {4, 2}. Input: a
13 min read
Maximize product of array by replacing array elements with its sum or product with element from another array
Given two arrays A[] and B[] consisting of N integers, the task is to update array A[] by assigning every array element A[i] to a single element B[j] and update A[i] to A[i] + B[j] or A[i] * B[j], such that the product of the array A[] is maximized. Note: Every array element in both the arrays can be paired with a single element from the other arra
7 min read
Count pairs with product of indices equal to the product of elements present at those indices
Given an array arr[] consisting of N integers, the task is to count the number of distinct pairs of array elements having the product of indices equals the product of elements at that indices. The pairs (x, y) and (y, x) are considered as the same pairs. Examples: Input: arr[] = {1, 0, 3, 2, 6}Output: 3Explanation: All possible pairs satisfying the
5 min read
Minimize sum of absolute difference between all pairs of array elements by decrementing and incrementing pairs by 1
Given an array arr[] ( 1-based indexing ) consisting of N integers, the task is to find the minimum sum of the absolute difference between all pairs of array elements by decrementing and incrementing any pair of elements by 1 any number of times. Examples: Input: arr[] = {1, 2, 3}Output: 0Explanation:Modify the array elements by performing the foll
5 min read
Sum of product of all unordered pairs in given range with update queries
Given an array A[] consisting of N integers and Q queries of the below types, the task is to print the output for all the update queries. (1, L, R): The 1st type of query to find the sum of the product of all unordered pairs from index L to R in the array where 1 &lt;= L &lt;= R &lt;= N.(2, P, X): The 2nd type of query to change the value of the Pt
21 min read
For all Array elements find Product of Sum of all smaller and Sum of all greater elements
Given an array arr[] of integers of length N, the task is to find the product of the sum of all the numbers larger than that number with the sum of all the numbers less than that number for each number in the array. Examples: Input: arr[] = {8, 4, 9, 3}, N = 4Output:- 63, 51, 0, 0Explanation:For first number 8: Sum of elements smaller than this is
15 min read
Sum of product of all pairs of a Binary Array
Given a binary array arr[] of size N, the task is to print the sum of product of all pairs of the given array elements. Note: The binary array contains only 0 and 1. Examples: Input: arr[] = {0, 1, 1, 0, 1}Output: 3Explanation: Sum of product of all possible pairs are: {0 × 1 + 0 × 1 + 0 × 0 + 0 × 1 + 1 × 1 + 1 × 0 + 1 × 1 + 1 × 0 + 1 × 1 + 0 × 1}.
5 min read