Related Articles
Sum of absolute differences of pairs from the given array that satisfy the given condition
• Last Updated : 13 Sep, 2019

Given an array arr[] of N elements, the task is to find the sum of absolute differences between all pairs (arr[i], arr[j]) such that i < j and (j – i) is prime.

Example:

Input: arr[] = {1, 2, 3, 5, 7, 12}
Output: 45
All valid index pairs are:
(5, 0) -> abs(12 – 1) = 11
(3, 0) -> abs(5 – 1) = 4
(2, 0) -> abs(3 – 1) = 2
(4, 1) -> abs(7 – 2) = 5
(3, 1) -> abs(5 – 2) = 3
(5, 2) -> abs(12 – 3) = 9
(4, 2) -> abs(7 – 3) = 4
(5, 3) -> abs(12 – 5) = 7
11 + 4 + 2 + 5 + 3 + 9 + 4 + 7 = 45

Input: arr[] = {2, 5, 6, 7}
Output: 11

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: Initialise sum = 0 and run two nested loops and for every pair arr[i], arr[j] is (j – i) is prime then update the sum as sum = sum + abs(arr[i], arr[j]). Print the sum in the end.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach``#include ``using` `namespace` `std;`` ` `// Function that returns true``// if n is prime``bool` `isPrime(``int` `n)``{`` ` `    ``// Corner case``    ``if` `(n <= 1)``        ``return` `false``;`` ` `    ``// Check from 2 to n-1``    ``for` `(``int` `i = 2; i < n; i++)``        ``if` `(n % i == 0)``            ``return` `false``;`` ` `    ``return` `true``;``}`` ` `// Function to return the absolute``// differences of the pairs which``// satisfy the given condition``int` `findSum(``int` `arr[], ``int` `n)``{`` ` `    ``// To store the required sum``    ``int` `sum = 0;`` ` `    ``for` `(``int` `i = 0; i < n - 1; i++) {``        ``for` `(``int` `j = i + 1; j < n; j++)`` ` `            ``// If difference between the indices``            ``// is prime``            ``if` `(isPrime(j - i)) {`` ` `                ``// Update the sum with the absolute``                ``// difference of the pair elements``                ``sum = sum + ``abs``(arr[i] - arr[j]);``            ``}``    ``}`` ` `    ``// Return the sum``    ``return` `sum;``}`` ` `// Driver code``int` `main()``{``    ``int` `arr[] = { 1, 2, 3, 5, 7, 12 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);`` ` `    ``cout << findSum(arr, n);`` ` `    ``return` `0;``}`

## Java

 `// Java implementation of the approach``import` `java.util.*;`` ` `class` `GFG ``{`` ` `    ``// Function that returns true``    ``// if n is prime``    ``static` `boolean` `isPrime(``int` `n) ``    ``{`` ` `        ``// Corner case``        ``if` `(n <= ``1``)``        ``{``            ``return` `false``;``        ``}`` ` `        ``// Check from 2 to n-1``        ``for` `(``int` `i = ``2``; i < n; i++) ``        ``{``            ``if` `(n % i == ``0``) ``            ``{``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}`` ` `    ``// Function to return the absolute``    ``// differences of the pairs which``    ``// satisfy the given condition``    ``static` `int` `findSum(``int` `arr[], ``int` `n) ``    ``{`` ` `        ``// To store the required sum``        ``int` `sum = ``0``;`` ` `        ``for` `(``int` `i = ``0``; i < n - ``1``; i++) ``        ``{``            ``// If difference between the indices is prime``            ``for` `(``int` `j = i + ``1``; j < n; j++) ``            ``{``                ``if` `(isPrime(j - i)) ``                ``{`` ` `                    ``// Update the sum with the absolute``                    ``// difference of the pair elements``                    ``sum = sum + Math.abs(arr[i] - arr[j]);``                ``}``            ``}``        ``}`` ` `        ``// Return the sum``        ``return` `sum;``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `main(String[] args) ``    ``{``        ``int` `arr[] = {``1``, ``2``, ``3``, ``5``, ``7``, ``12``};``        ``int` `n = arr.length;`` ` `        ``System.out.println(findSum(arr, n));``    ``}``} `` ` `// This code is contributed by Rajput-Ji`

## Python3

 `# Python3 implementation of the approach `` ` `# Function that returns true ``# if n is prime ``def` `isPrime(n) : `` ` `    ``# Corner case ``    ``if` `(n <``=` `1``) :``        ``return` `False``; `` ` `    ``# Check from 2 to n-1 ``    ``for` `i ``in` `range``(``2``, n) :``        ``if` `(n ``%` `i ``=``=` `0``) :``            ``return` `False``; `` ` `    ``return` `True``; `` ` `# Function to return the absolute ``# differences of the pairs which ``# satisfy the given condition ``def` `findSum(arr, n) : `` ` `    ``# To store the required sum ``    ``sum` `=` `0``; `` ` `    ``for` `i ``in` `range``(n ``-` `1``) :``        ``for` `j ``in` `range``(i ``+` `1``, n) : `` ` `            ``# If difference between the indices ``            ``# is prime ``            ``if` `(isPrime(j ``-` `i)) :`` ` `                ``# Update the sum with the absolute ``                ``# difference of the pair elements ``                ``sum` `=` `sum` `+` `abs``(arr[i] ``-` `arr[j]); `` ` `    ``# Return the sum ``    ``return` `sum``; `` ` `# Driver code ``if` `__name__ ``=``=` `"__main__"` `: `` ` `    ``arr ``=` `[ ``1``, ``2``, ``3``, ``5``, ``7``, ``12` `];``    ``n ``=` `len``(arr); `` ` `    ``print``(findSum(arr, n)); `` ` `# This code is contributed by AnkitRai01`

## C#

 `// C# implementation of the approach``using` `System;`` ` `class` `GFG ``{`` ` `    ``// Function that returns true``    ``// if n is prime``    ``static` `bool` `isPrime(``int` `n) ``    ``{`` ` `        ``// Corner case``        ``if` `(n <= 1)``        ``{``            ``return` `false``;``        ``}`` ` `        ``// Check from 2 to n-1``        ``for` `(``int` `i = 2; i < n; i++) ``        ``{``            ``if` `(n % i == 0) ``            ``{``                ``return` `false``;``            ``}``        ``}``        ``return` `true``;``    ``}`` ` `    ``// Function to return the absolute``    ``// differences of the pairs which``    ``// satisfy the given condition``    ``static` `int` `findSum(``int` `[]arr, ``int` `n) ``    ``{`` ` `        ``// To store the required sum``        ``int` `sum = 0;`` ` `        ``for` `(``int` `i = 0; i < n - 1; i++) ``        ``{``            ``// If difference between the indices is prime``            ``for` `(``int` `j = i + 1; j < n; j++) ``            ``{``                ``if` `(isPrime(j - i)) ``                ``{`` ` `                    ``// Update the sum with the absolute``                    ``// difference of the pair elements``                    ``sum = sum + Math.Abs(arr[i] - arr[j]);``                ``}``            ``}``        ``}`` ` `        ``// Return the sum``        ``return` `sum;``    ``}`` ` `    ``// Driver code``    ``public` `static` `void` `Main(String[] args) ``    ``{``        ``int` `[]arr = {1, 2, 3, 5, 7, 12};``        ``int` `n = arr.Length;`` ` `        ``Console.WriteLine(findSum(arr, n));``    ``}``} `` ` `// This code is contributed by PrinciRaj1992`
Output:
```45
```

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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up