Largest subarray having sum greater than k
Last Updated :
12 Feb, 2025
Given an array of integers and a value k, the task is to find the length of the largest subarray having a sum greater than k.
Examples:
Input: arr[] = [-2, 1, 6, -3], k = 5
Output: 2
Explanation: The largest subarray with a sum greater than 5 is {1, 6}.
Input: arr[] = [2, -3, 3, 2, 0, -1], k = 3
Output: 5
Explanation: The largest subarray with a sum greater than 3 is {2, -3, 3, 2, 0}.
[Naive Approach] - Generating All Subarrays - O(n ^ 2) Time and O(1) Space
- The idea is to consider each subarray one by one and find its sum.
- If the sum is greater than k, then compare the length of this subarray with maximum length found so far.
C++
// C++ program to find the largest
// subarray with sum greater than k
#include <bits/stdc++.h>
using namespace std;
int largestSubarray(vector<int> &arr, int k) {
// to store the result
int ans = 0;
// consider all subarrays starting from i
for(int i = 0; i < arr.size(); i++) {
// to store the sum of the subarray
int sum = 0;
for(int j = i; j < arr.size(); j++) {
sum += arr[j];
// if the sum is greater than k
if(sum > k) {
// update the result
ans = max(ans, j - i + 1);
}
}
}
return ans;
}
int main() {
vector<int> arr = {2, -3, 3, 2, 0, -1};
int k = 3;
cout << largestSubarray(arr, k);
return 0;
}
Java
// Java program to find the largest
// subarray with sum greater than k
class GFG {
static int largestSubarray(int[] arr, int k) {
// to store the result
int ans = 0;
// consider all subarrays starting from i
for (int i = 0; i < arr.length; i++) {
// to store the sum of the subarray
int sum = 0;
for (int j = i; j < arr.length; j++) {
sum += arr[j];
// if the sum is greater than k
if (sum > k) {
// update the result
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
public static void main(String[] args) {
int[] arr = {2, -3, 3, 2, 0, -1};
int k = 3;
System.out.println(largestSubarray(arr, k));
}
}
Python
# Python program to find the largest
# subarray with sum greater than k
def largestSubarray(arr, k):
# to store the result
ans = 0
# consider all subarrays starting from i
for i in range(len(arr)):
# to store the sum of the subarray
sum = 0
for j in range(i, len(arr)):
sum += arr[j]
# if the sum is greater than k
if sum > k:
# update the result
ans = max(ans, j - i + 1)
return ans
# Driver code
arr = [2, -3, 3, 2, 0, -1]
k = 3
print(largestSubarray(arr, k))
C#
// C# program to find the largest
// subarray with sum greater than k
using System;
class GFG {
static int largestSubarray(int[] arr, int k) {
// to store the result
int ans = 0;
// consider all subarrays starting from i
for (int i = 0; i < arr.Length; i++) {
// to store the sum of the subarray
int sum = 0;
for (int j = i; j < arr.Length; j++) {
sum += arr[j];
// if the sum is greater than k
if (sum > k) {
// update the result
ans = Math.Max(ans, j - i + 1);
}
}
}
return ans;
}
public static void Main() {
int[] arr = {2, -3, 3, 2, 0, -1};
int k = 3;
Console.WriteLine(largestSubarray(arr, k));
}
}
JavaScript
// JavaScript program to find the largest
// subarray with sum greater than k
function largestSubarray(arr, k) {
// to store the result
let ans = 0;
// consider all subarrays starting from i
for (let i = 0; i < arr.length; i++) {
// to store the sum of the subarray
let sum = 0;
for (let j = i; j < arr.length; j++) {
sum += arr[j];
// if the sum is greater than k
if (sum > k) {
// update the result
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
// Driver code
let arr = [2, -3, 3, 2, 0, -1];
let k = 3;
console.log(largestSubarray(arr, k));
Time Complexity: O(n^2), as we are using nested loops to find sum of all the subarrays.
Space Complexity: O(1)
[Expected Approach] - Using Prefix Sum and Binary Search - O(n * log(n)) Time and O(n) Space
The idea is to traverse the array and store prefix sum and corresponding array index in a vector of pairs. After finding prefix sum, sort the vector in increasing order of prefix sum, and for the same value of prefix sum, sort according to index. Create another array minInd[], in which minInd[i] stores the minimum index value in range [0..i] in sorted prefix sum vector. After this, start traversing array arr[] and store sum of subarray arr[0..i] in sum.
- If the sum is greater than k, then-largest subarray having a sum greater than k is arr[0..i] having length i + 1.
- If the sum is less than or equal to k, then a value greater than or equal to k + 1 - sum has to be added to sum to make it at least k+1.
Let this value be x.
Steps:
- Compute Prefix Sum:
- Traverse the array and store prefix sums along with their indices in a vector of pairs.
- Sort Prefix Sum Vector:
- Sort the vector based on prefix sums, and for duplicate prefix sums, sort by index.
- Build
minInd[]
Array:- Construct
minInd[]
where minInd[i]
stores the minimum index in the prefix sum vector for range [0..i]
.
- Traverse the Array and Compute Subarray Sums:
- Maintain a running sum of elements from
arr[0]
to arr[i]
. - If
sum > k
, update the largest subarray length as i + 1
. - If
sum <= k
, compute x = k + 1 - sum
, meaning we need to find a prefix sum at most -x
to form a valid subarray.
- Use Binary Search for Optimization:
- Perform a binary search on the prefix sum vector to find the largest index
ind
where preSum[ind] <= -x
. - If
minInd[ind] < i
, update the maximum subarray length.
Below is given the implementation:
C++
// C++ program to find the largest
// subarray with sum greater than k
#include <bits/stdc++.h>
using namespace std;
// Function to find index in preSum vector upto which
// all prefix sum values are less than or equal to val.
int findInd(vector<pair<int,int>>& preSum, int val) {
// Starting and ending index of search space.
int l = 0;
int h = preSum.size() - 1;
// To store required index value.
int ans = -1;
while (l <= h) {
int mid = (l + h) / 2;
if (preSum[mid].first <= val) {
ans = mid;
l = mid + 1;
}
else
h = mid - 1;
}
return ans;
}
// Function to find the largest subarray with sum
// greater than k.
int largestSubarray(vector<int> &arr, int k) {
int n = arr.size();
// to store the result
int result = 0;
// array to store pair of prefix sum
// and corresponding ending index value.
vector<pair<int,int>> preSum;
// To store current value of prefix sum.
int sum = 0;
// Insert values in preSum vector.
for (int i = 0; i < n; i++) {
sum = sum + arr[i];
preSum.push_back({ sum, i });
}
// Sort preSum array.
sort(preSum.begin(), preSum.end());
// To store minimum index value in range
// 0..i of preSum vector.
vector<int> minInd(n);
// Update minInd array.
minInd[0] = preSum[0].second;
for (int i = 1; i < n; i++) {
minInd[i] = min(minInd[i - 1], preSum[i].second);
}
sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + arr[i];
// If sum is greater than k, then
// update the result.
if (sum > k)
result = i + 1;
// If sum is less than or equal to k, then
// find index upto which all prefix sum values
// are less than or equal to sum - k - 1.
else {
int ind = findInd(preSum, sum - k - 1);
if (ind != -1 && minInd[ind] < i)
result = max(result, i - minInd[ind]);
}
}
return result;
}
int main() {
vector<int> arr = {2, -3, 3, 2, 0, -1};
int k = 3;
cout << largestSubarray(arr, k);
return 0;
}
Java
// Java program to find the largest
// subarray with sum greater than k
import java.util.*;
class GFG {
// Function to find index in preSum array upto which
// all prefix sum values are less than or equal to val.
static int findInd(int[][] preSum, int val) {
// Starting and ending index of search space.
int l = 0;
int h = preSum.length - 1;
// To store required index value.
int ans = -1;
while (l <= h) {
int mid = (l + h) / 2;
if (preSum[mid][0] <= val) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
// Function to find the largest subarray with sum
// greater than k.
static int largestSubarray(int[] arr, int k) {
int n = arr.length;
// to store the result
int result = 0;
// array to store pair of prefix sum
// and corresponding ending index value.
int[][] preSum = new int[n][2];
// To store current value of prefix sum.
int sum = 0;
// Insert values in preSum array.
for (int i = 0; i < n; i++) {
sum = sum + arr[i];
preSum[i][0] = sum;
preSum[i][1] = i;
}
// Sort preSum array.
Arrays.sort(preSum, Comparator.comparingInt(a -> a[0]));
// To store minimum index value in range
// 0..i of preSum array.
int[] minInd = new int[n];
// Update minInd array.
minInd[0] = preSum[0][1];
for (int i = 1; i < n; i++) {
minInd[i] = Math.min(minInd[i - 1], preSum[i][1]);
}
sum = 0;
for (int i = 0; i < n; i++) {
sum = sum + arr[i];
// If sum is greater than k, then
// update the result.
if (sum > k)
result = i + 1;
// If sum is less than or equal to k, then
// find index upto which all prefix sum values
// are less than or equal to sum - k - 1.
else {
int ind = findInd(preSum, sum - k - 1);
if (ind != -1 && minInd[ind] < i)
result = Math.max(result, i - minInd[ind]);
}
}
return result;
}
public static void main(String[] args) {
int[] arr = {2, -3, 3, 2, 0, -1};
int k = 3;
System.out.println(largestSubarray(arr, k));
}
}
Python
# Python program to find the largest
# subarray with sum greater than k
# Function to find index in preSum list upto which
# all prefix sum values are less than or equal to val.
def findInd(preSum, val):
# Starting and ending index of search space.
l = 0
h = len(preSum) - 1
# To store required index value.
ans = -1
while l <= h:
mid = (l + h) // 2
if preSum[mid][0] <= val:
ans = mid
l = mid + 1
else:
h = mid - 1
return ans
# Function to find the largest subarray with sum
# greater than k.
def largestSubarray(arr, k):
n = len(arr)
# to store the result
result = 0
# list to store pair of prefix sum
# and corresponding ending index value.
preSum = []
# To store current value of prefix sum.
sum = 0
# Insert values in preSum list.
for i in range(n):
sum += arr[i]
preSum.append((sum, i))
# Sort preSum list.
preSum.sort()
# To store minimum index value in range
# 0..i of preSum list.
minInd = [0] * n
# Update minInd list.
minInd[0] = preSum[0][1]
for i in range(1, n):
minInd[i] = min(minInd[i - 1], preSum[i][1])
sum = 0
for i in range(n):
sum += arr[i]
# If sum is greater than k, then
# update the result.
if sum > k:
result = i + 1
# If sum is less than or equal to k, then
# find index upto which all prefix sum values
# are less than or equal to sum - k - 1.
else:
ind = findInd(preSum, sum - k - 1)
if ind != -1 and minInd[ind] < i:
result = max(result, i - minInd[ind])
return result
if __name__ == "__main__":
arr = [2, -3, 3, 2, 0, -1]
k = 3
print(largestSubarray(arr, k))
C#
// C# program to find the largest
// subarray with sum greater than k
using System;
using System.Collections.Generic;
class GFG {
// Function to find index in preSum array upto which
// all prefix sum values are less than or equal to val.
static int findInd(List<Tuple<int, int>> preSum, int val) {
// Starting and ending index of search space.
int l = 0;
int h = preSum.Count - 1;
// To store required index value.
int ans = -1;
while (l <= h) {
int mid = (l + h) / 2;
if (preSum[mid].Item1 <= val) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
static int largestSubarray(int[] arr, int k) {
int n = arr.Length;
// to store the result
int result = 0;
// List to store pair of prefix sum
// and corresponding ending index value.
List<Tuple<int, int>> preSum = new List<Tuple<int, int>>();
// To store current value of prefix sum.
int sum = 0;
// Insert values in preSum list.
for (int i = 0; i < n; i++) {
sum += arr[i];
preSum.Add(new Tuple<int, int>(sum, i));
}
// Sort preSum list.
preSum.Sort((a, b) => a.Item1.CompareTo(b.Item1));
// To store minimum index value in range
// 0..i of preSum list.
int[] minInd = new int[n];
// Update minInd list.
minInd[0] = preSum[0].Item2;
for (int i = 1; i < n; i++) {
minInd[i] = Math.Min(minInd[i - 1], preSum[i].Item2);
}
sum = 0;
for (int i = 0; i < n; i++) {
sum += arr[i];
if (sum > k)
result = i + 1;
else {
int ind = findInd(preSum, sum - k - 1);
if (ind != -1 && minInd[ind] < i)
result = Math.Max(result, i - minInd[ind]);
}
}
return result;
}
public static void Main() {
int[] arr = {2, -3, 3, 2, 0, -1};
int k = 3;
Console.WriteLine(largestSubarray(arr, k));
}
}
JavaScript
// JavaScript program to find the largest
// subarray with sum greater than k
// Function to find index in preSum array upto which
// all prefix sum values are less than or equal to val.
function findInd(preSum, val) {
// Starting and ending index of search space.
let l = 0;
let h = preSum.length - 1;
// To store required index value.
let ans = -1;
while (l <= h) {
let mid = Math.floor((l + h) / 2);
if (preSum[mid][0] <= val) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
// Function to find the largest subarray with sum
// greater than k.
function largestSubarray(arr, k) {
let n = arr.length;
// to store the result
let result = 0;
// array to store pair of prefix sum
// and corresponding ending index value.
let preSum = [];
// To store current value of prefix sum.
let sum = 0;
// Insert values in preSum array.
for (let i = 0; i < n; i++) {
sum = sum + arr[i];
preSum.push([sum, i]);
}
// Sort preSum array.
preSum.sort((a, b) => a[0] - b[0]);
// To store minimum index value in range
// 0..i of preSum array.
let minInd = new Array(n);
// Update minInd array.
minInd[0] = preSum[0][1];
for (let i = 1; i < n; i++) {
minInd[i] = Math.min(minInd[i - 1], preSum[i][1]);
}
sum = 0;
for (let i = 0; i < n; i++) {
sum = sum + arr[i];
// If sum is greater than k, then
// update the result.
if (sum > k)
result = i + 1;
// If sum is less than or equal to k, then
// find index upto which all prefix sum values
// are less than or equal to sum - k - 1.
else {
let ind = findInd(preSum, sum - k - 1);
if (ind !== -1 && minInd[ind] < i)
result = Math.max(result, i - minInd[ind]);
}
}
return result;
}
// Driver code
let arr = [2, -3, 3, 2, 0, -1];
let k = 3;
console.log(largestSubarray(arr, k));
Time Complexity: O(n * log(n)), for each index of arr[] we are applying binary search to find the required index which works in O(log n) time, thus the overall time complexity will be O(n * log(n)).
Auxiliary Space: O(n), to create minInd[] and preSum[] array of size n each.
Similar Reads
K-th Largest Sum Contiguous Subarray Given an array arr[] of size n, the task is to find the kth largest sum of contiguous subarray within the array of numbers that has both negative and positive numbers.Examples: Input: arr[] = [20, -5, -1], k = 3Output: 14Explanation: All sum of contiguous subarrays are (20, 15, 14, -5, -6, -1), so t
15+ min read
Longest Subarray having Majority Elements Greater Than K Given an array arr[] and an integer k, the task is to find the length of longest subarray in which the count of elements greater than k is more than the count of elements less than or equal to k.Examples:Input: arr[]= [1, 2, 3, 4, 1], k = 2Output: 3 Explanation: The subarray [2, 3, 4] or [3, 4, 1] s
13 min read
Longest subarray having sum of elements atmost K Given an array arr[] of size N and an integer K, the task is to find the length of the largest subarray having the sum of its elements at most K, where K > 0.Examples: Input: arr[] = {1, 2, 1, 0, 1, 1, 0}, k = 4Output: 5Explanation: {1, 2, 1} => sum = 4, length = 3 {1, 2, 1, 0}, {2, 1, 0, 1} =
12 min read
Longest Subarray With Sum K Given an array arr[] of size n containing integers, the task is to find the length of the longest subarray having sum equal to the given value k.Note: If there is no subarray with sum equal to k, return 0.Examples: Input: arr[] = [10, 5, 2, 7, 1, -10], k = 15Output: 6Explanation: Subarrays with sum
10 min read
Longest Subarray with sum differences ⤠K Given a sorted array arr[] of size N, the task is to find the length of the longest subarray and print the subarray such that the sum of the differences of the maximum element of the chosen subarray with all other elements of that same subarray is ⤠K.i.e. â(amax-ai) ⤠K, for that given subarray. Ex
7 min read