📌  相关文章
📜  | arr [i] – arr [j] |的最大值+ | i – j |

📅  最后修改于: 2021-04-29 09:44:36             🧑  作者: Mango

给定一个由N个正整数组成的数组。任务是找到| arr [i] – arr [j] |的最大值。 + | i – j |,其中0 <= i,j <= N – 1且arr [i],arr [j]属于数组。

例子:

方法1:想法是使用蛮力,即在两个for循环中进行迭代。

以下是此方法的实现:

C++
#include 
using namespace std;
#define MAX 10
 
// Return maximum value of |arr[i] - arr[j]| + |i - j|
int findValue(int arr[], int n)
{
    int ans = 0;
 
    // Iterating two for loop, one for
    // i and another for j.
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
 
            // Evaluating |arr[i] - arr[j]| + |i - j|
            // and compare with previous maximum.
            ans = max(ans,
                      abs(arr[i] - arr[j]) + abs(i - j));
 
    return ans;
}
 
// Driven Program
int main()
{
    int arr[] = { 1, 2, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findValue(arr, n) << endl;
 
    return 0;
}


Java
// java program to find maximum value of
// |arr[i] - arr[j]| + |i - j|
class GFG {
    static final int MAX = 10;
 
    // Return maximum value of
    // |arr[i] - arr[j]| + |i - j|
    static int findValue(int arr[], int n)
    {
        int ans = 0;
 
        // Iterating two for loop,
        // one for i and another for j.
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
 
                // Evaluating |arr[i] - arr[j]|
                // + |i - j| and compare with
                // previous maximum.
                ans = Math.max(ans,
                               Math.abs(arr[i] - arr[j])
                               + Math.abs(i - j));
 
        return ans;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 1 };
        int n = arr.length;
 
        System.out.println(findValue(arr, n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find
# maximum value of
# |arr[i] - arr[j]| + |i - j|
 
# Return maximum value of
# |arr[i] - arr[j]| + |i - j|
def findValue(arr, n):
    ans = 0;
     
    # Iterating two for loop,
    # one for i and another for j.
    for i in range(n):
        for j in range(n):
             
            # Evaluating |arr[i] -
            # arr[j]| + |i - j|
            # and compare with
            # previous maximum.
            ans = ans if ans>(abs(arr[i] - arr[j]) +
                              abs(i - j)) else (abs(arr[i] -
                                      arr[j]) + abs(i - j)) ;
    return ans;
     
# Driver Code
arr = [1, 2, 3, 1];
n = len(arr);
print(findValue(arr, n));
 
# This code is contributed by mits.


C#
// C# program to find maximum value of
// |arr[i] - arr[j]| + |i - j|
using System;
 
class GFG {
     
    // Return maximum value of
    // |arr[i] - arr[j]| + |i - j|
    static int findValue(int []arr, int n)
    {
        int ans = 0;
     
        // Iterating two for loop,
        // one for i and another for j.
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
     
                // Evaluating |arr[i] - arr[j]|
                // + |i - j| and compare with
                // previous maximum.
                ans = Math.Max(ans,
                    Math.Abs(arr[i] - arr[j])
                            + Math.Abs(i - j));
     
        return ans;
    }
     
    // Driver code
    public static void Main ()
    {
        int []arr = { 1, 2, 3, 1 };
        int n =arr.Length;
         
        Console.Write(findValue(arr, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


C++
// Efficient CPP program to find maximum value
// of |arr[i] - arr[j]| + |i - j|
#include 
using namespace std;
 
// Return maximum |arr[i] - arr[j]| + |i - j|
int findValue(int arr[], int n)
{
    int a[n], b[n], tmp;
 
    // Calculating first_array and second_array
    for (int i = 0; i < n; i++)
    {
        a[i] = (arr[i] + i);
        b[i] = (arr[i] - i);
    }
 
    int x = a[0], y = a[0];
 
    // Finding maximum and minimum value in
    // first_array
    for (int i = 0; i < n; i++)
    {
        if (a[i] > x)
            x = a[i];
 
        if (a[i] < y)
            y = a[i];
    }
 
    // Storing the difference between maximum and
    // minimum value in first_array
    int ans1 = (x - y);
 
    x = b[0];
    y = b[0];
 
    // Finding maximum and minimum value in
    // second_array
    for (int i = 0; i < n; i++)
    {
        if (b[i] > x)
            x = b[i];
 
        if (b[i] < y)
            y = b[i];
    }
 
    // Storing the difference between maximum and
    // minimum value in second_array
    int ans2 = (x - y);
 
    return max(ans1, ans2);
}
 
// Driven Code
int main()
{
    int arr[] = { 1, 2, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findValue(arr, n) << endl;
 
    return 0;
}


Java
// Efficient Java program to find maximum
// value of |arr[i] - arr[j]| + |i - j|
import java.io.*;
class GFG {
 
    // Return maximum |arr[i] -
    // arr[j]| + |i - j|
    static int findValue(int arr[], int n)
    {
        int a[] = new int[n];
        int b[] = new int[n];
        int tmp;
 
        // Calculating first_array
        // and second_array
        for (int i = 0; i < n; i++)
        {
            a[i] = (arr[i] + i);
            b[i] = (arr[i] - i);
        }
 
        int x = a[0], y = a[0];
 
        // Finding maximum and
        // minimum value in
        // first_array
        for (int i = 0; i < n; i++)
        {
            if (a[i] > x)
                x = a[i];
 
            if (a[i] < y)
                y = a[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in first_array
        int ans1 = (x - y);
 
        x = b[0];
        y = b[0];
 
        // Finding maximum and
        // minimum value in
        // second_array
        for (int i = 0; i < n; i++)
        {
            if (b[i] > x)
                x = b[i];
 
            if (b[i] < y)
                y = b[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in second_array
        int ans2 = (x - y);
 
        return Math.max(ans1, ans2);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 1 };
        int n = arr.length;
        System.out.println(findValue(arr, n));
    }
}
 
// This code is contributed by anuj_67.


Python3
# Efficient Python3 program
# to find maximum value
# of |arr[i] - arr[j]| + |i - j|
 
# Return maximum |arr[i] -
# arr[j]| + |i - j|
 
 
def findValue(arr, n):
    a = []
    b = []
 
    # Calculating first_array
    # and second_array
    for i in range(n):
        a.append(arr[i] + i)
        b.append(arr[i] - i)
 
    x = a[0]
    y = a[0]
 
    # Finding maximum and
    # minimum value in
    # first_array
    for i in range(n):
        if (a[i] > x):
            x = a[i]
 
        if (a[i] < y):
            y = a[i]
 
    # Storing the difference
    # between maximum and
    # minimum value in first_array
    ans1 = (x - y)
 
    x = b[0]
    y = b[0]
 
    # Finding maximum and
    # minimum value in
    # second_array
    for i in range(n):
        if (b[i] > x):
            x = b[i]
 
        if (b[i] < y):
            y = b[i]
 
    # Storing the difference
    # between maximum and
    # minimum value in
    # second_array
    ans2 = (x - y)
 
    return max(ans1, ans2)
 
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 1]
    n = len(arr)
 
    print(findValue(arr, n))
 
# This code is contributed by mits


C#
// Efficient Java program to find maximum
// value of |arr[i] - arr[j]| + |i - j|
using System;
class GFG {
 
    // Return maximum |arr[i] -
    // arr[j]| + |i - j|
    static int findValue(int[] arr, int n)
    {
        int[] a = new int[n];
        int[] b = new int[n];
        // int tmp;
 
        // Calculating first_array
        // and second_array
        for (int i = 0; i < n; i++)
        {
            a[i] = (arr[i] + i);
            b[i] = (arr[i] - i);
        }
 
        int x = a[0], y = a[0];
 
        // Finding maximum and
        // minimum value in
        // first_array
        for (int i = 0; i < n; i++)
        {
            if (a[i] > x)
                x = a[i];
 
            if (a[i] < y)
                y = a[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in first_array
        int ans1 = (x - y);
 
        x = b[0];
        y = b[0];
 
        // Finding maximum and
        // minimum value in
        // second_array
        for (int i = 0; i < n; i++)
        {
            if (b[i] > x)
                x = b[i];
 
            if (b[i] < y)
                y = b[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in second_array
        int ans2 = (x - y);
 
        return Math.Max(ans1, ans2);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 1 };
        int n = arr.Length;
        Console.WriteLine(findValue(arr, n));
    }
}
 
// This code is contributed by anuj_67.


PHP
 $x)
        $x = $a[$i];
 
        if ($a[$i] < $y)
            $y = $a[$i];
    }
 
    // Storing the difference
    // between maximum and
    // minimum value in first_array
    $ans1 = ($x - $y);
 
    $x = $b[0];
    $y = $b[0];
 
    // Finding maximum and
    // minimum value in
    // second_array
    for ($i = 0; $i < $n; $i++)
    {
        if ($b[$i] > $x)
            $x = $b[$i];
 
        if ($b[$i] < $y)
            $y = $b[$i];
    }
 
    // Storing the difference
    // between maximum and
    // minimum value in
    // second_array
    $ans2 = ($x -$y);
 
    return max($ans1, $ans2);
}
 
    // Driver Code
    $arr = array(1, 2, 3, 1);
    $n = count($arr);
 
    echo findValue($arr, $n);
     
// This code is contributed by anuj_67.
?>


输出
4

方法2(棘手):

首先,通过去除绝对值符号(“ |”)来建立四个方程。将形成以下4个方程,我们需要找到这些方程的最大值,这将是我们的答案。

  1. arr [i] – arr [j] + i – j =(arr [i] + i)–(arr [j] + j)
  2. arr [i] – arr [j] – i + j =(arr [i] – i)–(arr [j] – j)
  3. -arr [i] + arr [j] + i – j =-(arr [i] – i)+(arr [j] – j)
  4. -arr [i] + arr [j] – i + j =-(arr [i] + i)+(arr [j] + j)

观察方程(1)和(4)是相同的,类似地,方程(2)和(3)是相同的。
现在的任务是找到这些方程式的最大值。因此,方法是形成两个数组first_array [],它将存储arr [i] + i,0 <= i 现在我们的任务很容易,我们只需要找到这两个数组的两个值之间的最大差即可。
为此,我们在first_array中找到最大值和最小值并存储它们的差值:
ans1 =(first_array中的最大值– first_array中的最小值)
同样,我们需要在second_array中找到最大值和最小值并存储它们的差值:
ans2 =(second_array中的最大值– second_array中的最小值)
我们的答案将是ans1和ans2的最大值。

下面是上述方法的实现:

C++

// Efficient CPP program to find maximum value
// of |arr[i] - arr[j]| + |i - j|
#include 
using namespace std;
 
// Return maximum |arr[i] - arr[j]| + |i - j|
int findValue(int arr[], int n)
{
    int a[n], b[n], tmp;
 
    // Calculating first_array and second_array
    for (int i = 0; i < n; i++)
    {
        a[i] = (arr[i] + i);
        b[i] = (arr[i] - i);
    }
 
    int x = a[0], y = a[0];
 
    // Finding maximum and minimum value in
    // first_array
    for (int i = 0; i < n; i++)
    {
        if (a[i] > x)
            x = a[i];
 
        if (a[i] < y)
            y = a[i];
    }
 
    // Storing the difference between maximum and
    // minimum value in first_array
    int ans1 = (x - y);
 
    x = b[0];
    y = b[0];
 
    // Finding maximum and minimum value in
    // second_array
    for (int i = 0; i < n; i++)
    {
        if (b[i] > x)
            x = b[i];
 
        if (b[i] < y)
            y = b[i];
    }
 
    // Storing the difference between maximum and
    // minimum value in second_array
    int ans2 = (x - y);
 
    return max(ans1, ans2);
}
 
// Driven Code
int main()
{
    int arr[] = { 1, 2, 3, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << findValue(arr, n) << endl;
 
    return 0;
}

Java

// Efficient Java program to find maximum
// value of |arr[i] - arr[j]| + |i - j|
import java.io.*;
class GFG {
 
    // Return maximum |arr[i] -
    // arr[j]| + |i - j|
    static int findValue(int arr[], int n)
    {
        int a[] = new int[n];
        int b[] = new int[n];
        int tmp;
 
        // Calculating first_array
        // and second_array
        for (int i = 0; i < n; i++)
        {
            a[i] = (arr[i] + i);
            b[i] = (arr[i] - i);
        }
 
        int x = a[0], y = a[0];
 
        // Finding maximum and
        // minimum value in
        // first_array
        for (int i = 0; i < n; i++)
        {
            if (a[i] > x)
                x = a[i];
 
            if (a[i] < y)
                y = a[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in first_array
        int ans1 = (x - y);
 
        x = b[0];
        y = b[0];
 
        // Finding maximum and
        // minimum value in
        // second_array
        for (int i = 0; i < n; i++)
        {
            if (b[i] > x)
                x = b[i];
 
            if (b[i] < y)
                y = b[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in second_array
        int ans2 = (x - y);
 
        return Math.max(ans1, ans2);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 1 };
        int n = arr.length;
        System.out.println(findValue(arr, n));
    }
}
 
// This code is contributed by anuj_67.

Python3

# Efficient Python3 program
# to find maximum value
# of |arr[i] - arr[j]| + |i - j|
 
# Return maximum |arr[i] -
# arr[j]| + |i - j|
 
 
def findValue(arr, n):
    a = []
    b = []
 
    # Calculating first_array
    # and second_array
    for i in range(n):
        a.append(arr[i] + i)
        b.append(arr[i] - i)
 
    x = a[0]
    y = a[0]
 
    # Finding maximum and
    # minimum value in
    # first_array
    for i in range(n):
        if (a[i] > x):
            x = a[i]
 
        if (a[i] < y):
            y = a[i]
 
    # Storing the difference
    # between maximum and
    # minimum value in first_array
    ans1 = (x - y)
 
    x = b[0]
    y = b[0]
 
    # Finding maximum and
    # minimum value in
    # second_array
    for i in range(n):
        if (b[i] > x):
            x = b[i]
 
        if (b[i] < y):
            y = b[i]
 
    # Storing the difference
    # between maximum and
    # minimum value in
    # second_array
    ans2 = (x - y)
 
    return max(ans1, ans2)
 
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 1]
    n = len(arr)
 
    print(findValue(arr, n))
 
# This code is contributed by mits

C#

// Efficient Java program to find maximum
// value of |arr[i] - arr[j]| + |i - j|
using System;
class GFG {
 
    // Return maximum |arr[i] -
    // arr[j]| + |i - j|
    static int findValue(int[] arr, int n)
    {
        int[] a = new int[n];
        int[] b = new int[n];
        // int tmp;
 
        // Calculating first_array
        // and second_array
        for (int i = 0; i < n; i++)
        {
            a[i] = (arr[i] + i);
            b[i] = (arr[i] - i);
        }
 
        int x = a[0], y = a[0];
 
        // Finding maximum and
        // minimum value in
        // first_array
        for (int i = 0; i < n; i++)
        {
            if (a[i] > x)
                x = a[i];
 
            if (a[i] < y)
                y = a[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in first_array
        int ans1 = (x - y);
 
        x = b[0];
        y = b[0];
 
        // Finding maximum and
        // minimum value in
        // second_array
        for (int i = 0; i < n; i++)
        {
            if (b[i] > x)
                x = b[i];
 
            if (b[i] < y)
                y = b[i];
        }
 
        // Storing the difference
        // between maximum and
        // minimum value in second_array
        int ans2 = (x - y);
 
        return Math.Max(ans1, ans2);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 2, 3, 1 };
        int n = arr.Length;
        Console.WriteLine(findValue(arr, n));
    }
}
 
// This code is contributed by anuj_67.

的PHP

 $x)
        $x = $a[$i];
 
        if ($a[$i] < $y)
            $y = $a[$i];
    }
 
    // Storing the difference
    // between maximum and
    // minimum value in first_array
    $ans1 = ($x - $y);
 
    $x = $b[0];
    $y = $b[0];
 
    // Finding maximum and
    // minimum value in
    // second_array
    for ($i = 0; $i < $n; $i++)
    {
        if ($b[$i] > $x)
            $x = $b[$i];
 
        if ($b[$i] < $y)
            $y = $b[$i];
    }
 
    // Storing the difference
    // between maximum and
    // minimum value in
    // second_array
    $ans2 = ($x -$y);
 
    return max($ans1, $ans2);
}
 
    // Driver Code
    $arr = array(1, 2, 3, 1);
    $n = count($arr);
 
    echo findValue($arr, $n);
     
// This code is contributed by anuj_67.
?>
输出
4