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
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem