包含连续元素的大小为 K 的最大和子数组
给定一个由N个正整数和一个正整数K组成的数组arr[] ,任务是找到大小为K的子数组的最大和,使得它包含任意组合的K个连续元素。
例子:
Input: arr[] = {10, 12, 9, 8, 10, 15, 1, 3, 2}, K = 3
Output: 27
Explanation:
The subarray having K (= 3) consecutive elements is {9, 8, 10} whose sum of elements is 9 + 8 + 10 = 27, which is maximum.
Input: arr[] = {7, 20, 2, 3, 4}, K = 2
Output: 7
方法:给定的问题可以通过检查每个大小为K的子数组是否包含连续元素,然后相应地最大化子数组的总和来解决。请按照以下步骤解决问题:
- 如果元素是连续的,则初始化一个变量,例如currSum以存储当前K个元素的子数组的总和。
- 初始化一个变量maxSum ,该变量存储任何大小为K的子数组的最大结果总和。
- 使用变量i迭代范围[0, N – K]并执行以下步骤:
- 将从i开始的K个元素存储在数组V[]中。
- 按升序对数组V[]进行排序。
- 遍历数组V[]以检查所有元素是否连续。如果发现为真,则将当前子数组的总和存储在currSum中,并将 maxSum 更新为maxSum和currSum的最大值。
- 完成上述步骤后,打印maxSum的值作为结果。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the largest sum
// subarray such that it contains K
// consecutive elements
int maximumSum(vector A, int N,
int K)
{
// Stores sum of subarray having
// K consecutive elements
int curr_sum = 0;
// Stores the maximum sum among all
// subarrays of size K having
// consecutive elements
int max_sum = INT_MIN;
// Traverse the array
for (int i = 0; i < N - K + 1; i++) {
// Store K elements of one
// subarray at a time
vector dupl_arr(
A.begin() + i,
A.begin() + i + K);
// Sort the duplicate array
// in ascending order
sort(dupl_arr.begin(),
dupl_arr.end());
// Checks if elements in subarray
// are consecutive or not
bool flag = true;
// Traverse the k elements
for (int j = 1; j < K; j++) {
// If not consecutive, break
if (dupl_arr[j]
- dupl_arr[j - 1]
!= 1) {
flag = false;
break;
}
}
// If flag is true update the
// maximum sum
if (flag) {
int temp = 0;
// Stores the sum of elements
// of the current subarray
curr_sum = accumulate(
dupl_arr.begin(),
dupl_arr.end(), temp);
// Update the max_sum
max_sum = max(max_sum,
curr_sum);
// Reset curr_sum
curr_sum = 0;
}
}
// Return the result
return max_sum;
}
// Driver Code
int main()
{
vector arr = { 10, 12, 9, 8, 10,
15, 1, 3, 2 };
int K = 3;
int N = arr.size();
cout << maximumSum(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.util.Arrays;
class GFG
{
// Function to find the largest sum
// subarray such that it contains K
// consecutive elements
public static Integer maximumSum(int[] A, int N, int K)
{
// Stores sum of subarray having
// K consecutive elements
int curr_sum = 0;
// Stores the maximum sum among all
// subarrays of size K having
// consecutive elements
int max_sum = Integer.MIN_VALUE;
// Traverse the array
for (int i = 0; i < N - K + 1; i++) {
// Store K elements of one
// subarray at a time
int[] dupl_arr = Arrays.copyOfRange(A, i, i + K);
// Sort the duplicate array
// in ascending order
Arrays.sort(dupl_arr);
// Checks if elements in subarray
// are consecutive or not
Boolean flag = true;
// Traverse the k elements
for (int j = 1; j < K; j++) {
// If not consecutive, break
if (dupl_arr[j] - dupl_arr[j - 1]
!= 1) {
flag = false;
break;
}
}
// If flag is true update the
// maximum sum
if (flag) {
int temp = 0;
// Stores the sum of elements
// of the current subarray
curr_sum = 0;
for(int x = 0; x < dupl_arr.length; x++){
curr_sum += dupl_arr[x];
}
// Update the max_sum
max_sum = Math.max(max_sum,
curr_sum);
// Reset curr_sum
curr_sum = 0;
}
}
// Return the result
return max_sum;
}
// Driver Code
public static void main(String args[]) {
int[] arr = { 10, 12, 9, 8, 10, 15, 1, 3, 2 };
int K = 3;
int N = arr.length;
System.out.println(maximumSum(arr, N, K));
}
}
// This code is contributed by _saurabh_jaiswal.
Python3
# Python3 program for the above approach
import sys
# Function to find the largest sum
# subarray such that it contains K
# consecutive elements
def maximumSum(A, N, K):
# Stores sum of subarray having
# K consecutive elements
curr_sum = 0
# Stores the maximum sum among all
# subarrays of size K having
# consecutive elements
max_sum = -sys.maxsize - 1
# Traverse the array
for i in range(N - K + 1):
# Store K elements of one
# subarray at a time
dupl_arr = A[i:i + K]
# Sort the duplicate array
# in ascending order
dupl_arr.sort()
# Checks if elements in subarray
# are consecutive or not
flag = True
# Traverse the k elements
for j in range(1, K, 1):
# If not consecutive, break
if (dupl_arr[j] - dupl_arr[j - 1] != 1):
flag = False
break
# If flag is true update the
# maximum sum
if (flag):
temp = 0
# Stores the sum of elements
# of the current subarray
curr_sum = temp
curr_sum = sum(dupl_arr)
# Update the max_sum
max_sum = max(max_sum, curr_sum)
# Reset curr_sum
curr_sum = 0
# Return the result
return max_sum
# Driver Code
if __name__ == '__main__':
arr = [ 10, 12, 9, 8, 10,
15, 1, 3, 2 ]
K = 3
N = len(arr)
print(maximumSum(arr, N, K))
# This code is contributed by SURENDRA_GANGWAR
Javascript
输出:
27
时间复杂度: O(N*K*log K)
辅助空间: O(K)