Product of all the pairs from the given array
Last Updated :
15 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int productPairs( int arr[], int n)
{
int product = 1;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
product *= (arr[i] % mod
* arr[j] % mod)
% mod;
product = product % mod;
}
}
return product % mod;
}
int main()
{
int arr[] = { 1, 2, 3 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << productPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int mod = 1000000007 ;
static int productPairs( int arr[], int n)
{
int product = 1 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
product *= (arr[i] % mod *
arr[j] % mod) % mod;
product = product % mod;
}
}
return product % mod;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.print(productPairs(arr, n));
}
}
|
Python3
mod = 1000000007 ;
def productPairs(arr, n):
product = 1 ;
for i in range (n):
for j in range (n):
product * = (arr[i] % mod *
arr[j] % mod) % mod;
product = product % mod;
return product % mod;
if __name__ = = '__main__' :
arr = [ 1 , 2 , 3 ];
n = len (arr);
print (productPairs(arr, n));
|
C#
using System;
class GFG{
static readonly int mod = 1000000007;
static int productPairs( int []arr, int n)
{
int product = 1;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
product *= (arr[i] % mod *
arr[j] % mod) % mod;
product = product % mod;
}
}
return product % mod;
}
public static void Main(String[] args)
{
int []arr = { 1, 2, 3 };
int n = arr.Length;
Console.Write(productPairs(arr, n));
}
}
|
Javascript
<script>
mod = 1000000007
function productPairs(arr, n)
{
let product = 1;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
product *= (arr[i] % mod
* arr[j] % mod)
% mod;
product = product % mod;
}
}
return product % mod;
}
let arr = [ 1, 2, 3 ];
let n = arr.length;
document.write(productPairs(arr, n));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long int
int power( int x, unsigned int y)
{
int p = 1000000007;
int res = 1;
x = x % p;
while (y > 0) {
if (y & 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
ll productPairs(ll arr[], ll n)
{
ll product = 1;
for ( int i = 0; i < n; i++) {
product
= (product
% mod
* ( int )power(
arr[i], (2 * n))
% mod)
% mod;
}
return product % mod;
}
int main()
{
ll arr[] = { 1, 2, 3 };
ll n = sizeof (arr) / sizeof (arr[0]);
cout << productPairs(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static final int mod = 1000000007 ;
static int power( int x, int y)
{
int p = 1000000007 ;
int res = 1 ;
x = x % p;
while (y > 0 )
{
if (y % 2 == 1 )
res = (res * x) % p;
y = y >> 1 ;
x = (x * x) % p;
}
return res;
}
static int productPairs( int arr[], int n)
{
int product = 1 ;
for ( int i = 0 ; i < n; i++)
{
product = (product % mod *
( int )power(arr[i],
( 2 * n)) % mod) % mod;
}
return product % mod;
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 };
int n = arr.length;
System.out.print(productPairs(arr, n));
}
}
|
Python3
mod = 1000000007
def power(x, y):
p = 1000000007
res = 1
x = x % p
while (y > 0 ):
if ((y & 1 ) ! = 0 ):
res = (res * x) % p
y = y >> 1
x = (x * x) % p
return res
def productPairs(arr, n):
product = 1
for i in range (n):
product = (product % mod *
( int )(power(arr[i], ( 2 * n))) %
mod) % mod
return (product % mod)
arr = [ 1 , 2 , 3 ]
n = len (arr)
print (productPairs(arr, n))
|
C#
using System;
class GFG{
const int mod = 1000000007;
static int power( int x, int y)
{
int p = 1000000007;
int res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static int productPairs( int []arr, int n)
{
int product = 1;
for ( int i = 0; i < n; i++)
{
product = (product % mod *
( int )power(arr[i],
(2 * n)) % mod) % mod;
}
return product % mod;
}
public static void Main()
{
int []arr = { 1, 2, 3 };
int n = arr.Length;
Console.Write(productPairs(arr, n));
}
}
|
Javascript
<script>
let mod = 1000000007;
function power(x, y)
{
let p = 1000000007;
let res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
function productPairs(arr, n)
{
let product = 1;
for (let i = 0; i < n; i++)
{
product = (product % mod *
power(arr[i],
(2 * n)) % mod) % mod;
}
return product % mod;
}
let arr = [ 1, 2, 3 ];
let n = arr.length;
document.write(productPairs(arr, n));
</script>
|
Time Complexity: O(N)
Space Complexity: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...