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
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 <iostream> 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[0]); 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 |
Javascript
<script> // JS implementation of the approach // Function that returns true // if n is prime function isPrime(n) { // Corner case if (n <= 1) return false ; // Check from 2 to n-1 for (let 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 function findSum( arr, n) { // To store the required sum let sum = 0; for (let i = 0; i < n - 1; i++) { for (let 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 + Math.abs(arr[i] - arr[j]); } } // Return the sum return sum; } // Driver code let arr = [ 1, 2, 3, 5, 7, 12 ]; let n = arr.length; document.write(findSum(arr, n)); </script> |
45
Time Complexity: O(N3)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!