Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Questions on Array
  • Practice Array
  • MCQs on Array
  • Tutorial on Array
  • Types of Arrays
  • Array Operations
  • Subarrays, Subsequences, Subsets
  • Reverse Array
  • Static Vs Arrays
  • Array Vs Linked List
  • Array | Range Queries
  • Advantages & Disadvantages
Open In App
Next Article:
Longest subarray having sum K | Set 2
Next article icon

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}.

Table of Content

  • [Naive Approach] – Generating All Subarrays – O(n ^ 2) Time and O(1) Space
  • [Expected Approach] – Using Prefix Sum and Binary Search – O(n * log(n)) Time and O(n) Space

[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
Longest subarray having sum K | Set 2
author
nik1996
Improve
Article Tags :
  • Arrays
  • DSA
  • Searching
  • Binary Search
  • prefix-sum
  • subarray
  • subarray-sum
Practice Tags :
  • Arrays
  • Binary Search
  • prefix-sum
  • Searching

Similar Reads

  • Number of subarrays having absolute sum greater than K | Set-2
    Given an integer array arr[] of length N consisting of both positive and negative integers, the task is to find the number of sub-arrays with the absolute value of sum greater than a given positive number K. Examples: Input : arr[] = {-1, 0, 1}, K = 0 Output : 4 All possible sub-arrays and there tot
    6 min read
  • 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
    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]
    13 min read
  • Longest subarray having sum K | Set 2
    Given an array arr[] of size N containing integers. The task is to find the length of the longest sub-array having sum equal to the given value K. Examples: Input: arr[] = {2, 3, 4, 2, 1, 1}, K = 10 Output: 4 Explanation: The subarray {3, 4, 2, 1} gives summation as 10. Input: arr[] = {6, 8, 14, 9,
    14 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
  • Largest Sum Contiguous Subarray having unique elements
    Given an array arr[] of N positive integers, the task is to find the subarray having maximum sum among all subarrays having unique elements and print its sum. Input arr[] = {1, 2, 3, 3, 4, 5, 2, 1}Output: 15Explanation:The subarray having maximum sum with distinct element is {3, 4, 5, 2, 1}.Therefor
    15+ min read
  • Longest subarray having average greater than or equal to x
    Given an array of integers and an integer x. Find the length of maximum size subarray having an average of integers greater than or equal to x. Examples: Input : arr[] = {-2, 1, 6, -3}, x = 3Output : 2Longest subarray is {1, 6} having average3.5 greater than x = 3.Input : arr[] = {2, -3, 3, 2, 1}, x
    14 min read
  • Count of subarrays with sum at least K
    Given an array arr[] of size N and an integer K > 0. The task is to find the number of subarrays with sum at least K.Examples: Input: arr[] = {6, 1, 2, 7}, K = 10 Output: 2 {6, 1, 2, 7} and {1, 2, 7} are the only valid subarrays.Input: arr[] = {3, 3, 3}, K = 5 Output: 3 Approach: For a fixed left
    6 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 su
    11 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences