给定一个由N个整数组成的未排序数组A, 返回的F最大值(I,J)对所有1≤I,J≤N.
f(i,j)或数组A的两个元素的绝对差定义为| A [i] – A [j] | + | i – j | ,其中| A |表示
A的绝对值。
例子:
We will calculate the value of f(i, j) for each pair
of (i, j) and return the maximum value thus obtained.
Input : A = {1, 3, -1}
Output : 5
f(1, 1) = f(2, 2) = f(3, 3) = 0
f(1, 2) = f(2, 1) = |1 - 3| + |1 - 2| = 3
f(1, 3) = f(3, 1) = |1 - (-1)| + |1 - 3| = 4
f(2, 3) = f(3, 2) = |3 - (-1)| + |2 - 3| = 5
So, we return 5.
Input : A = {3, -2, 5, -4}
Output : 10
f(1, 1) = f(2, 2) = f(3, 3) = f(4, 4) = 0
f(1, 2) = f(2, 1) = |3 - (-2)| + |1 - 2| = 6
f(1, 3) = f(3, 1) = |3 - 5| + |1 - 3| = 4
f(1, 4) = f(4, 1) = |3 - (-4)| + |1 - 4| = 10
f(2, 3) = f(3, 2) = |(-2) - 5| + |2 - 3| = 8
f(2, 4) = f(4, 2) = |(-2) - (-4)| + |2 - 4| = 4
f(3, 4) = f(4, 3) = |5 - (-4)| + |3 - 4| = 10
So, we return 10
天真的暴力方法是通过迭代所有这样的对(i,j)并计算最大绝对差来计算值f(i,j),这将在下面实现。
C++
// Brute force C++ program to calculate the
// maximum absolute difference of an array.
#include
using namespace std;
int calculateDiff(int i, int j, int arr[])
{
// Utility function to calculate
// the value of absolute difference
// for the pair (i, j).
return abs(arr[i] - arr[j]) + abs(i - j);
}
// Function to return maximum absolute
// difference in brute force.
int maxDistance(int arr[], int n)
{
// Variable for storing the maximum
// absolute distance throughout the
// traversal of loops.
int result = 0;
// Iterate through all pairs.
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
// If the absolute difference of
// current pair (i, j) is greater
// than the maximum difference
// calculated till now, update
// the value of result.
if (calculateDiff(i, j, arr) > result)
result = calculateDiff(i, j, arr);
}
}
return result;
}
// Driver program to test the above function.
int main()
{
int arr[] = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxDistance(arr, n) << endl;
return 0;
}
Java
// Java program to calculate the maximum
// absolute difference of an array.
public class MaximumAbsoluteDifference
{
private static int calculateDiff(int i, int j,
int[] array)
{
// Utility function to calculate
// the value of absolute difference
// for the pair (i, j).
return Math.abs(array[i] - array[j]) +
Math.abs(i - j);
}
// Function to return maximum absolute
// difference in brute force.
private static int maxDistance(int[] array)
{
// Variable for storing the maximum
// absolute distance throughout the
// traversal of loops.
int result = 0;
// Iterate through all pairs.
for (int i = 0; i < array.length; i++)
{
for (int j = i; j < array.length; j++)
{
// If the absolute difference of
// current pair (i, j) is greater
// than the maximum difference
// calculated till now, update
// the value of result.
result = Math.max(result, calculateDiff(i, j, array));
}
}
return result;
}
// Driver program to test above function
public static void main(String[] args)
{
int[] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
System.out.println(maxDistance(array));
}
}
// This code is contributed by Harikrishnan Rajan
Python3
# Brute force Python 3 program
# to calculate the maximum
# absolute difference of an array.
def calculateDiff(i, j, arr):
# Utility function to calculate
# the value of absolute difference
# for the pair (i, j).
return abs(arr[i] - arr[j]) + abs(i - j)
# Function to return maximum
# absolute difference in
# brute force.
def maxDistance(arr, n):
# Variable for storing the
# maximum absolute distance
# throughout the traversal
# of loops.
result = 0
# Iterate through all pairs.
for i in range(0,n):
for j in range(i, n):
# If the absolute difference of
# current pair (i, j) is greater
# than the maximum difference
# calculated till now, update
# the value of result.
if (calculateDiff(i, j, arr) > result):
result = calculateDiff(i, j, arr)
return result
# Driver program
arr = [ -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 ]
n = len(arr)
print(maxDistance(arr, n))
# This code is contributed by Smitha Dinesh Semwal
C#
// C# program to calculate the maximum
// absolute difference of an array.
using System;
public class MaximumAbsoluteDifference
{
private static int calculateDiff(int i, int j,
int[] array)
{
// Utility function to calculate
// the value of absolute difference
// for the pair (i, j).
return Math.Abs(array[i] - array[j]) +
Math.Abs(i - j);
}
// Function to return maximum absolute
// difference in brute force.
private static int maxDistance(int[] array)
{
// Variable for storing the maximum
// absolute distance throughout the
// traversal of loops.
int result = 0;
// Iterate through all pairs.
for (int i = 0; i < array.Length; i++)
{
for (int j = i; j < array.Length; j++)
{
// If the absolute difference of
// current pair (i, j) is greater
// than the maximum difference
// calculated till now, update
// the value of result.
result = Math.Max(result, calculateDiff(i, j, array));
}
}
return result;
}
// Driver program
public static void Main()
{
int[] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
Console.WriteLine(maxDistance(array));
}
}
// This code is contributed by vt_m
PHP
$result)
$result = calculateDiff($i, $j, $arr);
}
}
return $result;
}
// Driver Code
$arr = array( -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 );
$n = sizeof($arr);
echo maxDistance($arr, $n);
// This Code is contributed by mits
?>
Javascript
C++
// C++ program to calculate the maximum
// absolute difference of an array.
#include
using namespace std;
// Function to return maximum absolue
// difference in linear time.
int maxDistance(int arr[], int n)
{
// max and min variables as described
// in algorithm.
int max1 = INT_MIN, min1 = INT_MAX;
int max2 = INT_MIN, min2 = INT_MAX;
for (int i = 0; i < n; i++) {
// Updating max and min variables
// as described in algorithm.
max1 = max(max1, arr[i] + i);
min1 = min(min1, arr[i] + i);
max2 = max(max2, arr[i] - i);
min2 = min(min2, arr[i] - i);
}
// Calculating maximum absolute difference.
return max(max1 - min1, max2 - min2);
}
// Driver program to test the above function.
int main()
{
int arr[] = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxDistance(arr, n) << endl;
return 0;
}
Java
// Java program to calculate the maximum
// absolute difference of an array.
public class MaximumAbsoluteDifference
{
// Function to return maximum absolue
// difference in linear time.
private static int maxDistance(int[] array)
{
// max and min variables as described
// in algorithm.
int max1 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE;
int max2 = Integer.MIN_VALUE;
int min2 = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++)
{
// Updating max and min variables
// as described in algorithm.
max1 = Math.max(max1, array[i] + i);
min1 = Math.min(min1, array[i] + i);
max2 = Math.max(max2, array[i] - i);
min2 = Math.min(min2, array[i] - i);
}
// Calculating maximum absolute difference.
return Math.max(max1 - min1, max2 - min2);
}
// Driver program to test above function
public static void main(String[] args)
{
int[] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
System.out.println(maxDistance(array));
}
}
// This code is contributed by Harikrishnan Rajan
Python3
# Python program to
# calculate the maximum
# absolute difference
# of an array.
# Function to return
# maximum absolue
# difference in linear time.
def maxDistance(array):
# max and min variables as described
# in algorithm.
max1 = -2147483648
min1 = +2147483647
max2 = -2147483648
min2 = +2147483647
for i in range(len(array)):
# Updating max and min variables
# as described in algorithm.
max1 = max(max1, array[i] + i)
min1 = min(min1, array[i] + i)
max2 = max(max2, array[i] - i)
min2 = min(min2, array[i] - i)
# Calculating maximum absolute difference.
return max(max1 - min1, max2 - min2)
# Driver program to
# test above function
array = [ -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 ]
print(maxDistance(array))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to calculate the maximum
// absolute difference of an array.
using System;
public class MaximumAbsoluteDifference
{
// Function to return maximum absolue
// difference in linear time.
private static int maxDistance(int[] array)
{
// max and min variables as described
// in algorithm.
int max1 = int.MinValue ;
int min1 = int.MaxValue ;
int max2 = int.MinValue ;
int min2 =int.MaxValue ;
for (int i = 0; i < array.Length; i++)
{
// Updating max and min variables
// as described in algorithm.
max1 = Math.Max(max1, array[i] + i);
min1 = Math.Min(min1, array[i] + i);
max2 = Math.Max(max2, array[i] - i);
min2 = Math.Min(min2, array[i] - i);
}
// Calculating maximum absolute difference.
return Math.Max(max1 - min1, max2 - min2);
}
// Driver program
public static void Main()
{
int[] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
Console.WriteLine(maxDistance(array));
}
}
// This code is contributed by vt_m
PHP
输出:
167
时间复杂度: O(n ^ 2)
使用绝对值的属性可以解决O(n)时间复杂度的有效解决方案。
f(i,j)= | A [i] – A [j] | + | i – j |可以用4种方式来写(因为我们正在查看最大值,所以只要我们也以某种方式覆盖最大值,我们甚至都不关心该值是否变为负值)。
Case 1: A[i] > A[j] and i > j
|A[i] - A[j]| = A[i] - A[j]
|i -j| = i - j
hence, f(i, j) = (A[i] + i) - (A[j] + j)
Case 2: A[i] < A[j] and i < j
|A[i] - A[j]| = -(A[i]) + A[j]
|i -j| = -(i) + j
hence, f(i, j) = -(A[i] + i) + (A[j] + j)
Case 3: A[i] > A[j] and i < j
|A[i] - A[j]| = A[i] - A[j]
|i -j| = -(i) + j
hence, f(i, j) = (A[i] - i) - (A[j] - j)
Case 4: A[i] < A[j] and i > j
|A[i] - A[j]| = -(A[i]) + A[j]
|i -j| = i - j
hence, f(i, j) = -(A[i] - i) + (A[j] - j)
请注意,情况1和2等价,情况3和4也等价,因此我们只能针对两种情况设计算法,因为它将涵盖所有可能的情况。
1. Calculate the value of A[i] + i and A[i] – i for every element of the array while traversing through the array.
2. Then for the two equivalent cases, we find the maximum possible value. For that, we have to store minimum and maximum values of expressions A[i] + i and A[i] – i for all i.
3. Hence the required maximum absolute difference is maximum of two values i.e. max((A[i] + i) – (A[j] + j)) and max((A[i] – i) – (A[j] – j)). These values can be found easily in linear time.
a. For max((A[i] + i) – (A[j] + j)) Maintain two variables max1 and min1 which will store maximum and minimum values of A[i] + i respectively. max((A[i] + i) – (A[j] + j)) = max1 – min1
b. For max((A[i] – i) – (A[j] – j)). Maintain two variables max2 and min2 which will store maximum and minimum values of A[i] – i respectively. max((A[i] – i) – (A[j] – j)) = max2 – min2
下面给出使用上述快速算法的实现。
C++
// C++ program to calculate the maximum
// absolute difference of an array.
#include
using namespace std;
// Function to return maximum absolue
// difference in linear time.
int maxDistance(int arr[], int n)
{
// max and min variables as described
// in algorithm.
int max1 = INT_MIN, min1 = INT_MAX;
int max2 = INT_MIN, min2 = INT_MAX;
for (int i = 0; i < n; i++) {
// Updating max and min variables
// as described in algorithm.
max1 = max(max1, arr[i] + i);
min1 = min(min1, arr[i] + i);
max2 = max(max2, arr[i] - i);
min2 = min(min2, arr[i] - i);
}
// Calculating maximum absolute difference.
return max(max1 - min1, max2 - min2);
}
// Driver program to test the above function.
int main()
{
int arr[] = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << maxDistance(arr, n) << endl;
return 0;
}
Java
// Java program to calculate the maximum
// absolute difference of an array.
public class MaximumAbsoluteDifference
{
// Function to return maximum absolue
// difference in linear time.
private static int maxDistance(int[] array)
{
// max and min variables as described
// in algorithm.
int max1 = Integer.MIN_VALUE;
int min1 = Integer.MAX_VALUE;
int max2 = Integer.MIN_VALUE;
int min2 = Integer.MAX_VALUE;
for (int i = 0; i < array.length; i++)
{
// Updating max and min variables
// as described in algorithm.
max1 = Math.max(max1, array[i] + i);
min1 = Math.min(min1, array[i] + i);
max2 = Math.max(max2, array[i] - i);
min2 = Math.min(min2, array[i] - i);
}
// Calculating maximum absolute difference.
return Math.max(max1 - min1, max2 - min2);
}
// Driver program to test above function
public static void main(String[] args)
{
int[] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
System.out.println(maxDistance(array));
}
}
// This code is contributed by Harikrishnan Rajan
Python3
# Python program to
# calculate the maximum
# absolute difference
# of an array.
# Function to return
# maximum absolue
# difference in linear time.
def maxDistance(array):
# max and min variables as described
# in algorithm.
max1 = -2147483648
min1 = +2147483647
max2 = -2147483648
min2 = +2147483647
for i in range(len(array)):
# Updating max and min variables
# as described in algorithm.
max1 = max(max1, array[i] + i)
min1 = min(min1, array[i] + i)
max2 = max(max2, array[i] - i)
min2 = min(min2, array[i] - i)
# Calculating maximum absolute difference.
return max(max1 - min1, max2 - min2)
# Driver program to
# test above function
array = [ -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 ]
print(maxDistance(array))
# This code is contributed
# by Anant Agarwal.
C#
// C# program to calculate the maximum
// absolute difference of an array.
using System;
public class MaximumAbsoluteDifference
{
// Function to return maximum absolue
// difference in linear time.
private static int maxDistance(int[] array)
{
// max and min variables as described
// in algorithm.
int max1 = int.MinValue ;
int min1 = int.MaxValue ;
int max2 = int.MinValue ;
int min2 =int.MaxValue ;
for (int i = 0; i < array.Length; i++)
{
// Updating max and min variables
// as described in algorithm.
max1 = Math.Max(max1, array[i] + i);
min1 = Math.Min(min1, array[i] + i);
max2 = Math.Max(max2, array[i] - i);
min2 = Math.Min(min2, array[i] - i);
}
// Calculating maximum absolute difference.
return Math.Max(max1 - min1, max2 - min2);
}
// Driver program
public static void Main()
{
int[] array = { -70, -64, -6, -56, 64,
61, -57, 16, 48, -98 };
Console.WriteLine(maxDistance(array));
}
}
// This code is contributed by vt_m
的PHP
输出:
167
时间复杂度:O(n)