📌  相关文章
📜  最大化数组中 (arr[i] – i) – (arr[j] – j) 的值

📅  最后修改于: 2022-05-13 01:57:50.157000             🧑  作者: Mango

最大化数组中 (arr[i] – i) – (arr[j] – j) 的值

给定一个数组,arr[] 找到 (arr[i] – i) – (arr[j] – j) 的最大值,其中 i 不等于 j。其中 i 和 j 从 0 到 n-1 变化,n 是输入数组 arr[] 的大小。
例子:

Input : arr[] = {9, 15, 4, 12, 13}
Output : 12
We get the maximum value for i = 1 and j = 2
(15 - 1) - (4 - 2) = 12

Input : arr[] = {-1, -2, -3, 4, 10}
Output : 6
We get the maximum value for i = 4 and j = 2
(10 - 4) - (-3 - 2) = 11

一个重要的观察结果是,(arr[i] – i) – (arr[j] – j) 的值永远不会是负数。我们总是可以交换 i 和 j 以将负值转换为正值。所以条件 i 不等于 j 是假的,不需要明确检查。
方法 1(朴素:O(n 2 ))
这个想法是运行两个循环来考虑所有可能的对并跟踪表达式的最大值 (arr[i]-i)-(arr[j]-j)。下面是这个想法的实现。

C++
// C++ program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
#include
using namespace std;
 
// Returns maximum value of (arr[i]-i) - (arr[j]-j)
int findMaxDiff(int arr[], int n)
{
    if (n < 2)
    {
        cout << "Invalid ";
        return 0;
    }
 
    // Use two nested loops to find the result
    int res = INT_MIN;
    for (int i=0; i


Java
// Java program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
import java.util.*;
 
class GFG {
     
// Returns maximum value of
// (arr[i]-i) - (arr[j]-j)
static int findMaxDiff(int arr[], int n)
{
    if (n < 2) {
    System.out.print("Invalid ");
    return 0;
    }
 
    // Use two nested loops to find the result
    int res = Integer.MIN_VALUE;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        if (res < (arr[i] - arr[j] - i + j))
        res = (arr[i] - arr[j] - i + j);
 
    return res;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = {9, 15, 4, 12, 13};
    int n = arr.length;
    System.out.print(findMaxDiff(arr, n));
}
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python program to find
# maximum value (arr[i]-i)
# - (arr[j]-j) in an array.
 
# Returns maximum value of
# (arr[i]-i) - (arr[j]-j)
def findMaxDiff(arr,n):
 
    if (n < 2):
     
        print("Invalid ")
        return 0
     
  
    # Use two nested loops
    # to find the result
    res = -2147483648
    for i in range(n):
        for j in range(n):
            if ( res < (arr[i]-arr[j]-i+j) ):
                res = (arr[i]-arr[j]-i+j)
  
    return res
 
# Driver code
 
arr= [9, 15, 4, 12, 13]
n = len(arr)
 
print(findMaxDiff(arr, n))
 
# This code is contributed
# by Anant Agarwal.


C#
// C# program to find maximum
// value (arr[i]-i)- (arr[j]-j)
// in an array.
using System;
class GFG {
     
// Returns maximum value of
// (arr[i]-i) - (arr[j]-j)
static int findMaxDiff(int []arr, int n)
{
    if (n < 2) {
    Console.WriteLine("Invalid ");
    return 0;
    }
 
    // Use two nested loops to
    // find the result
    int res = int.MinValue;
    for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
        if (res < (arr[i] - arr[j] - i + j))
            res = (arr[i] - arr[j] - i + j);
 
    return res;
}
 
// Driver code
public static void Main()
{
    int []arr = {9, 15, 4, 12, 13};
    int n = arr.Length;
    Console.WriteLine(findMaxDiff(arr, n));
}
}
 
// This code is contributed by anjuj_67.


PHP


Javascript


C++
// C++ program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
#include
using namespace std;
 
// Returns maximum value of (arr[i]-i) - (arr[j]-j)
int findMaxDiff(int a[], int n)
{
    if (n < 2)
    {
        cout << "Invalid ";
        return 0;
    }
 
    // Find maximum of value of arr[i] - i for all
    // possible values of i. Let this value be max_val.
    // Find minimum of value of arr[i] - i for all
    // possible values of i. Let this value be min_val.
    // The difference max_val - min_v
    int min_val = INT_MAX, max_val =
                           INT_MIN;
    for (int i=0; i max_val)
            max_val = a[i] - i;
        if ((a[i]-i) < min_val)
            min_val = a[i]-i;
    }
 
    return (max_val - min_val);
}
 
// Driver program
int main()
{
    int arr[] = {9, 15, 4, 12, 13};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << findMaxDiff(arr, n);
    return 0;
}


Java
// Java program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
import java.io.*;
 
class GFG {
 
    // Returns maximum value of (arr[i]-i) -
    // (arr[j]-j)
    static int findMaxDiff(int arr[], int n)   
    {
        if (n < 2)
        {
            System.out.println("Invalid ");
            return 0;
        }
 
        // Find maximum of value of arr[i] - i
        // for all possible values of i. Let
        // this value be max_val. Find minimum
        // of value of arr[i] - i for all
        // possible values of i. Let this value
        // be min_val. The difference max_val -
        // min_v
        int min_val = Integer.MAX_VALUE,
            max_val = Integer.MIN_VALUE;
         
        for (int i = 0; i < n; i++)
        {
            if ((arr[i]-i) > max_val)
                max_val = arr[i] - i;
                 
            if ((arr[i]-i) < min_val)
                min_val = arr[i]-i;
        }
 
        return (max_val - min_val);
    }
 
    // Driver program
    public static void main(String[] args)
    {
        int arr[] = {9, 15, 4, 12, 13};
        int n = arr.length;
        System.out.println(findMaxDiff(arr, n));
    }
}
// This code is contributed by Prerna Saini


Python3
# Python 3 program to find maximum value
# (arr[i]-i) - (arr[j]-j) in an array.
import sys
 
# Returns maximum value of
# (arr[i]-i) - (arr[j]-j)
def findMaxDiff(a, n):
    if (n < 2):
        print("Invalid ")
        return 0
 
    # Find maximum of value of arr[i] - i
    # for all possible values of i. Let
    # this value be max_val. Find minimum
    # of value of arr[i] - i for all possible
    # values of i. Let this value be min_val.
    # The difference max_val - min_v
    min_val = sys.maxsize
    max_val = -sys.maxsize - 1
    for i in range(n):
        if ((a[i] - i) > max_val):
            max_val = a[i] - i
        if ((a[i] - i) < min_val):
            min_val = a[i] - i
 
    return (max_val - min_val)
 
# Driver Code
if __name__ == '__main__':
    arr = [9, 15, 4, 12, 13]
    n = len(arr)
    print(findMaxDiff(arr, n))
 
# This code is contributed by Rajput-Ji


C#
// C# program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
using System;
  
class GFG {
  
    // Returns maximum value of (arr[i]-i) -
    // (arr[j]-j)
    static int findMaxDiff(int []arr, int n)   
    {
        if (n < 2)
        {
            Console.Write("Invalid ");
            return 0;
        }
  
        // Find maximum of value of arr[i] - i
        // for all possible values of i. Let
        // this value be max_val. Find minimum
        // of value of arr[i] - i for all
        // possible values of i. Let this value
        // be min_val. The difference max_val -
        // min_v
        int min_val = int.MaxValue,
            max_val = int.MinValue;
          
        for (int i = 0; i < n; i++)
        {
            if ((arr[i] - i) > max_val)
                max_val = arr[i] - i;
                  
            if ((arr[i] - i) < min_val)
                min_val = arr[i] - i;
        }
  
        return (max_val - min_val);
    }
  
    // Driver program
    public static void Main()
    {
        int []arr = {9, 15, 4, 12, 13};
        int n = arr.Length;
        Console.Write(findMaxDiff(arr, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP
 $max_val)
            $max_val = $a[$i] - $i;
        if (($a[$i] - $i) < $min_val)
            $min_val = $a[$i] - $i;
    }
 
    return ($max_val - $min_val);
}
 
// Driver Code
$arr = array(9, 15, 4, 12, 13);
$n = sizeof($arr);
echo findMaxDiff($arr, $n);
 
// This code is contributed by Sachin.
?>


Javascript


输出:

12


方法2(棘手:O(n))
1) 在整个数组中找到 arr[i] – i 的最大值。
2) 在整个数组中找到 arr[i] – i 的最小值。
3) 返回上述两个值的差值。

C++

// C++ program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
#include
using namespace std;
 
// Returns maximum value of (arr[i]-i) - (arr[j]-j)
int findMaxDiff(int a[], int n)
{
    if (n < 2)
    {
        cout << "Invalid ";
        return 0;
    }
 
    // Find maximum of value of arr[i] - i for all
    // possible values of i. Let this value be max_val.
    // Find minimum of value of arr[i] - i for all
    // possible values of i. Let this value be min_val.
    // The difference max_val - min_v
    int min_val = INT_MAX, max_val =
                           INT_MIN;
    for (int i=0; i max_val)
            max_val = a[i] - i;
        if ((a[i]-i) < min_val)
            min_val = a[i]-i;
    }
 
    return (max_val - min_val);
}
 
// Driver program
int main()
{
    int arr[] = {9, 15, 4, 12, 13};
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << findMaxDiff(arr, n);
    return 0;
}

Java

// Java program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
import java.io.*;
 
class GFG {
 
    // Returns maximum value of (arr[i]-i) -
    // (arr[j]-j)
    static int findMaxDiff(int arr[], int n)   
    {
        if (n < 2)
        {
            System.out.println("Invalid ");
            return 0;
        }
 
        // Find maximum of value of arr[i] - i
        // for all possible values of i. Let
        // this value be max_val. Find minimum
        // of value of arr[i] - i for all
        // possible values of i. Let this value
        // be min_val. The difference max_val -
        // min_v
        int min_val = Integer.MAX_VALUE,
            max_val = Integer.MIN_VALUE;
         
        for (int i = 0; i < n; i++)
        {
            if ((arr[i]-i) > max_val)
                max_val = arr[i] - i;
                 
            if ((arr[i]-i) < min_val)
                min_val = arr[i]-i;
        }
 
        return (max_val - min_val);
    }
 
    // Driver program
    public static void main(String[] args)
    {
        int arr[] = {9, 15, 4, 12, 13};
        int n = arr.length;
        System.out.println(findMaxDiff(arr, n));
    }
}
// This code is contributed by Prerna Saini

Python3

# Python 3 program to find maximum value
# (arr[i]-i) - (arr[j]-j) in an array.
import sys
 
# Returns maximum value of
# (arr[i]-i) - (arr[j]-j)
def findMaxDiff(a, n):
    if (n < 2):
        print("Invalid ")
        return 0
 
    # Find maximum of value of arr[i] - i
    # for all possible values of i. Let
    # this value be max_val. Find minimum
    # of value of arr[i] - i for all possible
    # values of i. Let this value be min_val.
    # The difference max_val - min_v
    min_val = sys.maxsize
    max_val = -sys.maxsize - 1
    for i in range(n):
        if ((a[i] - i) > max_val):
            max_val = a[i] - i
        if ((a[i] - i) < min_val):
            min_val = a[i] - i
 
    return (max_val - min_val)
 
# Driver Code
if __name__ == '__main__':
    arr = [9, 15, 4, 12, 13]
    n = len(arr)
    print(findMaxDiff(arr, n))
 
# This code is contributed by Rajput-Ji

C#

// C# program to find maximum value (arr[i]-i)
// - (arr[j]-j) in an array.
using System;
  
class GFG {
  
    // Returns maximum value of (arr[i]-i) -
    // (arr[j]-j)
    static int findMaxDiff(int []arr, int n)   
    {
        if (n < 2)
        {
            Console.Write("Invalid ");
            return 0;
        }
  
        // Find maximum of value of arr[i] - i
        // for all possible values of i. Let
        // this value be max_val. Find minimum
        // of value of arr[i] - i for all
        // possible values of i. Let this value
        // be min_val. The difference max_val -
        // min_v
        int min_val = int.MaxValue,
            max_val = int.MinValue;
          
        for (int i = 0; i < n; i++)
        {
            if ((arr[i] - i) > max_val)
                max_val = arr[i] - i;
                  
            if ((arr[i] - i) < min_val)
                min_val = arr[i] - i;
        }
  
        return (max_val - min_val);
    }
  
    // Driver program
    public static void Main()
    {
        int []arr = {9, 15, 4, 12, 13};
        int n = arr.Length;
        Console.Write(findMaxDiff(arr, n));
    }
}
 
// This code is contributed by nitin mittal.

PHP

 $max_val)
            $max_val = $a[$i] - $i;
        if (($a[$i] - $i) < $min_val)
            $min_val = $a[$i] - $i;
    }
 
    return ($max_val - $min_val);
}
 
// Driver Code
$arr = array(9, 15, 4, 12, 13);
$n = sizeof($arr);
echo findMaxDiff($arr, $n);
 
// This code is contributed by Sachin.
?>

Javascript


输出:

12