📌  相关文章
📜  满足给定条件的给定数组中对的绝对差之和

📅  最后修改于: 2021-04-29 08:26:51             🧑  作者: Mango

给定N个元素的数组arr [] ,任务是找到所有对(arr [i],arr [j])之间的绝对差之和,以使i (j – i)为质数

例子:

方法:初始化sum = 0并运行两个嵌套循环,对于每对arr [i],arr [j](j – i)为素数,然后将总和更新为sum = sum + abs(arr [i],arr [ j])。最后打印总和

下面是上述方法的实现:

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