GeeksforGeeks App
Open App
Browser
Continue

# Product of absolute difference of every pair in given Array

Given an array arr[] of N elements, the task is to find the product of absolute differences of all pairs in the given array.

Examples:

Input: arr[] = {1, 2, 3, 4}
Output: 12
Explanation:
Product of |2-1| * |3-1| * |4-1| * |3-2| * |4-2| * |4-3| = 12
Input: arr[] = {1, 8, 9, 15, 16}
Output: 27659520

Approach: The idea is to generate every possible pairs of the given array arr[] and find the product of the absolute difference of all the pairs.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to return the product of``// abs diff of all pairs (x, y)``int` `getProduct(``int` `a[], ``int` `n)``{``    ``// To store product``    ``int` `p = 1;` `    ``// Iterate all possible pairs``    ``for` `(``int` `i = 0; i < n; i++) {` `        ``for` `(``int` `j = i + 1; j < n; j++) {` `            ``// Find the product``            ``p *= ``abs``(a[i] - a[j]);``        ``}``    ``}` `    ``// Return product``    ``return` `p;``}` `// Driver Code``int` `main()``{``    ``// Given array arr[]``    ``int` `arr[] = { 1, 2, 3, 4 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);` `    ``// Function Call``    ``cout << getProduct(arr, N);``    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{``    ` `// Function to return the product of``// abs diff of all pairs (x, y)``static` `int` `getProduct(``int` `a[], ``int` `n)``{``    ` `    ``// To store product``    ``int` `p = ``1``;` `    ``// Iterate all possible pairs``    ``for``(``int` `i = ``0``; i < n; i++)``    ``{``        ``for``(``int` `j = i + ``1``; j < n; j++)``        ``{` `            ``// Find the product``            ``p *= Math.abs(a[i] - a[j]);``        ``}``    ``}` `    ``// Return product``    ``return` `p;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ` `    ``// Given array arr[]``    ``int` `arr[] = { ``1``, ``2``, ``3``, ``4` `};``    ``int` `N = arr.length;` `    ``// Function call``    ``System.out.println(getProduct(arr, N));``}``}` `// This code is contributed by Ritik Bansal`

## Python3

 `# Python3 program for``# the above approach` `# Function to return the product of``# abs diff of all pairs (x, y)``def` `getProduct(a, n):` `    ``# To store product``    ``p ``=` `1``    ` `    ``# Iterate all possible pairs``    ``for` `i ``in` `range` `(n):``        ``for` `j ``in` `range` `(i ``+` `1``, n):``          ` `            ``# Find the product``            ``p ``*``=` `abs``(a[i] ``-` `a[j])``            ` `    ``# Return product``    ``return` `p``  ` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``# Given array arr[]``    ``arr ``=` `[``1``, ``2``, ``3``, ``4``]``    ``N ``=` `len``(arr)``    ` `    ``# Function Call``    ``print` `(getProduct(arr, N))``  ` `# This code is contributed by Chitranayal`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{``    ` `// Function to return the product of``// abs diff of all pairs (x, y)``static` `int` `getProduct(``int` `[]a, ``int` `n)``{``    ` `    ``// To store product``    ``int` `p = 1;` `    ``// Iterate all possible pairs``    ``for``(``int` `i = 0; i < n; i++)``    ``{``        ``for``(``int` `j = i + 1; j < n; j++)``        ``{` `            ``// Find the product``            ``p *= Math.Abs(a[i] - a[j]);``        ``}``    ``}` `    ``// Return product``    ``return` `p;``}` `// Driver Code``public` `static` `void` `Main(``string``[] args)``{``    ` `    ``// Given array arr[]``    ``int` `[]arr = { 1, 2, 3, 4 };``    ``int` `N = arr.Length;` `    ``// Function call``    ``Console.Write(getProduct(arr, N));``}``}` `// This code is contributed by rutvik_56`

## Javascript

 ``

Output:

`12`

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

My Personal Notes arrow_drop_up