Sum of absolute differences of pairs from the given array that satisfy the given condition
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 |
45
Recommended Posts:
- Pairs from an array that satisfy the given condition
- Sum of absolute differences of all pairs in a given array
- Count index pairs which satisfy the given condition
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition
- Minimum and Maximum sum of absolute differences of pairs
- Array element with minimum sum of absolute differences
- Find all pairs (a,b) and (c,d) in array which satisfy ab = cd
- Count sub-sets that satisfy the given condition
- Count pairs in an array such that the absolute difference between them is ≥ K
- Sum of bit differences among all pairs
- Choose X elements from A[] and Y elements from B[] which satisfy the given condition
- Minimum sum of absolute difference of pairs of two arrays
- Remove elements to make array satisfy arr[ i+1] < arr[i] for each valid i
- Find the maximum cost of an array of pairs choosing at most K pairs
- Minimum sum of differences with an element in an array
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.



