📌  相关文章
📜  阵列中相隔至少 K 距离的对的最大总和

📅  最后修改于: 2021-09-07 03:11:35             🧑  作者: Mango

给定一个由N 个整数和一个整数K组成的数组arr[] ,任务是找到由至少K 个索引分隔的元素对的最大和。

例子:

天真的方法:最简单的 解决给定问题的方法是生成给定数组中相距K 远的所有可能对,并打印形成的所有可能对中的最大和。

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

高效的方法:可以通过预先计算每个数组元素的前缀最大值来优化上述方法。请按照以下步骤解决给定的问题:

  • 初始化一个变量,比如resINT_MIN ,它存储给定数组中相距 K 的有效对的最大总和。
  • 初始化一个数组,比如preMax[] ,它将最大值数组元素存储到每个索引i
  • 初始化preMax[0]等于arr[0]
  • [1, N – 1]范围内遍历数组并将preMax[i]的值更新为preMax[i – 1]arr[i]的最大值。
  • 现在,迭代范围[K, N – 1] ,对于每个索引i ,将res的值更新为res(arr[i] + preMax[i – K])的最大值。
  • 完成上述步骤后,打印res的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to find the largest sum
// pair that are K distant apart
int getMaxPairSum(int arr[], int N,
                  int K)
{
    // Stores the prefix maximum array
    int preMax[N];
 
    // Base Case
    preMax[0] = arr[0];
 
    // Traverse the array and update
    // the maximum value upto index i
    for (int i = 1; i < N; i++) {
        preMax[i] = max(preMax[i - 1],
                        arr[i]);
    }
 
    // Stores the maximum sum of pairs
    int res = INT_MIN;
 
    // Iterate over the range [K, N]
    for (int i = K; i < N; i++) {
        // Find the maximum value of
        // the sum of valid pairs
        res = max(res, arr[i]
                           + preMax[i - K]);
    }
 
    // Return the resultant sum
    return res;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 4, 8, 6, 3 };
    int K = 3;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << getMaxPairSum(arr, N, K);
 
    return 0;
}


Java
// java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
public class GFG {
 
    // Function to find the largest sum
    // pair that are K distant apart
    static int getMaxPairSum(int[] arr, int N, int K)
    {
 
        // Stores the prefix maximum array
        int[] preMax = new int[N];
 
        // Base Case
        preMax[0] = arr[0];
 
        // Traverse the array and update
        // the maximum value upto index i
        for (int i = 1; i < N; i++) {
            preMax[i] = Math.max(preMax[i - 1], arr[i]);
        }
 
        // Stores the maximum sum of pairs
        int res = Integer.MIN_VALUE;
 
        // Iterate over the range [K, N]
        for (int i = K; i < N; i++) {
 
            // Find the maximum value of
            // the sum of valid pairs
            res = Math.max(res, arr[i] + preMax[i - K]);
        }
 
        // Return the resultant sum
        return res;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int[] arr = { 1, 2, 4, 8, 6, 3 };
        int K = 3;
        int N = arr.length;
        System.out.print(getMaxPairSum(arr, N, K));
    }
}
 
// This code is contributed by Kingash


Python3
# Python3` program for the above approach
 
# Function to find the largest sum
# pair that are K distant apart
def getMaxPairSum(arr, N, K):
   
    # Stores the prefix maximum array
    preMax = [0]*N
 
    # Base Case
    preMax[0] = arr[0]
 
    # Traverse the array and update
    # the maximum value upto index i
    for i in range(1, N):
        preMax[i] = max(preMax[i - 1], arr[i])
 
    # Stores the maximum sum of pairs
    res = -10**8
 
    # Iterate over the range [K, N]
    for i in range(K, N):
       
        # Find the maximum value of
        # the sum of valid pairs
        res = max(res, arr[i] + preMax[i - K])
 
    # Return the resultant sum
    return res
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 4, 8, 6, 3]
    K = 3
    N = len(arr)
    print (getMaxPairSum(arr, N, K))
 
# This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG {
 
    // Function to find the largest sum
    // pair that are K distant apart
    static int getMaxPairSum(int[] arr, int N, int K)
    {
       
        // Stores the prefix maximum array
        int[] preMax = new int[N];
 
        // Base Case
        preMax[0] = arr[0];
 
        // Traverse the array and update
        // the maximum value upto index i
        for (int i = 1; i < N; i++) {
            preMax[i] = Math.Max(preMax[i - 1], arr[i]);
        }
 
        // Stores the maximum sum of pairs
        int res = Int32.MinValue;
 
        // Iterate over the range [K, N]
        for (int i = K; i < N; i++)
        {
           
            // Find the maximum value of
            // the sum of valid pairs
            res = Math.Max(res, arr[i] + preMax[i - K]);
        }
 
        // Return the resultant sum
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 1, 2, 4, 8, 6, 3 };
        int K = 3;
        int N = arr.Length;
        Console.Write(getMaxPairSum(arr, N, K));
    }
}
 
// This code is contributed by ukasp.


Javascript


输出:
9

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

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