Smallest subarray with sum greater than a given value | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 18. 03. 2017
  • Explanation for the article: www.geeksforgeeks.org/minimum-...
    This video is contributed by Harshit Jain.

Komentáře • 50

  • @justinmiller129
    @justinmiller129 Před 6 lety +32

    The array contains non-negative numbers, only.
    with negative numbers the presented solution won't work.
    e.g. find the sub-array with sum greater than 50 in {-100, -200, -300, -400, -500, 50, 1, 2}

    • @suvambasak9313
      @suvambasak9313 Před 3 lety

      wont work for this also : arr = [1, 2, 3,4,1,2,10] value = 5

    • @ritwikgopi
      @ritwikgopi Před 3 lety

      @@suvambasak9313 What was the issue you found with this input? I can see it won't work if the negative numbers are there. But what is the issue with the input you mentioned?

    • @hitendrarawat9590
      @hitendrarawat9590 Před 3 lety +1

      @@suvambasak9313 your implementation is wrong, it should work with this input.

    • @mihirmodi1936
      @mihirmodi1936 Před 2 lety

      @@suvambasak9313
      for positive values :
      int smallestSubWithSum(vector nums,int X){
      int sum=0;
      int start=0;
      int end=0;
      int mini=nums.size();
      while(end

    • @mihirmodi1936
      @mihirmodi1936 Před 2 lety

      Try this for negative values :
      int smallestSubWithSum(vector nums,int X){
      int sum=0;
      int start=0;
      int end=0;
      int mini=nums.size();
      while(end

  • @agao4470
    @agao4470 Před 3 lety +10

    Fun fact: this code is not getting accepted by gfg,shows wrong answer for a particular case

  • @harishv7113
    @harishv7113 Před rokem +1

    there is a slight code issue in second approach. Try this code instead
    var minSubArrayLen = function(target, nums) {
    let start=0,end=0, sum=0, minLength=Number.MAX_VALUE;
    while(end

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII Před 3 lety +2

    can this be solved using DP?

  • @viczsaurav
    @viczsaurav Před 6 lety

    In first program O(N^2): Add following line after 'if' condition in inner loop. This breaks and return loop if even after adding all elements, the sum is not greater than x:
    if ((end-start+1) == n && curr_sum < x) return 0; // No SubArray possible

  • @ipranavprashant
    @ipranavprashant Před 7 měsíci

    the external for loop isnt making any sense if you have that and condition in the next while itself, moreover it isnt explicitly changing any variable on its own

  • @harsharaikar2156
    @harsharaikar2156 Před 7 lety +6

    in the first example why isn't {45,19} the smallest subarray

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 7 lety +13

      Hi Harsha,
      {45,19} is not a subarray but a subsequence. {4, 45, 6} is a subarray as the elements are contiguous.

  • @sreerup_dhrino
    @sreerup_dhrino Před 4 lety +1

    Please improve the volume of the video. I can hardly hear anything without using headphone

  • @AbhishekSharma_1504
    @AbhishekSharma_1504 Před 3 lety +1

    Why are we initailizing min_length as n+1. Even if we initialize it as n, it doesn't make any change to the output, can you please explain.

    • @adhvaithanayak1606
      @adhvaithanayak1606 Před 3 lety

      hey! can u explain y?
      I mean min_length as n+1, but y?

    • @siddharthsharma6175
      @siddharthsharma6175 Před rokem

      @@adhvaithanayak1606 you can take min_len=n because all the sum of array is always greater than the given number

  • @gokhandroid
    @gokhandroid Před 6 lety +1

    in your first simple solution, you will never return min_len! you will always return 1! check the second loop, once you have curr_sum > x , it will go to first loop and it will return 1 since your "return min_len" is out of both loop

  • @caohoaitam7269
    @caohoaitam7269 Před 3 lety

    How to return the subset, not the length of subset?

  • @komalgujarathi8900
    @komalgujarathi8900 Před 6 lety +5

    it is just for positive numbers in array i guess

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 6 lety +1

      To handle negative numbers, add a condition to ignore subarrays with negative sums.
      You can find the complete code on the article: www.geeksforgeeks.org/minimum-length-subarray-sum-greater-given-value/

    • @freddierice
      @freddierice Před 5 lety

      @@GeeksforGeeksVideos That implementation does not work either. Consider the array {10, -5, -4, 50, 51, -50, 20, 20} and x=100. The condition you add is never triggered and {50, 51} is never tested.

    • @azadalishah2966
      @azadalishah2966 Před 5 lety

      @@GeeksforGeeksVideos Input
      [84,-37,32,40,95]
      , X=167
      Output:
      5
      Expected
      : 3
      It doesn't work with negative condition provided.

  • @archertam
    @archertam Před 5 lety +1

    Why inner loop checks start < n? Wouldn't it be more logical while start < end (as it can not be higher)?

    • @tutysara
      @tutysara Před 5 lety +1

      yes, the invariant should be start

  • @LiudongZuo
    @LiudongZuo Před 7 lety +1

    Thanks, man, this video is very useful!

  • @devalpatel7688
    @devalpatel7688 Před 3 lety

    this is not working for {-28,81,-20,28,-29} k = 89

  • @shaikmohammedrafi3309
    @shaikmohammedrafi3309 Před 3 lety

    very helpful

  • @timcook3410
    @timcook3410 Před 7 lety

    is the 2nd algorithm O (n) ?

    • @revunurusatish5338
      @revunurusatish5338 Před 7 lety

      Yes, it's O(2n) ie O(n). Start and end pointer will traverse entire array in worst case. But those are not nested loops, those are independent loops. So n+n.

    • @timcook3410
      @timcook3410 Před 7 lety

      revunuru satish how is O (2n)=O (n)?

    • @revunurusatish5338
      @revunurusatish5338 Před 7 lety

      Big O notation. Limit n->infinity 2n=n. Eg. n^2+4n+1~=n^2 for larger values of n. so O(n^2+4n+1) = O(n^2).

  • @jaylal6802
    @jaylal6802 Před 6 lety

    That Optimization was certainly a Ninja Trick!

  • @sunilsigar3145
    @sunilsigar3145 Před 5 lety

    perfect

  • @sankitfodu2007
    @sankitfodu2007 Před 6 lety +2

    Why is the array length end-start instead of end-start+1?

    • @2fast4uspartan
      @2fast4uspartan Před 5 lety

      Because they haven't updated start yet when the min length is calculated. If start was incremented, then you'd need to add 1. Also, end is one index to the right of the actual end item that was added to sum, see the [end++] part. So no need to add + 1.

  • @user9851-q9n
    @user9851-q9n Před 3 lety

    Solution in pythpn

  • @GuyKershtein
    @GuyKershtein Před 3 lety

    I love you

  • @anandpurushottam4435
    @anandpurushottam4435 Před 3 lety

    wrong solution does not work

  • @reygamingyt8007
    @reygamingyt8007 Před rokem

    Try this condition along with this code if (n==1 && x==1 )return 0; thank me later

  • @sunanqi
    @sunanqi Před 5 lety

    Thumbs down. Sorry. It's wrong. The author fails to consider the case where the array has negative numbers. And I don't think it can be solved in O(n). The answer is misleading.

    • @kanishkanand1555
      @kanishkanand1555 Před 3 lety

      Can't we use binary search on answer concept to guess the size of subarray and then ...