给定N个元素的数组arr [] ,任务是找到所有对(arr [i],arr [j])之间的绝对差之和,以使i
例子:
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
方法:初始化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