Maximum circular subarray sum of size K (original) (raw)
Last Updated : 23 Dec, 2022
Given an array arr of size N and an integer K, the task is to find the maximum sum subarray of size k among all contiguous sub-array (considering circular subarray also).
Examples:
Input: arr = {18, 4, 3, 4, 5, 6, 7, 8, 2, 10}, k = 3
Output:
max circular sum = 32
start index = 9
end index = 1
Explanation:
Maximum Sum = 10 + 18 + 4 = 32Input: arr = {8, 2, 5, 9}, k = 4
Output:
max circular sum = 24
start index = 0
end index = 3
Approach:
- Iterate the loop till (n + k) times and
- Take (i % n) to handle the case when the array index becomes greater than n.
Below is the implementation of above approach:
C++ `
// C++ program to find maximum circular // subarray sum of size k
#include <bits/stdc++.h> using namespace std;
// Function to calculate // maximum sum void maxCircularSum(int arr[], int n, int k) { // k must be greater if (n < k) { cout << "Invalid"; return; }
int sum = 0, start = 0, end = k - 1;
// calculate the sum of first k elements.
for (int i = 0; i < k; i++) {
sum += arr[i];
}
int ans = sum;
for (int i = k; i < n + k; i++) {
// add current element to sum
// and subtract the first element
// of the previous window.
sum += arr[i % n] - arr[(i - k) % n];
if (sum > ans) {
ans = sum;
start = (i - k + 1) % n;
end = i % n;
}
}
cout << "max circular sum = "
<< ans << endl;
cout << "start index = " << start
<< "\nend index = " << end << endl;
}
// Driver Code int main() { int arr[] = { 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 3;
maxCircularSum(arr, n, k);
return 0;
}
Java
// Java program to find maximum circular // subarray sum of size k
import java.util.*;
class GFG {
// Function to calculate
// maximum sum
static void maxCircularSum(int[] arr, int n, int k)
{
// k must be greater
if (n < k)
{
System.out.println("Invalid");
return;
}
int sum = 0, start = 0, end = k - 1;
// calculate the sum of first k elements.
for (int i = 0; i < k; i++)
sum += arr[i];
int ans = sum;
for (int i = k; i < n + k; i++)
{
// add current element to sum
// and subtract the first element
// of the previous window.
sum += arr[i % n] - arr[(i - k) % n];
if (sum > ans)
{
ans = sum;
start = (i - k + 1) % n;
end = i % n;
}
}
System.out.println("max circular sum = " + ans);
System.out.println("start index = " + start + "\nend index = " + end);
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 };
int n = arr.length;
int k = 3;
maxCircularSum(arr, n, k);
}
}
// This code is contributed by // sanjeev2552
Python3
Python3 program to find maximum circular
subarray sum of size k
Function to calculate
maximum sum
def maxCircularSum(arr, n, k) :
# k must be greater
if (n < k) :
print("Invalid");
return;
sum = 0; start = 0; end = k - 1;
# calculate the sum of first k elements.
for i in range(k) :
sum += arr[i];
ans = sum;
for i in range(k, n + k) :
# add current element to sum
# and subtract the first element
# of the previous window.
sum += arr[i % n] - arr[(i - k) % n];
if (sum > ans) :
ans = sum;
start = (i - k + 1) % n;
end = i % n;
print("max circular sum = ",ans);
print("start index = ", start,
"\nend index = ", end);
Driver Code
if name == "main" :
arr = [ 18, 4, 3, 4, 5, 6, 7, 8, 2, 10 ];
n = len(arr);
k = 3;
maxCircularSum(arr, n, k);
This code is contributed by AnkitRai01
C#
// C# program to find maximum circular // subarray sum of size k using System;
class GFG {
// Function to calculate
// maximum sum
static void maxCircularSum(int[] arr,
int n, int k)
{
// k must be greater
if (n < k)
{
Console.WriteLine("Invalid");
return;
}
int sum = 0, start = 0, end = k - 1;
// calculate the sum of first k elements.
for (int i = 0; i < k; i++)
sum += arr[i];
int ans = sum;
for (int i = k; i < n + k; i++)
{
// add current element to sum
// and subtract the first element
// of the previous window.
sum += arr[i % n] - arr[(i - k) % n];
if (sum > ans)
{
ans = sum;
start = (i - k + 1) % n;
end = i % n;
}
}
Console.WriteLine("max circular sum = " + ans);
Console.WriteLine("start index = " + start +
"\nend index = " + end);
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 18, 4, 3, 4, 5,
6, 7, 8, 2, 10 };
int n = arr.Length;
int k = 3;
maxCircularSum(arr, n, k);
}
}
// This code is contributed by 29AjayKumar
JavaScript
`
Output:
max circular sum = 32 start index = 9 end index = 1
**Time Complexity:**O(N)
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
- Maximum Circular Subarray Sum Given a circular array arr[] of size n, find the maximum possible sum of a non-empty subarray.Examples: Input: arr[] = {8, -8, 9, -9, 10, -11, 12}Output: 22Explanation: Circular Subarray {12, 8, -8, 9, -9, 10} has the maximum sum, which is 22.Input: arr[] = {10, -3, -4, 7, 6, 5, -4, -1}Output: 23 Ex 15+ min read
- Maximum sum subarray of size range [L, R] Given an integer array arr[] of size N and two integer L and R. The task is to find the maximum sum subarray of size between L and R (both inclusive). Example: Input: arr[] = {1, 2, 2, 1}, L = 1, R = 3 Output: 5 Explanation: Subarray of size 1 are {1}, {2}, {2}, {1} and maximum sum subarray = 2 for 8 min read
- Size of The Subarray With Maximum Sum Given an array arr[] of size N, the task is to find the length of the subarray having maximum sum. Examples : Input : a[] = {1, -2, 1, 1, -2, 1} Output : Length of the subarray is 2 Explanation : Subarray with consecutive elements and maximum sum will be {1, 1}. So length is 2 Input : ar[] = { -2, - 10 min read
- Maximum Subarray Sum in C++ In this article, we will learn how to find the maximum sum of a contiguous subarray within a given array of integers in C++ language. Finding the maximum subarray sum involves determining the contiguous subarray that has the largest sum.Example:Input:arr[] = {-2, 1, -3, 4, -1, 2, 1, -5, 4}Output:6Ex 7 min read
- Sum of all subarrays of size K Given an array arr[] and an integer K, the task is to calculate the sum of all subarrays of size K. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}, K = 3 Output: 6 9 12 15 Explanation: All subarrays of size k and their sum: Subarray 1: {1, 2, 3} = 1 + 2 + 3 = 6 Subarray 2: {2, 3, 4} = 2 + 3 + 4 = 9 Sub 11 min read
- Find maximum (or minimum) sum of a subarray of size k Given an array of integers and a number k, find the maximum sum of a subarray of size k. Examples: Input : arr[] = {100, 200, 300, 400}, k = 2Output : 700Input : arr[] = {1, 4, 2, 10, 23, 3, 1, 0, 20}, k = 4 Output : 39Explanation: We get maximum sum by adding subarray {4, 2, 10, 23} of size 4.Input 14 min read
- Maximum Unique Element in every subarray of size K Given an array and an integer K. We need to find the maximum of every segment of length K which has no duplicates in that segment. Examples: Input : a[] = {1, 2, 2, 3, 3}, K = 3. Output : 1 3 2 For segment (1, 2, 2), Maximum = 1. For segment (2, 2, 3), Maximum = 3. For segment (2, 3, 3), Maximum = 2 7 min read
- Maximum count of distinct sized subarrays with given sum Given a binary array arr[] of N integers, the task is to find the maximum count of distinct sized subarrays such that the sum of each subarray is K. Example: Input: arr[] = {0, 1, 1 , 0}, K = 2Output: 3Explanation: The subset {{0, 1, 1, 0}, {0, 1, 1}, {1, 1}} is the subset of 3 subarrays such that t 7 min read
- Maximum sum bitonic subarray Given an array containing n numbers. The problem is to find the maximum sum bitonic subarray. A bitonic subarray is a subarray in which elements are first increasing and then decreasing. A strictly increasing or strictly decreasing subarray is also considered a bitonic subarray. Time Complexity of O 15+ min read
- Maximum Subarray Sum - Kadane's Algorithm Given an array arr[], the task is to find the subarray that has the maximum sum and return its sum.Examples:Input: arr[] = {2, 3, -8, 7, -1, 2, 3}Output: 11Explanation: The subarray {7, -1, 2, 3} has the largest sum 11.Input: arr[] = {-2, -4}Output: -2Explanation: The subarray {-2} has the largest s 9 min read