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: 10 
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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

12


 

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.