largest subarray of sum k

Solutions on MaxInterview for largest subarray of sum k by the best coders in the world

showing results for - "largest subarray of sum k"
Elisa
25 Apr 2020
1def smallestSubWithSum(arr, n, x):
2 
3    # Initialize current sum and minimum length
4    curr_sum = 0
5    min_len = n + 1
6 
7    # Initialize starting and ending indexes
8    start = 0
9    end = 0
10    while (end < n):
11 
12        # Keep adding array elements while current
13        # sum is smaller than or equal to x
14        while (curr_sum <= x and end < n):
15            curr_sum += arr[end]
16            end += 1
17 
18        # If current sum becomes greater than x.
19        while (curr_sum > x and start < n):
20 
21            # Update minimum length if needed
22            if (end - start < min_len):
23                min_len = end - start
24 
25            # remove starting elements
26            curr_sum -= arr[start]
27            start += 1
28 
29    return min_len
Francesca
03 Apr 2019
1def max_length(s, k):
2    current = []
3    max_len = -1 # returns -1 if there is no subsequence that adds up to k.
4    for i in s:
5        current.append(i)
6        while sum(current) > k: # Shrink the array from the left, until the sum is <= k.
7           current = current[1:]
8        if sum(current) == k:
9            max_len = max(max_len, len(current))
10
11    return max_len
Emil
20 May 2016
1//variable size sliding window
2#include <bits/stdc++.h>
3
4using namespace std;
5
6int main()
7{
8    int n;
9    cout<<"Enter the size of the array:"<<endl;
10    cin>>n;
11    int arr[n];
12    cout<<"Enter the elements of the array:"<<endl;
13    for(int i=0;i<n;i++)
14    {
15        cin>>arr[i];
16    }
17    int k;
18    cout<<"enter the sum whose longest subarray you want to find:"<<endl;
19    cin>>k;
20    int i=0,j=0,sum=0;
21    int mx=0;
22    while(j<n)
23    {
24        sum=sum+arr[j];
25        if(sum<k)
26        {
27            j++;
28        }
29        else if(sum==k)
30        {
31            mx=max(mx,j-i+1);
32            j++;
33        }
34        else
35        {
36            while(sum>k)
37            {
38                sum=sum-arr[i];
39                i++;
40            }
41            j++;
42        }
43    }
44    cout<<mx<<endl;
45    return 0;
46}
47
queries leading to this page
find longest subarray by summin number of contiguous subarrays with sum klongest subarray with sum k gfgto find largest subarray sumhow find maximum subarray sumfind maximum length sub array having given sumlongest subarray with k sumlongest subarray with sum k gfg practiceminimum subarray size with sum 3eklongest subarray with sum 0find the longest subarray which sum is unevenmax subarray sum no larger than kmaximum sum subarray of size 3dk and sum 3cxsub array with minimum sumlargest sum subarraymax sum subarray of size k practisehow is shortest subarray sum at least k different from minimum size subarray sumlongest length subarray of sum klargeset sequence of sum 3e kminimum size subarray sum interviebithow to find longest subarray given target summinimum sum subarray of size k gfgmaximum sum of subarray of size kmax sum subarray of size k and sum 3c xfind maximum subarray sumlargest number in size of subarraylargest sum subarray pythonreturn longest subarrayposition of the longest subarray with all elements sameminimum sum subarraylongest subarray with sum at 3beast k with negative numberslongest subarray with sum k only positive integershow to find longest subarray sumfind the sum of subarrays of size kmaximum sum subarray with at least k elementsminimum length subarray with sum 3c 3dklongest sub array having sum klongest subarray sumlongest subarray sum at most kgiven an array of integers 2c find the longest subarray with gcd 3e 1sum of minimum and maximum elements of all subarrays of size kminimum size subarray sum gfghow to find longest and subarraymaximum sub array sum with size k pythonmaximum elements in subarray of size klongest subarray sum less than ksum of minimum and maximum elements of all subarrays of size k maximum size subarray sum equals kmaximum sum subarray of size k 3blongest subarray by sumfind maximum sum of a subarray of size ksmallest sum subarray sizemaximum of all subarrays of size k 2aklongest subarray with sum greater than kfind longest subarray sum code signinalmaximum sum of contiguous subarray of size kalgorithm to find maximum subarray sumlength of the longest subarray having sum equal to kmin size subarray summax sum subarray of size k gfg solutionmaximum sub array sum with size klongest subaary with given sumsmallest sum contiguous subarray sizelargest sum of subarrayfind the maximum subarray sum for a given arraymaximum subarray sum less than klargest subarray sum in arraygiven an array arr 5b 5d 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 longestsubarray with given summax sum subarray of size k practice find maximum length subarray having given sumlongest sub array problems gfgfind the largest sum subarrayfind the longest subarray that equals k find the minimum of k subarrays in an array of size nmaximum subarray sum at most ksum of subarray size having maximum int kfind longest subarray with sum 3e 3d k maximum of subarray size kbiggest sum of subarraysum of elements of subarray equals length of sub arrayfind subarray with max sum 2asizekth largest sum continuous subarraysfind subarray summinimum subarray exceeding given sumlongest and and subarray solutionlargest subarray of sum k gfgmaximum subarray of sum kfind the maximum sum of the subarray find sub array from array which has the largest sumsnum of maximum length subarray of given sum2 maximum sum subarray of size k 28easy 29minimum subarray with given sumfind minimum subarray equal to sum kcontinuous subarray sum of size kmax subarray sum of size k with indexlargest subarray with sum multiple of kmaximum size subarray sum equals kshortest subarray with sum kfind largest sum of contiguous subarraylongest subarray solutionminimum number of elements in subarray with given sumsum of k smallest elements of a subarray queiressubarray of size k maximum maximum subarray sum k stepsmaximum sum of k size subarrayfind largest sum continuous sub arrayget the longest subarray of an arraymaximum length subarray with sum less than kmaximum sum of k subarrayslongest sub array with sum klongest sum subarray only positivemaximum subarray with sum kfind subarray with maximum sumfinding the longest subarray with sum 0 in the binary array find maximum sum of subarrayhow to find maximum sum of subarray in a arrary minimum size subarray summaximum sum subarray of size e2 80 98k e2 80 99find the subarray with the minimum sum kmax sum subarray less than kfind the k subarray with the minimum sum maximum sum of subarray sum equals kfind maximum sum contiguous subarrayfind maximum sum of a subarraykth largest sum contiguous subarray gfg practicedivide the array into kk subarrays so that the maximum sum in a subarray is as small as possible max sum of subarray no larger than kminimum subarray sum larger than kminimum length subarray with sum klongest subsequence with sum klength of maximum sum subarray less than kmaximum length subarray with sum equals kmax subarray sum of size klargest subarray sum equals kfind the sum of maximum subarraygiven an array of integers and a number k 2c find the maximum sum of a subarray of size k max size subarray sum equals kpython minimum count of subarray with sum atmost k and size 2longest subarray with a given sumlongest subarray with sum 3eklongest sub array having sum k javascriptmaximum subarray sum of size klargest subarry sum in an arraykth largest subarray summinimum sum in k subarrayfind k subarray with maximum summaximum sum of subarray sum no longer klength of longest subarray with sum kmaximum sum subarray of size k 28easy 29maximum subarray sum klargest k sublist sumsubarray sum equals length of sub arrayfind minimum of all subarrays of size kminimum subarray sum equals kmax sum of subarray of size klongest sub array with sum k in o 28n 29 and o 281 29 spacefind max sum of k consecutive subarraybinary search of the largest substring having sum ksubarray of size k with given sumfind the maximum subarray sum given an array 2c find the length of the longest subarray whose sum is 0largest subarray of given sumlongest sum sub arrayminimum sum subarray of size kk th largest sum contiguous subarray gfg practicefind the maximum sum of subarray of size of ksmallest subarray of sum kk sublists with largest sumsmallest subarray with sum klargest sum of contiguous subarraylargest subarray length with given summinimum subarray whose sum is greater than ksmallest subarray with sum k from an array gfg practicelongest sum contiguous subarraylongest subarray with 0 sumq3 arbitrary shopping 3a find the length of the longest sub array such that sum of elements does not exceed e2 80 98k e2 80 99 minimum subarray of given sumfind minimum length subarray with sum greater than kmax sum of contiguous subarray of size klargest sub array sumsmallest subarray with sum atleast kmaximum length subarray with sum khow to find the largest sum subarraysubarray of size k which the max min is maximummax subarray of sum klongest subarray with a givwn sumsubarray of size k with maximum sumlongest k sum subarrayfind largest sum contiguous subarraylargest subarray with sum kminimum subarray with sum greater than kfind the length of largest subarraylargest subarray with given sumlongest subarray having sum 0k th largest sum contiguous subarraymaximum subarray k sumdivide the array into kk subarray so that the maximum sum in a subarray is as small as possible minimum of subarray of size kfind minimum sum subarray of size 60k 60find longest subarray summaximum subarray sum equals klongest sorted subarray with even sumlongest sub array with sum ksum of elements of subarray 3d length of sub arraylength of longest contiguous subarrayfind largest sum contiguous subarray gfglongest length of subarray with sum greater than kminimum subarray sumlargest subarray having sum less than kmax sum subarray of size kthe subarray with minimum sumlargest subarray having sum klargest contiguous subarray with sum kmaximum sum subarray with at least size klongest sum subarraymaximum length subarray with given sumfind the minimum element from all the subarrays of size klongest subarray with summaximum and minimum of all subarrays of size kfind the longest subarray with the same number ofmaximum sum of a subarray of size klargest sum subarray of size at least kfind the longest subarray which sum is oddlargest sum subarray indexesmaximum number in subarray of size kminimum subarray of size ksplit array largest sum into m subarrays having k minimum maximum sumlargest sum subarray with at least k numbersk th largest sum contiguous subarray practicemaximum sum of continuous subarray of size kfind the largest sum contiguous subarrayfind the subarray with maximum sumsum of subarray size with maximum int klargest sum in subarraymaximum sum of all subarrays of size khow to find maximum subarray summaximum of k size subarrayfind the longest subarray with given sumlongest subarray having sum kfind the length of the longest sub array with sum of the elements equal to the given value kfind the maximum sum of subarraymaximum subarray sum not larger than kmaximum sum k disjoint subarrays of length lsubarray of size 27k 27 with maximum sum sum of maximum length subarray of given summinimum subarray size 3eksmallest subarray with sum k from an arrayfind maximum sum subarrayminimum subarray of sum klongest subarray length with same elementslongest subarray sum atleast kgiven an array of integers what is the length of the longest subarray containing no more than twolongest subarray with sum 3dklongest subarray such that its sum is non negativelargest contiguous subarray summin subarray length to given sumlargest subarray with k sumfind suberray with greatest sumlargest subarray of sum ksum of minimum and maximum elements of all subarrays of size e2 80 9ck e2 80 9d longest subarray with consecutive elementslongest contiguous subarraylargest subarray have sum equal klongest subsequence with sum 3d kmax sum subarray of size k solutionsum minimum max subarrays of size klongest subarray sum 0smallest subarray with sum at least kfind largest sum contigous subarraylongest subarray length whose sum is not divisible by kgiven an array containing n integers and an integer k your task is to find the length of the longest sub array with sum of the elements equal to the given value k longest subarray with postive summaximum subarray of size kfind largest sum contigeous subarraylongest subarray with sum divisble bykmaximu size subarray with sum klargest subarray sumfind minimum of subarray with sum k practicelargest subarray of sum k hackerrank solutionsubarray sum at least k longest subarray with sum 0largest sum contiguous subarray 28kadane 27 algorithm 29longest subarray with sum 1longest and subarray solutionlongest increasing subarray sumgiven an array of integers what is the length of the longest subarrayfind longest arithmetic subarrayminimum size subrrray sumlongest sub array having sum k jsfind minimum length sub array with sum kfind maximum sum in array of contiguous subarraysminimum sub matrix with sum klongest subsequence with sum equals to kwrite a program that find largest sum of subarrayfind maximum possible sum subarrayminimum size of sub matrix in a matrix with sum klargest sum subarray of size k containing consecutive elements publishlongest subarray with 0 sum leetcodesubarray of size k maximum summaximum subarray sum of k sizeminimum sum of subarrayfind the largest subarray sum with sum atmost klongest length subarray sortedlargest sum contiguous subarraylongest subarray with sum kmaximum sum subarray of size kminimum and maximum of all subarrays of size kmaximum sum subarray with length kmaximum of subarrays of size kminimum sum in k subarrayslargest subarray of sum k practicelargest sum subarray of size k containing consecutive elementsfind maximum sum of a subarray of size k pythonmaximum sum subarray of klongest subarray of sum k gfg practicethe subarray with minimum sum pythonhow to find maximum sum of subarraymaximum sum if k size subarrayminimum length subarray with sum at least klongest subarray of sum kfind largest subarray with given sumbinary search of the longest substring having sum klargest subbarray with given sumsubarray sum equals at least kongest subarray with a sum constraintfind minimum and maximum of subarray with sum k practicelongest sorted subarray with sum k maximum of all subarrays of size k practicesubarray of size k with max sumfinding longest subarray with positive summinimum of all subarrays of size klongest subarray with given sumlargest subarray of sum k