Largest sum subarray of size K containing consecutive elements
Last Updated :
25 Jan, 2023
Given an array arr[] consisting of N positive integers and a positive integer K, the task is to find the maximum sum of the subarray of size K such that it contains K consecutive elements in any combination.
Examples:
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
Approach: The given problem can be solved by checking every subarray of size K whether it contains consecutive elements or not and then maximize the sum of the subarray accordingly. Follow the steps below to solve the problem:
- Initialize a variable, say currSum to store the sum of the current subarray of K elements if the elements are consecutive.
- Initialize a variable maxSum that stores the maximum resultant sum of any subarray of size K.
- Iterate over the range [0, N – K] using the variable i and perform the following steps:
- After completing the above steps, print the value of maxSum as the result.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int maximumSum(vector< int > A, int N,
int K)
{
int curr_sum = 0;
int max_sum = INT_MIN;
for ( int i = 0; i < N - K + 1; i++) {
vector< int > dupl_arr(
A.begin() + i,
A.begin() + i + K);
sort(dupl_arr.begin(),
dupl_arr.end());
bool flag = true ;
for ( int j = 1; j < K; j++) {
if (dupl_arr[j]
- dupl_arr[j - 1]
!= 1) {
flag = false ;
break ;
}
}
if (flag) {
int temp = 0;
curr_sum = accumulate(
dupl_arr.begin(),
dupl_arr.end(), temp);
max_sum = max(max_sum,
curr_sum);
curr_sum = 0;
}
}
return max_sum;
}
int main()
{
vector< int > 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
import java.util.Arrays;
class GFG
{
public static Integer maximumSum( int [] A, int N, int K)
{
int curr_sum = 0 ;
int max_sum = Integer.MIN_VALUE;
for ( int i = 0 ; i < N - K + 1 ; i++) {
int [] dupl_arr = Arrays.copyOfRange(A, i, i + K);
Arrays.sort(dupl_arr);
Boolean flag = true ;
for ( int j = 1 ; j < K; j++) {
if (dupl_arr[j] - dupl_arr[j - 1 ]
!= 1 ) {
flag = false ;
break ;
}
}
if (flag) {
int temp = 0 ;
curr_sum = 0 ;
for ( int x = 0 ; x < dupl_arr.length; x++){
curr_sum += dupl_arr[x];
}
max_sum = Math.max(max_sum,
curr_sum);
curr_sum = 0 ;
}
}
return max_sum;
}
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));
}
}
|
Python3
import sys
def maximumSum(A, N, K):
curr_sum = 0
max_sum = - sys.maxsize - 1
for i in range (N - K + 1 ):
dupl_arr = A[i:i + K]
dupl_arr.sort()
flag = True
for j in range ( 1 , K, 1 ):
if (dupl_arr[j] - dupl_arr[j - 1 ] ! = 1 ):
flag = False
break
if (flag):
temp = 0
curr_sum = temp
curr_sum = sum (dupl_arr)
max_sum = max (max_sum, curr_sum)
curr_sum = 0
return max_sum
if __name__ = = '__main__' :
arr = [ 10 , 12 , 9 , 8 , 10 ,
15 , 1 , 3 , 2 ]
K = 3
N = len (arr)
print (maximumSum(arr, N, K))
|
C#
using System;
public class GFG {
public static int maximumSum( int [] A, int N, int K) {
int curr_sum = 0;
int max_sum = int .MinValue;
for ( int i = 0; i < N - K + 1; i++) {
int [] dupl_arr = new int [K];
Array.Copy(A, i, dupl_arr, 0, K);
Array.Sort(dupl_arr);
bool flag = true ;
for ( int j = 1; j < K; j++) {
if (dupl_arr[j] - dupl_arr[j - 1] != 1) {
flag = false ;
break ;
}
}
if (flag) {
curr_sum = 0;
for ( int x = 0; x < dupl_arr.Length; x++) {
curr_sum += dupl_arr[x];
}
max_sum = Math.Max(max_sum,curr_sum);
curr_sum = 0;
}
}
return max_sum;
}
public static void Main() {
int [] arr = { 10, 12, 9, 8, 10, 15, 1, 3, 2 };
int K = 3;
int N = arr.Length;
Console.WriteLine(maximumSum(arr, N, K));
}
}
|
Javascript
<script>
function maximumSum(A, N, K) {
let curr_sum = 0;
let max_sum = Number.MIN_SAFE_INTEGER;
for (let i = 0; i < N - K + 1; i++) {
let dupl_arr = [...A.slice(i, i + K)];
dupl_arr.sort((a, b) => a - b)
let flag = true ;
for (let j = 1; j < K; j++) {
if (dupl_arr[j]
- dupl_arr[j - 1]
!= 1) {
flag = false ;
break ;
}
}
if (flag) {
let temp = 0;
curr_sum = dupl_arr.reduce((acc, cur) => acc + cur, 0)
max_sum = Math.max(max_sum,
curr_sum);
curr_sum = 0;
}
}
return max_sum;
}
let arr = [10, 12, 9, 8, 10,
15, 1, 3, 2];
let K = 3;
let N = arr.length;
document.write(maximumSum(arr, N, K));
</script>
|
Time Complexity: O(N*K*log K)
Auxiliary Space: O(K)
Share your thoughts in the comments
Please Login to comment...