Smallest subarray with sum greater than a given value | GeeksforGeeks

Поделиться
HTML-код
  • Опубликовано: 29 окт 2024

Комментарии • 50

  • @justinmiller129
    @justinmiller129 7 лет назад +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 3 года назад

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

    • @ritwikgopi
      @ritwikgopi 3 года назад

      @@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 3 года назад +1

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

    • @mihirmodi1936
      @mihirmodi1936 2 года назад

      @@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 2 года назад

      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

  • @harishv7113
    @harishv7113 Год назад +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

  • @agao4470
    @agao4470 3 года назад +10

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

    • @reygamingyt8007
      @reygamingyt8007 2 года назад

      Dont follow gfg

    • @reygamingyt8007
      @reygamingyt8007 2 года назад +1

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

  • @ipranavprashant
    @ipranavprashant 11 месяцев назад

    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

  • @SHASHANKRUSTAGII
    @SHASHANKRUSTAGII 3 года назад +2

    can this be solved using DP?

  • @viczsaurav
    @viczsaurav 6 лет назад

    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

  • @gokhandroid
    @gokhandroid 6 лет назад +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

  • @AbhishekSharma_1504
    @AbhishekSharma_1504 3 года назад +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 3 года назад

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

    • @siddharthsharma6175
      @siddharthsharma6175 Год назад

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

  • @harsharaikar2156
    @harsharaikar2156 7 лет назад +6

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

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +13

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

  • @archertam
    @archertam 6 лет назад +1

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

    • @tutysara
      @tutysara 6 лет назад +1

      yes, the invariant should be start

  • @sreerup_dhrino
    @sreerup_dhrino 5 лет назад +1

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

  • @komalgujarathi8900
    @komalgujarathi8900 7 лет назад +5

    it is just for positive numbers in array i guess

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 лет назад +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 6 лет назад

      @@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 5 лет назад

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

  • @caohoaitam7269
    @caohoaitam7269 3 года назад

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

  • @devalpatel7688
    @devalpatel7688 4 года назад

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

  • @timcook3410
    @timcook3410 7 лет назад

    is the 2nd algorithm O (n) ?

    • @revunurusatish5338
      @revunurusatish5338 7 лет назад

      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 7 лет назад

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

    • @revunurusatish5338
      @revunurusatish5338 7 лет назад

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

  • @sankitfodu2007
    @sankitfodu2007 6 лет назад +2

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

    • @2fast4uspartan
      @2fast4uspartan 6 лет назад

      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 4 года назад

    Solution in pythpn

  • @anandpurushottam4435
    @anandpurushottam4435 3 года назад

    wrong solution does not work

  • @jaylal6802
    @jaylal6802 7 лет назад

    That Optimization was certainly a Ninja Trick!

  • @LiudongZuo
    @LiudongZuo 7 лет назад +1

    Thanks, man, this video is very useful!

  • @reygamingyt8007
    @reygamingyt8007 2 года назад

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

  • @shaikmohammedrafi3309
    @shaikmohammedrafi3309 4 года назад

    very helpful

  • @sunilsigar3145
    @sunilsigar3145 5 лет назад

    perfect

  • @sunanqi
    @sunanqi 5 лет назад

    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 4 года назад

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

  • @GuyKershtein
    @GuyKershtein 3 года назад

    I love you