📌  相关文章
📜  最大子序列总和,以使所有元素相距K个距离

📅  最后修改于: 2021-04-21 21:14:05             🧑  作者: Mango

给定一个由N个整数和另一个整数K组成的数组arr [] 。任务是找到一个子序列的最大和,以使原始数组中该子序列中所有连续元素的索引之差恰好为K。例如,如果arr [i]是子序列的第一个元素,则下一个元素必须是arr [i + k],然后是arr [i + 2k] ,依此类推。

例子:

方法:存在K个序列,其中元素之间相距K个距离,这些序列可以是以下形式:

现在,序列的任何子数组都是原始数组的子序列,其中元素彼此相距K距离。

因此,现在该任务减少了,以找到这些序列的最大子阵列和,可以通过Kadane的算法找到它们。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximum subarray sum
// for the array {a[i], a[i + k], a[i + 2k], ...}
int maxSubArraySum(int a[], int n, int k, int i)
{
    int max_so_far = INT_MIN, max_ending_here = 0;
  
    while (i < n) {
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;
  
        if (max_ending_here < 0)
            max_ending_here = 0;
  
        i += k;
    }
    return max_so_far;
}
  
// Function to return the sum of
// the maximum required subsequence
int find(int arr[], int n, int k)
{
    // To store the result
    int maxSum = 0;
  
    // Run a loop from 0 to k
    for (int i = 0; i <= min(n, k); i++) {
        int sum = 0;
  
        // Find the maximum subarray sum for the
        // array {a[i], a[i + k], a[i + 2k], ...}
        maxSum = max(maxSum,
                     maxSubArraySum(arr, n, k, i));
    }
  
    // Return the maximum value
    return maxSum;
}
  
// Driver code
int main()
{
    int arr[] = { 2, -3, -1, -1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int k = 2;
  
    cout << find(arr, n, k);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
  
class GFG
{
  
// Function to return the maximum subarray sum 
// for the array {a[i], a[i + k], a[i + 2k], ...} 
static int maxSubArraySum(int a[], int n, 
                          int k, int i) 
{ 
    int max_so_far = Integer.MIN_VALUE,
        max_ending_here = 0; 
  
    while (i < n) 
    { 
        max_ending_here = max_ending_here + a[i]; 
        if (max_so_far < max_ending_here) 
            max_so_far = max_ending_here; 
  
        if (max_ending_here < 0) 
            max_ending_here = 0; 
  
        i += k; 
    } 
    return max_so_far; 
} 
  
// Function to return the sum of 
// the maximum required subsequence 
static int find(int arr[], int n, int k) 
{ 
    // To store the result 
    int maxSum = 0; 
  
    // Run a loop from 0 to k 
    for (int i = 0; i <= Math.min(n, k); i++)
    { 
        int sum = 0; 
  
        // Find the maximum subarray sum for the 
        // array {a[i], a[i + k], a[i + 2k], ...} 
        maxSum = Math.max(maxSum, 
                      maxSubArraySum(arr, n, k, i)); 
    } 
  
    // Return the maximum value 
    return maxSum; 
} 
  
// Driver code 
public static void main(String[] args) 
{
    int arr[] = {2, -3, -1, -1, 2};
    int n = arr.length;
    int k = 2;
  
    System.out.println(find(arr, n, k));
}
} 
  
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
# Function to return the maximum subarray sum
# for the array {a[i], a[i + k], a[i + 2k], ...}
import sys
def maxSubArraySum(a, n, k, i):
  
    max_so_far = -sys.maxsize;
    max_ending_here = 0;
  
    while (i < n):
        max_ending_here = max_ending_here + a[i];
        if (max_so_far < max_ending_here):
            max_so_far = max_ending_here;
  
        if (max_ending_here < 0):
            max_ending_here = 0;
  
        i += k;
      
    return max_so_far;
  
# Function to return the sum of
# the maximum required subsequence
def find(arr, n, k):
  
    # To store the result
    maxSum = 0;
  
    # Run a loop from 0 to k
    for i in range(0, min(n, k) + 1):
        sum = 0;
  
        # Find the maximum subarray sum for the
        # array {a[i], a[i + k], a[i + 2k], ...}
        maxSum = max(maxSum,
                     maxSubArraySum(arr, n, k, i));
      
    # Return the maximum value
    return maxSum;
  
# Driver code
if __name__ == '__main__':
    arr = [ 2, -3, -1, -1, 2 ];
    n = len(arr);
    k = 2;
  
    print(find(arr, n, k));
  
# This code is contributed by Princi Singh


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the maximum subarray sum 
    // for the array {a[i], a[i + k], a[i + 2k], ...} 
    static int maxSubArraySum(int []a, int n, 
                              int k, int i) 
    { 
        int max_so_far = int.MinValue,
            max_ending_here = 0; 
      
        while (i < n) 
        { 
            max_ending_here = max_ending_here + a[i]; 
            if (max_so_far < max_ending_here) 
                max_so_far = max_ending_here; 
      
            if (max_ending_here < 0) 
                max_ending_here = 0; 
      
            i += k; 
        } 
        return max_so_far; 
    } 
      
    // Function to return the sum of 
    // the maximum required subsequence 
    static int find(int []arr, int n, int k) 
    { 
        // To store the result 
        int maxSum = 0; 
      
        // Run a loop from 0 to k 
        for (int i = 0; i <= Math.Min(n, k); i++)
        { 
  
            // Find the maximum subarray sum for the 
            // array {a[i], a[i + k], a[i + 2k], ...} 
            maxSum = Math.Max(maxSum, 
                              maxSubArraySum(arr, n, k, i)); 
        } 
      
        // Return the maximum value 
        return maxSum; 
    } 
      
    // Driver code 
    public static void Main() 
    {
        int []arr = {2, -3, -1, -1, 2};
        int n = arr.Length;
        int k = 2;
      
        Console.WriteLine(find(arr, n, k));
    }    
}
  
// This code is contributed by AnkitRai01


输出:
3