Open In App

Largest subarray having sum greater than k

Last Updated : 12 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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));

Output
5

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:

  1. Compute Prefix Sum:
    • Traverse the array and store prefix sums along with their indices in a vector of pairs.
  2. Sort Prefix Sum Vector:
    • Sort the vector based on prefix sums, and for duplicate prefix sums, sort by index.
  3. Build minInd[] Array:
    • Construct minInd[] where minInd[i] stores the minimum index in the prefix sum vector for range [0..i].
  4. 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.
  5. 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));

Output
5

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.


Next Article

Similar Reads