Minimum Subarray Size | Sliding Window Challenge | C++ Placement Course | Lecture 30.2

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • Complete C++ Placement Course (Data Structures+Algorithm) : • C++ Full Course | C++...
    Telegram: t.me/apnikaksh...
    Instagram: / dhattarwalaman

ความคิดเห็น • 67

  • @harshverdhanswami5850
    @harshverdhanswami5850 3 ปีที่แล้ว +8

    please recheck its not o(n) ,time limit exceeded.

    • @rehanhussan
      @rehanhussan 10 หลายเดือนก่อน +2

      Exactly

  • @meme_eternity
    @meme_eternity ปีที่แล้ว +3

    int shortestSubarray(vector& nums, int k)
    {
    int ans = INT_MAX,n = nums.size(),start = 0,sum = 0;
    for(int i = 0;i < n;i++)
    {
    sum += nums[i];
    if(sum >= k)
    {
    ans = min(ans,i - start + 1);
    }
    while(sum > k && start < i)
    {
    sum -= nums[start++];
    if(sum >= k)
    {
    ans = min(ans,i - start + 1);
    }
    }
    }
    if(ans == INT_MAX)
    {
    return -1;
    }
    return ans;
    }

  • @pratham654
    @pratham654 3 ปีที่แล้ว +6

    This code is wrong as in the inner 1st while loop the condition should be sum>=x (go to 11:51) and in the given example the ans should be 2 (45+6) not 3 (go to 17:30).

    • @hyphaamrinmulla5366
      @hyphaamrinmulla5366 3 ปีที่แล้ว

      true

    • @ayushikathait1312
      @ayushikathait1312 2 ปีที่แล้ว +2

      no, actually we want the subarray sum to be "greater" than the given value, not "greater than or equal to", so ans will be 3

    • @pratham654
      @pratham654 2 ปีที่แล้ว

      @@ayushikathait1312 ya u r right but for the coding platform like gfg..if you write the condition

    • @payalbhattamisra6985
      @payalbhattamisra6985 2 ปีที่แล้ว

      Code is right. x is greater than equal to sum && n is greater than end.then sum +=arr[end++].output is 3 (45+6+4=55) because sum>x and it is also smallest subarray .but code is not work for negative elements.

  • @SurajSingh-mx5zs
    @SurajSingh-mx5zs 2 ปีที่แล้ว +2

    but it is not working when we take nagative. element in array

  • @shreyamkumar7210
    @shreyamkumar7210 3 ปีที่แล้ว +5

    How long this course will be please comment if anyone can predict

    • @anshumansingh6316
      @anshumansingh6316 3 ปีที่แล้ว

      I guess course me basic topic ho chuke h
      Sirf ye que k series hi chl rha h

  • @vinayaksangar1928
    @vinayaksangar1928 3 ปีที่แล้ว +2

    Will not work if negative elements are present in the array

    • @willturner3440
      @willturner3440 2 ปีที่แล้ว +2

      Then problem turns into binary search problem

  • @NoName-vg8vf
    @NoName-vg8vf 4 หลายเดือนก่อน

    #include
    using namespace std;
    int minSubarraySum(int arr[], int n, int x)
    {
    int k=0, sum=0, i=0, j=0, ans=INT_MIN;
    while(ix)
    {
    sum-=arr[j];
    j++;
    }
    }
    if(j==0){
    return ans+1;//no elements exist in the array that exceeds the accumulated value
    }
    return ans;
    }
    int main()
    {
    int arr[]={1, 4, 45, 6, 10, 19};
    cout

  • @harshtank7075
    @harshtank7075 3 ปีที่แล้ว +4

    Didn't we have to add 1 to the difference of end-start ??

    • @aastikchaudhary3714
      @aastikchaudhary3714 3 ปีที่แล้ว +6

      No as the end already exceeds the original end by 1 when we do end++

    • @chirag-sg4kd
      @chirag-sg4kd 3 ปีที่แล้ว +1

      @@aastikchaudhary3714 I was having the same doubt thank you

    • @vishaldas5692
      @vishaldas5692 3 ปีที่แล้ว +1

      @@aastikchaudhary3714 i had missed this ....thanks for telling🙂

  • @shaikhparwez2969
    @shaikhparwez2969 3 ปีที่แล้ว

    Sir ap bout accha explain Karte Ho
    Sir java / javascript ya python ispar bhi banao sir plzzz

  • @praveentakpuriya5431
    @praveentakpuriya5431 3 ปีที่แล้ว +3

    when I am trying to run same code on same question for LEETCODE it's not working.

    • @gauravupreti9340
      @gauravupreti9340 3 ปีที่แล้ว +1

      It doen't work for negative no

    • @rishavraj9284
      @rishavraj9284 7 หลายเดือนก่อน

      It's not working because In this example she is taking only positive value but In leet code problem negative value is also present

  • @techbrothers999
    @techbrothers999 3 ปีที่แล้ว +3

    From where to get notes of this lecture???

  • @akshatvyas2594
    @akshatvyas2594 3 ปีที่แล้ว +4

    Is it ok,
    int MinSubArrSize(int arr[], int n , int x){
    int ans = INT_MAX;
    int sum = 0;
    int index = 0;
    for(int i = 0 ; i< n ; i++){
    sum += arr[i];
    if(sum > x and index < i){
    ans = min(ans, i - index);
    sum -= arr[index];
    index++;
    }
    }
    return ans;
    }

  • @ayushmishravlogs
    @ayushmishravlogs 3 ปีที่แล้ว +8

    Waiting for Java course 🙂🙂

    • @krunaldevganiya955
      @krunaldevganiya955 3 ปีที่แล้ว

      💯

    • @al-hadees6236
      @al-hadees6236 3 ปีที่แล้ว +7

      when u didn't learn one language properly but you eager to learn more languages, that's why most people are unsuccessful to be a expert in coding

    • @shaikhparwez2969
      @shaikhparwez2969 3 ปีที่แล้ว

      Haa bhai

    • @ahmedaghadi8281
      @ahmedaghadi8281 3 ปีที่แล้ว +1

      @@al-hadees6236 maybe he is talking about java course left half way in apni kaksha channel.

    • @justfun4976
      @justfun4976 ปีที่แล้ว

      choose only one coding language like Java or C++..if you learn both then may be you will stuck or contradict both language's syntax 😅

  • @thethe4051
    @thethe4051 3 ปีที่แล้ว +3

    Hi bro/sir, i m not in 12th but can you please tell me that, one day one subject or one day multiple subject 🙏🙏🙏🙏 please tell me i m so much confused.

    • @al-hadees6236
      @al-hadees6236 3 ปีที่แล้ว

      try one day multiple subject, but if you like to study one subject then go on with only one subject, and when you bored at one subject then move on with other subjects

  • @parvjain09
    @parvjain09 3 ปีที่แล้ว +1

    Diii kya C language bhi shekna padhega.....agr java ya c++ me se koi ek shek le to kaffi h ....ya phir sabse pehle C learn kare.....?
    Please koi reply de dena

    • @arjavkanadia9730
      @arjavkanadia9730 3 ปีที่แล้ว

      depends job ke liye toh koi bhi lang chalega lekin operating systems ya robotics mei jana hai toh C sikhna padega

    • @diveshkumar3823
      @diveshkumar3823 3 ปีที่แล้ว +1

      Bro no matter ki which language you choose, but if you do c first you easily understandable and clear the basics for java or c++. So I recommend you to do c language first then go for cpp or java

  • @PVarshney
    @PVarshney 3 ปีที่แล้ว +6

    Are dp, graph and trees the important to get into a software company

  • @hustlewithVaibhav
    @hustlewithVaibhav 3 ปีที่แล้ว

    Thank you 🙏🙏

  • @sagarkumarjha3849
    @sagarkumarjha3849 3 ปีที่แล้ว +2

    wrong solution if array contains negative values.

    • @magnitegame
      @magnitegame 3 ปีที่แล้ว

      check constraints

  • @pranavdoshi1544
    @pranavdoshi1544 3 ปีที่แล้ว +7

    can this code work if array elements include negative elements?

  • @apnamanoranjan8228
    @apnamanoranjan8228 3 ปีที่แล้ว

    Bhaiya java course basic se start kariye

  • @ujwalkachade7066
    @ujwalkachade7066 3 ปีที่แล้ว +1

    here's my code sliding window approch but little bit different implementation .
    i guess easy to understand.
    #include
    #include
    using namespace std;
    int smallestsubarray(int arr[], int n, int x)
    {
    if (n == 0)
    return -1;
    int lo = 0; //start
    int currentLength = 0;
    int ans = INT_MAX;
    int sum = 0;
    for (int i = 0; i < n; i++)
    {
    sum += arr[i];
    currentLength++;
    if (sum > x)
    {
    while (sum > x)
    {
    sum -= arr[lo];
    lo++;
    ans = min(currentLength, ans); // compare just before sum becomes less than x;
    currentLength--;
    }
    }
    }
    return ans;
    }
    int main()
    {
    int arr[] = {1, -5, 45, 55, 10, 8};
    int n = sizeof(arr) / sizeof(arr[0]);
    int x = 51;
    int ans = smallestsubarray(arr, n, x);
    if (ans < n + 1)
    cout

    • @sourabhchoudhary7289
      @sourabhchoudhary7289 3 ปีที่แล้ว

      hmm fairly simple
      but you can return (ans==INT_MAX)? -1:ans)
      we will check if no subarray is avaliable with sum >x

    • @tarunbisht8016
      @tarunbisht8016 3 ปีที่แล้ว +1

      u can again minimize the code like there is no use of that if condition and no need to find the length also just use two pointer tech
      int minSubArrayLen(int target, vector& nums)
      {
      int min_size=INT_MAX,sum=0;
      int left=0;

      for(int right=0;right=target)
      {
      min_size=min(min_size,right-left+1);
      sum-=nums[left++];
      }
      }

      return (min_size!=INT_MAX)?min_size:0;
      }

    • @tarunbisht8016
      @tarunbisht8016 3 ปีที่แล้ว +1

      one advice whenever u guys use sliding window technique try to use two pointer technique.

  • @salmaniproductions1104
    @salmaniproductions1104 ปีที่แล้ว

    Dont anyone think that its Time complexity increases to O(n*n) coz there is a nested loop tooo...??

  • @siddhantchavan1370
    @siddhantchavan1370 2 ปีที่แล้ว

    wrong solution he, if sum== target value toh voh length ko update nahi karta

  • @Sachin_.10
    @Sachin_.10 ปีที่แล้ว

    Aap shraddha didi hai kya

  • @BeingEntertained
    @BeingEntertained 3 ปีที่แล้ว

    test case pass nhi hore is se leetcode pe

  • @sumanmandal4485
    @sumanmandal4485 3 ปีที่แล้ว

    True love

  • @vaibhavsudan7384
    @vaibhavsudan7384 2 ปีที่แล้ว

    Noice

  • @ishmartishtiaq371
    @ishmartishtiaq371 3 ปีที่แล้ว +10

    Dislike kon karta hy yaar😠

  • @sourav_Barbigahiya
    @sourav_Barbigahiya 2 ปีที่แล้ว

    Thanks ❤️

  • @hustlewithVaibhav
    @hustlewithVaibhav 3 ปีที่แล้ว +1

    Thank you 👍👍