📌  相关文章
📜  任何一对数组的 arr[i] + arr[j] + i – j 的最大值

📅  最后修改于: 2021-09-04 11:26:34             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是为给定数组的任何可能对(i, j)找到(arr[i] + arr[j] + i − j)的最大值。

例子:

朴素方法:解决给定问题的最简单方法是生成给定数组的所有可能对 (i, j),并在所有可能对中找到表达式的最大值。

下面是上述方法的实现:

C++
// C++ program to for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
void maximumValue(int arr[], int n)
{
    // Stores the required result
    int ans = 0;
 
    // Traverse over all the pairs (i, j)
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
 
            // Calculate the value of the
            // expression and update the
            // overall maximum value
            ans = max(ans, arr[i] + arr[j]
                               + i - j);
        }
    }
 
    // Print the result
    cout << ans;
}
 
// Driven Code
int main()
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    maximumValue(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
static void maximumValue(int arr[], int n)
{
     
    // Stores the required result
    int ans = 0;
 
    // Traverse over all the pairs (i, j)
    for(int i = 0; i < n; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
             
            // Calculate the value of the
            // expression and update the
            // overall maximum value
            ans = Math.max(ans,
                           arr[i] + arr[j] + i - j);
        }
    }
 
    // Print the result
    System.out.println(ans);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = arr.length;
     
    maximumValue(arr, N);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 ptogram for the above approach
 
# Function to find the maximum value of
# arr[i] + arr[j] + i - j over all pairs
def maximumValue(arr, n):
     
    # Stores the required result
    ans = 0
     
    # Traverse over all the pairs (i, j)
    for i in range(n):
        for j in range(i + 1, n):
             
            # Calculate the value of the
            # expression and update the
            # overall maximum value
            ans = max(ans, arr[i] + arr[j] + i - j)
 
    print(ans)
 
# Driver Code
arr = [ 1, 9, 3, 6, 5 ]
N = len(arr)
 
maximumValue(arr, N)
 
# This code is contributed by abhinavjain194


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
static void maximumValue(int[] arr, int n)
{
     
    // Stores the required result
    int ans = 0;
 
    // Traverse over all the pairs (i, j)
    for(int i = 0; i < n; i++)
    {
        for(int j = i + 1; j < n; j++)
        {
             
            // Calculate the value of the
            // expression and update the
            // overall maximum value
            ans = Math.Max(ans, arr[i] + arr[j] + i - j);
        }
    }
     
    // Print the result
    Console.Write(ans);
}
 
// Driver code
static void Main()
{
    int[]  arr = { 1, 9, 3, 6, 5 };
    int N = arr.Length;
     
    maximumValue(arr, N);
}
}
 
// This code is contributed by abhinavjain194


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum value
// of (arr[i] + arr[j] + i - j)
// possible for a pair in the array
void maximumValue(int arr[], int n)
{
    // Stores the maximum value
    // of(arr[i] + i)
    int maxvalue = arr[0];
 
    // Stores the required result
    int result = 0;
 
    // Traverse the array arr[]
    for (int i = 1; i < n; i++) {
 
        // Calculate for current pair
        // and update maximum value
        result = max(result,
                     maxvalue + arr[i] - i);
 
        // Update maxValue if (arr[i] + I)
        // is greater than maxValue
        maxvalue = max(maxvalue, arr[i] + i);
    }
 
    // Print the result
    cout << result;
}
 
// Driven Code
int main()
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    maximumValue(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to find the maximum value
// of (arr[i] + arr[j] + i - j)
// possible for a pair in the array
static void maximumValue(int arr[], int n)
{
     
    // Stores the maximum value
    // of(arr[i] + i)
    int maxvalue = arr[0];
 
    // Stores the required result
    int result = 0;
 
    // Traverse the array arr[]
    for(int i = 1; i < n; i++)
    {
         
        // Calculate for current pair
        // and update maximum value
        result = Math.max(result,
                          maxvalue + arr[i] - i);
 
        // Update maxValue if (arr[i] + I)
        // is greater than maxValue
        maxvalue = Math.max(maxvalue, arr[i] + i);
    }
 
    // Print the result
    System.out.println(result);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = arr.length;
     
    maximumValue(arr, N);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to find the maximum value
# of (arr[i] + arr[j] + i - j)
# possible for a pair in the array
def maximumValue(arr, n):
     
     # Stores the maximum value
    # of(arr[i] + i)
    maxvalue = arr[0]
     
    # Stores the required result
    result = 0
     
    # Traverse the array arr[]
    for  i in range(1, n):
         
        # Calculate for current pair
        # and update maximum value
        result = max(result, maxvalue + arr[i] - i)
         
        # Update maxValue if (arr[i] + I)
        # is greater than maxValue
        maxvalue = max(maxvalue, arr[i] + i)
         
    print(result)
     
# Driver code      
arr = [ 1, 9, 3, 6, 5 ]
N = len(arr)
 
maximumValue(arr, N)
 
# This code is contributed by abhinavjain194


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
static void maximumValue(int[] arr, int n)
{
     
    // Stores the maximum value
    // of(arr[i] + i)
    int maxvalue = arr[0];
 
    // Stores the required result
    int result = 0;
 
    // Traverse the array arr[]
    for(int i = 1; i < n; i++)
    {
         
        // Calculate for current pair
        // and update maximum value
        result = Math.Max(result,
                          maxvalue + arr[i] - i);
 
        // Update maxValue if (arr[i] + I)
        // is greater than maxValue
        maxvalue = Math.Max(maxvalue, arr[i] + i);
    }
 
    // Print the result
    Console.Write(result);
}
 
// Driver code
static void Main()
{
    int[] arr = { 1, 9, 3, 6, 5 };
    int N = arr.Length;
     
    maximumValue(arr, N);
}
}
 
// This code is contributed by abhinavjain194


Javascript


输出:
13

时间复杂度: O(N 2 )
辅助空间: O(1)

高效的方法:上述方法也可以通过将表达式(arr[i] + arr[j] + i – j)分成两部分来优化: (arr[i] + i)(arr[j] – j)然后最大化(arr[i] + i) 的最大值与(arr[i] – i) 的所有可能值的总和。请按照以下步骤解决问题:

  • 初始化两个变量, res0以存储所需的总和, maxValuearr[0]以跟踪(arr[i] + i)的最大值。
  • 使用变量i在范围[1, N – 1] 上遍历给定数组arr[]并执行以下步骤:
    • 将表达式在X 中的值存储为(maxValue + arr[i] – i)
    • 如果X的值大于水库时,则更新资源x的值。
    • 如果arr[i] + i的值大于maxValue ,则将maxValue更新为(arr[i] + i) 
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the maximum value
// of (arr[i] + arr[j] + i - j)
// possible for a pair in the array
void maximumValue(int arr[], int n)
{
    // Stores the maximum value
    // of(arr[i] + i)
    int maxvalue = arr[0];
 
    // Stores the required result
    int result = 0;
 
    // Traverse the array arr[]
    for (int i = 1; i < n; i++) {
 
        // Calculate for current pair
        // and update maximum value
        result = max(result,
                     maxvalue + arr[i] - i);
 
        // Update maxValue if (arr[i] + I)
        // is greater than maxValue
        maxvalue = max(maxvalue, arr[i] + i);
    }
 
    // Print the result
    cout << result;
}
 
// Driven Code
int main()
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = sizeof(arr) / sizeof(arr[0]);
    maximumValue(arr, N);
 
    return 0;
}

Java

// Java program for the above approach
class GFG{
 
// Function to find the maximum value
// of (arr[i] + arr[j] + i - j)
// possible for a pair in the array
static void maximumValue(int arr[], int n)
{
     
    // Stores the maximum value
    // of(arr[i] + i)
    int maxvalue = arr[0];
 
    // Stores the required result
    int result = 0;
 
    // Traverse the array arr[]
    for(int i = 1; i < n; i++)
    {
         
        // Calculate for current pair
        // and update maximum value
        result = Math.max(result,
                          maxvalue + arr[i] - i);
 
        // Update maxValue if (arr[i] + I)
        // is greater than maxValue
        maxvalue = Math.max(maxvalue, arr[i] + i);
    }
 
    // Print the result
    System.out.println(result);
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 9, 3, 6, 5 };
    int N = arr.length;
     
    maximumValue(arr, N);
}
}
 
// This code is contributed by abhinavjain194

蟒蛇3

# Python3 program for the above approach
 
# Function to find the maximum value
# of (arr[i] + arr[j] + i - j)
# possible for a pair in the array
def maximumValue(arr, n):
     
     # Stores the maximum value
    # of(arr[i] + i)
    maxvalue = arr[0]
     
    # Stores the required result
    result = 0
     
    # Traverse the array arr[]
    for  i in range(1, n):
         
        # Calculate for current pair
        # and update maximum value
        result = max(result, maxvalue + arr[i] - i)
         
        # Update maxValue if (arr[i] + I)
        # is greater than maxValue
        maxvalue = max(maxvalue, arr[i] + i)
         
    print(result)
     
# Driver code      
arr = [ 1, 9, 3, 6, 5 ]
N = len(arr)
 
maximumValue(arr, N)
 
# This code is contributed by abhinavjain194

C#

// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum value of
// arr[i] + arr[j] + i - j over all pairs
static void maximumValue(int[] arr, int n)
{
     
    // Stores the maximum value
    // of(arr[i] + i)
    int maxvalue = arr[0];
 
    // Stores the required result
    int result = 0;
 
    // Traverse the array arr[]
    for(int i = 1; i < n; i++)
    {
         
        // Calculate for current pair
        // and update maximum value
        result = Math.Max(result,
                          maxvalue + arr[i] - i);
 
        // Update maxValue if (arr[i] + I)
        // is greater than maxValue
        maxvalue = Math.Max(maxvalue, arr[i] + i);
    }
 
    // Print the result
    Console.Write(result);
}
 
// Driver code
static void Main()
{
    int[] arr = { 1, 9, 3, 6, 5 };
    int N = arr.Length;
     
    maximumValue(arr, N);
}
}
 
// This code is contributed by abhinavjain194

Javascript


输出:
13

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live