L4. Max Consecutive Ones III | 2 Pointers and Sliding Window Playlist

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ธ.ค. 2024

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

  • @ouuualo9947
    @ouuualo9947 8 หลายเดือนก่อน +76

    Solved by myself before but can't skip your video. Nice one!

    • @iam_bantu
      @iam_bantu 8 หลายเดือนก่อน +3

      Lol... Me also😅

  • @studyafa7159
    @studyafa7159 8 หลายเดือนก่อน +27

    3:43 brute
    4:45 brute code
    7:55 better
    13:45 better code
    17:00 better T(0)
    19:20 best
    24:46 - 26:48 best code

  • @krishnavamsiyerrapatruni5385
    @krishnavamsiyerrapatruni5385 8 หลายเดือนก่อน +27

    I've always had a problem with two pointer + sliding window problems. I've solved a few in Leetcode by reading the editorials. I understood them at that point of time but couldn't apply them again in the future as I just couldn't wrap my head around them. But now the intuition kicked in after watching the first few videos of your playlist and I'm able to visualise the algo while solving problems. Thank you so much!!! :)

    • @deepakjain4481
      @deepakjain4481 4 หลายเดือนก่อน +2

      i think that is the best part of striver everything start to fall in place

  • @animexworld6614
    @animexworld6614 8 หลายเดือนก่อน +94

    I may not comment on all your video. But I do watch them till last

  • @PrinceKumar-ef1xf
    @PrinceKumar-ef1xf 5 หลายเดือนก่อน +4

    00:06 Solving the problem of finding the maximum consecutive ones with at most K zeros.
    02:57 Using sliding window to find longest subarray with at most K zeros
    07:43 Using sliding window to find maximum consecutive ones with K zeros.
    10:13 Using sliding window technique to manage consecutive ones and zeros efficiently
    15:10 Use a sliding window technique to handle scenarios with more zeros than K.
    17:40 Optimizing Max Consecutive Ones III using sliding window technique
    22:01 Illustration of updating max consecutive ones with sliding window approach
    24:13 Algorithm to find max consecutive ones after K flips.
    28:58 Algorithm works with time complexity of O(n) and space complexity of O(1).

  • @dhineshkumard7639
    @dhineshkumard7639 8 หลายเดือนก่อน +12

    class Solution {
    public int longestOnes(int[] nums, int k) {
    int l=0,r=0,max=0,zero=0,n=nums.length;
    while(rk){
    if(nums[l]==0) zero--;
    l++;
    }
    if(zero

  • @AkshatMehra-l4b
    @AkshatMehra-l4b 8 หลายเดือนก่อน +7

    Good video ! Wasn't expecting the last solution, took me some time to think but definitely made my brain work.
    The main logic is that once we have found a subarray with 2 zeros of size 5, as discussed in example, and a subarray with 2 zeros of size 6 exists... then once we reach subarray of size 5, we do not shrink our sliding window. And we keep moving it ahead by moving both left and right pointers. Once we reach the subarray of size 6, our sliding window's right pointer is updated while left keeps calm, and sliding window size is updated to 6. I hope it helps.

    • @brokegod5871
      @brokegod5871 4 หลายเดือนก่อน +1

      Easier way to understand this is that the window will eventually reach a condition (with current highest length) when it just crosses K, so what we were doing is move left pointer until we find another zero effectively reducing the highest length we had gotten so far and we start to make a window freshly in hopes that it'll cross the last highest but this is pointless. The answer now will be either higher or the current highest so we do not let the window shrink beyond current highest until zero count comes within K again to eliminate the useless window refresh

  • @tanujaSangwan
    @tanujaSangwan 3 หลายเดือนก่อน +10

    Unbelievable! I solved it entirely on my own in just 5 minutes using a priority queue. Now, time to watch the video.

    • @iPunishCode
      @iPunishCode 2 หลายเดือนก่อน

      the only uncanny thing about that is a priority Queue other than that everything can be accepted

    • @tanujaSangwan
      @tanujaSangwan 2 หลายเดือนก่อน +4

      @@iPunishCode My sol with pq:
      int findZeroes(int arr[], int n, int m) {
      // code here
      priority_queue pq;
      int maxLength=1;
      int left=0;
      int right=0;
      for(int i=0; im)
      {
      left = pq.top()+1;
      pq.pop();
      }
      else {
      right=i;
      maxLength=std::max(maxLength, right-left+1);
      }
      }
      }
      return maxLength;
      }

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

      see it was not neccesary in this question thats why i said even i can do this same problem in 10 diff ways but that will not be intuitive but hey who am i to judge everyone has their way of thinking

    • @tanujaSangwan
      @tanujaSangwan 2 หลายเดือนก่อน

      @@iPunishCode I agree

    • @iPunishCode
      @iPunishCode 2 หลายเดือนก่อน +1

      @@tanujaSangwan can we exchange the number to practice together

  • @AbhijayaPal-dq3zt
    @AbhijayaPal-dq3zt 5 หลายเดือนก่อน +6

    sliding window and two pointer approach best playlist, thank you so much raj (our striver)

  • @harshchaudhari1874
    @harshchaudhari1874 5 หลายเดือนก่อน

    00:06 Solving the Max Consecutive Ones III problem using two-pointer and sliding window techniques.
    02:57 Finding longest subarray with at most K zeros.
    07:43 Implementing sliding window technique with two pointers
    10:13 Sliding window technique helps in efficiently handling zeros in the array.
    15:10 Maintain sliding window to count consecutive ones with up to K flips
    17:40 Using sliding window to find maximum consecutive ones with K flips
    22:01 Using two pointers and sliding window to find maximum consecutive ones with allowed updates.
    24:13 Using 2 pointers and sliding window to find max consecutive ones with k allowed flips
    28:58 Algorithm works by avoiding moving L extremely to the right

  • @ShahNawaz-cx3pi
    @ShahNawaz-cx3pi 6 หลายเดือนก่อน +2

    Another Approach:-
    class Solution {
    public:
    int longestOnes(vector& nums, int k) {
    int size = nums.size();
    int l = 0;
    int r = 0;
    vectorind;
    int i = 0;
    int ans = 0;
    for(int i = 0;i

  • @user-nm2wc1tt9u
    @user-nm2wc1tt9u 6 หลายเดือนก่อน +2

    class Solution {
    public:
    int longestOnes(vector& nums, int k) {
    // Brute force
    int length = 0;
    int maxLen = 0;
    for(int i=0;i

  • @baldevsundarani9615
    @baldevsundarani9615 4 หลายเดือนก่อน

    You are the best striver , solving two pointer became very easy after seeing your videos.

  • @akshatdubey4421
    @akshatdubey4421 6 หลายเดือนก่อน +2

    I could implement this myself in the first try, thanks for helping me gain confidence raj.

  • @shibainu7500
    @shibainu7500 7 หลายเดือนก่อน +2

    OMG i solved it by myself. Idk if it was an easy question but your lectures are super helpful.

  • @hashcodez757
    @hashcodez757 3 หลายเดือนก่อน +1

    "UNDERSTOOD BHAIYA!!"
    Mza aagya

  • @AnuragSingh-xe1nm
    @AnuragSingh-xe1nm 5 หลายเดือนก่อน +2

    C++ CODE BASED ON THIS LOGIC.
    class Solution {
    public:
    int longestOnes(vector& nums, int k)
    {
    int n = nums.size();
    int left = 0, right = 0, maxlen = 0, count0 = 0;
    for(right = 0; right < n; right++)
    {
    if(nums[right] == 0)
    {
    count0++;
    }

    while(count0 > k)
    {
    if(nums[left] == 0)
    {
    count0--;
    }
    left++;
    maxlen = max(maxlen, right - left + 1);
    }

    return maxlen;
    }
    };

  • @calisthenics5247
    @calisthenics5247 4 หลายเดือนก่อน +12

    How ppl build such logics like my brain stopped braining for a while when i see question.

  • @TanmayDwivedi-tu6lv
    @TanmayDwivedi-tu6lv 6 หลายเดือนก่อน

    another approach
    class Solution {
    public:
    int longestOnes(vector& nums, int k) {
    int n = nums.size(); // Get the size of the input vector
    int ans = 0; // Variable to store the maximum length of subarray with at most k zeros
    int ct = 0; // Variable to count the number of zeros encountered
    vector v1; // Vector to store the cumulative count of zeros

    // Traverse the input vector to fill the cumulative count of zeros
    for (int i = 0; i < n; i++) {
    if (nums[i] == 0) {
    ct++; // Increment the count if the current element is zero
    }
    v1.push_back(ct); // Add the cumulative count to the vector
    }
    int j = 0; // Left pointer of the sliding window
    int g = k - 1; // Right pointer of the sliding window
    if (k == 0) {
    g = 0; // Handle edge case when k is 0
    }

    // Traverse the input vector using the sliding window approach
    while (g < n && g < v1.size()) { // Ensure g does not go out of bounds
    // Calculate the number of zeros in the current window
    int temp = v1[g] - (j > 0 ? v1[j - 1] : 0);
    if (temp

  • @namannema3349
    @namannema3349 7 หลายเดือนก่อน +1

    these explaining style is good striver please make more videos like that only

  • @aloklaha8694
    @aloklaha8694 4 หลายเดือนก่อน

    Thanks for the optimal approach.

  • @expanse8846
    @expanse8846 4 หลายเดือนก่อน

    another way can be use sum to count no of ones and check if len-sum >k, reduce sum if nums[l]=1, update l and find maxlen

  • @SuvradipDasPhotographyOfficial
    @SuvradipDasPhotographyOfficial 5 หลายเดือนก่อน

    Solved it on my own after seeing the brute force. Buit I used a deque making it more simple
    class Solution {
    public:
    int longestOnes(vector& nums, int k) {
    int i = 0, j = 0, zeroes = 0;
    int ans = INT_MIN, len = 0;
    int n = nums.size();
    deque q;
    while(j < n){
    if(nums[j] == 0){
    zeroes++;
    q.push_back(j);
    }
    if(zeroes > k){
    zeroes--;
    int x = q.front();
    i = ++x;
    q.pop_front();
    }
    len = j - i + 1;
    ans = max(ans, len);
    j++;

    }
    return ans;
    }
    };

  • @SanjayChandramohan-lq3wp
    @SanjayChandramohan-lq3wp 8 หลายเดือนก่อน

    Quality teaching brother... love you

  • @SahilRaj-yp2zu
    @SahilRaj-yp2zu 5 หลายเดือนก่อน

    we can also use a queue instead of nested loop ,
    Here the time complexity is O(N), and the space complexity is O(N)
    int max =0, l=0,r=0;
    Queue index =new LinkedList();
    int zero =0;
    while(rk){
    l=index.poll() +1;
    zero--;
    }
    }
    max =Math.max(max, (r-l+1));
    r++;

    } hope you like my solution🙂

  • @Cool96267
    @Cool96267 6 หลายเดือนก่อน

    Thankyou so much Striver for all you efforts throughout in delivering us so much valuable content. Any student / working professional can now be able to transition their career without paying money for courses.
    Would also like your insights on the point :
    While preparing for interviews most of the aspirants are going through the videos solely and solving the question after completely watching the video. And also are feeling lazy trying to solve the question on our own. What is the best way to complete any topic without being lazy and how should an aspirant approach any topic/playlist?

  • @shaisthatabassum-f1o
    @shaisthatabassum-f1o 2 หลายเดือนก่อน

    class Solution {
    public:
    int longestOnes(vector& nums, int k) {
    int l=0, r=0, maxLen =0, zeros=0,len=0;
    int n=nums.size();
    while(r k){
    if(nums[l] ==0){
    zeros--;
    }
    l++;
    }
    if(zeros

  • @immrhrr
    @immrhrr 6 หลายเดือนก่อน

    int longestOnes(vector& nums, int k) {
    int i = 0, j = 0;
    int n = nums.size();
    int zero = 0;
    int maxi = 0;
    while (j < n) {
    if (nums[j] == 0) {
    zero++;
    }
    while (zero > k) {
    if (nums[i] == 0) {
    zero--;
    }
    i++;
    }
    maxi = max(maxi, j - i + 1);
    j++;
    }
    return maxi;
    }

  • @yashwanthyerra2820
    @yashwanthyerra2820 17 วันที่ผ่านมา

    i think there is no need to check for maxLen because we are not shrinking window size beyond maxlen so what we can do is
    class Solution {
    public int longestOnes(int[] nums, int k) {
    // we just need two pointers left and right
    int left = 0,right=0;
    // we had to continue till it reaches end
    for(right=0;right

  • @priyanshugagiya
    @priyanshugagiya 8 หลายเดือนก่อน +1

    16:21 if condition is not required while is doing the same thing

  • @knowthrvdo
    @knowthrvdo 5 หลายเดือนก่อน

    AWOSOME LECTURE. I SOLVED THIS QUESTION BY MYSELF !!!!

  • @adarshmv261
    @adarshmv261 7 หลายเดือนก่อน +2

    Great job... putting out this playlist. And, I don't see notes out there in TuF website?? for 2P and Sliding Windw problems

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

    in worst case lets assume all array elements are zero and k=0 it takes still 0(2n) the most optimal one

    • @devanshkumar7723
      @devanshkumar7723 4 หลายเดือนก่อน +2

      No, the optimal answer will never run for O(2n), it will always be O(n).
      According to your example, the left and right pointer both get updated for each iteration but their updation takes only O(1) i.e. constant time

    • @aryandeevoliya7421
      @aryandeevoliya7421 3 หลายเดือนก่อน

      😊❤2:​@@devanshkumar7723

  • @DurgaSivaLokeshTelaprolu
    @DurgaSivaLokeshTelaprolu 7 วันที่ผ่านมา

    small correction:
    if countZero > K, we'll need to incr left even though if nums[r] != 0

  • @Saket-op2xp
    @Saket-op2xp 6 หลายเดือนก่อน

    int main() {
    int size_arr , flips ;
    cin >> size_arr >> flips ;
    vector arr(size_arr,0) ;
    vector arr0(size_arr + 2 ,0);
    int count = 0 ;
    arr0[count] = -1 ;
    for(int i = 0 ; i < size_arr ; i++){
    cin >> arr[i] ;
    if(arr[i] == 0){count++;
    arr0[count] = i ;
    }
    }
    count++;
    arr0[count] = size_arr ;
    int l = 0 ;
    int r = l + flips + 1 ;
    int max_len = arr0[r] - arr0[l] - 1 ;
    while(r < count + 1){
    r++ ; l++ ;
    max_len= max(max_len,arr0[r]-arr0[l] - 1) ;
    }
    cout

  • @rahulshendre7089
    @rahulshendre7089 2 วันที่ผ่านมา

    hahaha got this one by myself, still watching for the knowledge

  • @ShinAkuma
    @ShinAkuma 3 หลายเดือนก่อน +1

    Buddhi khul gayi bhai. Was stuck on this problem for a long time, I had the solution, but still wasn't able to understand it even after dry running it, why did it work. Thank you.

  • @NikhilSinha-y7o
    @NikhilSinha-y7o หลายเดือนก่อน

    always a awesome content

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

    You are the besttttttttttttttttttttttttttttttttt

  • @ArjunKumar-xh2xo
    @ArjunKumar-xh2xo 4 หลายเดือนก่อน

    Thank you so much, it was very helpful

  • @unknown2698
    @unknown2698 5 หลายเดือนก่อน +2

    My solution with same logic
    class Sliding_window {
    public static void main(String[] args) {
    int[] arr = {1,1,0,1,1,1,0,1,0,1,1,1,0,1};
    int k =2;
    int l=0,r=0,zeros =0,max=0;
    while(r< arr.length){
    if(arr[r] == 0){
    zeros++;
    }
    if(zeros>k){
    if(arr[l]==0){
    zeros--;
    }
    l++;
    }
    if(zerosmax){
    max = r-l+1;
    }
    r++;
    }
    System.out.println(max);
    }
    }

  • @SHIVAMSINGHPARIHAR-w1i
    @SHIVAMSINGHPARIHAR-w1i 3 หลายเดือนก่อน

    This was asked to me in Arcesium Intern Technical Round 1

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

    I also thought the same optimal. although it took me more than 1.5 hours to tune the code.

  • @suchitakulkarni2744
    @suchitakulkarni2744 4 หลายเดือนก่อน +1

    Can we do left = right-1 instead of while loop for validating condition!!

  • @unknown2698
    @unknown2698 5 หลายเดือนก่อน

    Thankyou bhai very Good explanation

  • @ManishKumar-dk8hl
    @ManishKumar-dk8hl 8 หลายเดือนก่อน +3

    TC - O(N)
    class Solution {
    public int longestOnes(int[] arr, int k) {
    int r=0;
    int l=0;
    int maxlen=0;
    int zeroes=0;
    while(rk){
    if(arr[l]==0){
    zeroes--;
    }
    l++;
    }
    if(zeroes

  • @ddevarapaga5134
    @ddevarapaga5134 3 หลายเดือนก่อน

    broo ur explanation is greattt but can u please reduce the length of the video size cause even though i am able to solve the problems on my own i am still watching your videos to gain better understanding but it would have been great if the length of the video was smaller

  • @rajharsh3739
    @rajharsh3739 8 หลายเดือนก่อน +3

    change while to if and we trim TC from O(2n) to O(n) . VOILA 👍👍

  • @socify4410
    @socify4410 6 หลายเดือนก่อน

    SOLVED BY MYSELF BUT SKIPPING VIDEO MAY COST ME LOSE OF MOST OPTIMAL SOLUTION ❤‍🔥❤‍🔥

  • @SethuIyer95
    @SethuIyer95 8 หลายเดือนก่อน +1

    Thank you!

  • @karthik-varma-1579
    @karthik-varma-1579 2 หลายเดือนก่อน

    Java Code :
    class Solution {
    public int longestOnes(int[] nums, int k) {
    int n = nums.length;
    int maxLen = 0;
    int zeros = 0;
    int l = 0, r = 0;
    while(l

  • @karnav3366
    @karnav3366 4 หลายเดือนก่อน

    Even in optimal solution. worst case will take O(2n)

  • @PawanKumar-hq6dy
    @PawanKumar-hq6dy หลายเดือนก่อน

    class Solution {
    public int longestOnes(int[] nums, int k) {
    //[1,1,1,0,0,0,1,1,1,1,0]
    int i =0, j=0, maxLength =0;
    while(j0) {k--;}
    }
    if((j-i+1)> maxLength) {
    maxLength = j-i+1;
    }
    j++;
    }
    return maxLength;
    }
    }

  • @t-anime517
    @t-anime517 7 หลายเดือนก่อน

    dhanyawad guru ji🙏

  • @PratapSingh-yg8tc
    @PratapSingh-yg8tc 7 หลายเดือนก่อน

    very thankful to you

  • @abhaykumarsingh3884
    @abhaykumarsingh3884 4 หลายเดือนก่อน

    Last wala bahut jada intuitive hai.
    Ones you get window size for cntZeros

  • @riteshbisht94
    @riteshbisht94 7 หลายเดือนก่อน +1

    Great 🔥😃

  • @oyeshxrme
    @oyeshxrme 4 หลายเดือนก่อน

    understood bhaiya

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

    i didn't understand one thing
    in most optimum sol by the time "R" reaches end "L' has traversed N-k (k is some constant)
    so shouldn't time complexity be O(N+N-k) which is same as O(2N)🤔🤔

  • @mr_weird3680
    @mr_weird3680 8 หลายเดือนก่อน

    Thanks Brother💌

  • @mount2020
    @mount2020 8 หลายเดือนก่อน +1

    Do your previous website also exist? I have notes for questions attached there, I am not able to find it

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

    Pattern 3🔥

  • @codeman3828
    @codeman3828 8 หลายเดือนก่อน

    Understood. thanks

  • @ok-jg9jb
    @ok-jg9jb 8 หลายเดือนก่อน

    Thanks❤

  • @subee128
    @subee128 8 หลายเดือนก่อน

    Thanks

  • @Shantisingh-tz5sr
    @Shantisingh-tz5sr 8 หลายเดือนก่อน

    You are amazing....wowwwwwwwwwwwwwwwwwwwwwwwwwwwww

  • @Axpsi
    @Axpsi 8 หลายเดือนก่อน

    18:50 There is a while loop inside another while loop. Then how come Time complexity in worst case is O(n)+O(n) and not O(n^2)?

    • @RK-Sonide4vr
      @RK-Sonide4vr 8 หลายเดือนก่อน

      Because for every element it is not running for n times. Even in worst case , it runs for n times for last element only.

    • @Axpsi
      @Axpsi 8 หลายเดือนก่อน +1

      @@RK-Sonide4vr gotcha

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

      @@RK-Sonide4vr still it runs N time inside a N time running while loop so why not n^2?

    • @brokegod5871
      @brokegod5871 5 หลายเดือนก่อน +1

      @@amansaini4969 It does not run N times inside the N.
      You need to imagine what an actual n^2 loop is like - It means that for every value till N of outer loop, the inner loop is running N times ALWAYS. It's not always here. The outer loop runs for N times surely, but are you always updating the ith pointer in every value of the outer loop? Let's say there are 8 numbers, 5 1's and then 3 0's and the K = 1. That means your outer loop will move one by one to the first 0 after crossing 5 1's, but did you actually run the inner loop while crossing each and every 1? You only run that inner loop just when the condition is violated which does not happen ALWAYS as it should be in n^2.
      Take the worst case, where at the end you have 3 zeroes and K = 2. In that case, the inner loop has to cover till n-2 index which is near about N, but it did in ONE instance of outer loop, not for every instance of outer loop for it to become multiplicative. As it happened for one instance, it got added up and became 2N.

  • @osamaintezar8100
    @osamaintezar8100 3 หลายเดือนก่อน +1

    Interviewer is never happy 😞

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

      Exactly and we never get job

  • @rudreshpatel6135
    @rudreshpatel6135 6 หลายเดือนก่อน

    Hey, I have 1 question .
    What if number of 0's are less than K so we have to flip all zeros and then k-no. of zeros times we have to flip 1 as well, right? How will we able to solve that question?

  • @DheerendraSingh-u2m
    @DheerendraSingh-u2m 3 หลายเดือนก่อน

    UnderStud❤❤❤

  • @RajeevCanDev
    @RajeevCanDev 6 หลายเดือนก่อน

    In GFG the most optimized approach is giving out TLE with some 50 Testcases left out of 500, and the better one which uses two while loops is passing out all the test cases without TLE! WHY IS THAT SO?

    • @RajeevCanDev
      @RajeevCanDev 4 หลายเดือนก่อน

      @@ananyamishra382 the idea is to get the maximum window size possible or Better i should say RETAIN the maximum window size , that's why we are not shrinking L .

    • @RajeevCanDev
      @RajeevCanDev 4 หลายเดือนก่อน +1

      @@ananyamishra382 we are retaining the size only for the window which is good but small in size , that means considering that condition won't play any vital role in it , therefore we are not shrinking the size for the window which is smaller than the max size window which we have attained earlier.... simply we are just looking for the window with greater length than maxlen. Hope it makes sense

  • @deepakjain4481
    @deepakjain4481 4 หลายเดือนก่อน

    can somebody explain me what happens when there is brigde of lets say less than k zeroes between two groups of ones

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

    Understood :)

  • @shototodoroki4719
    @shototodoroki4719 8 หลายเดือนก่อน

    understood

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

    in the better solution, what if instead of incrementing the left one by one, we store the occurence index of the first 0, and directly use left = first_Occurence + 1
    ?? can someone help?

  • @mvikramaditya8264
    @mvikramaditya8264 6 หลายเดือนก่อน

    sir I can't understand how to take length like j-i+1 and some times other length
    can you give me any idea

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

    Hey can I keep a variable to keep track of the index where the last 0 occured ? When we encountered a 0 currently and instead of a while loop use the last occurence of the 0 to make the L pointer jump directly to it instead of a loop?
    Example --
    11110001110
    Now when my R pointer reaches the 3 zero I have a variable that has the index of the 2nd zero and I can use my L pointer to directly jump to that.....won't it be better optimal? Or if there is a fault can anyone pls point it out?

  • @avanishmaurya2034
    @avanishmaurya2034 5 หลายเดือนก่อน

    great

  • @SAACHIPANDEY
    @SAACHIPANDEY 6 หลายเดือนก่อน

    19:20

  • @Enigm.1.
    @Enigm.1. 3 หลายเดือนก่อน

    Please upload codes for all...

  • @studyafa7159
    @studyafa7159 8 หลายเดือนก่อน

    code have not been updated in striver link

  • @pranaycc
    @pranaycc 8 หลายเดือนก่อน

    Understood

  • @agnivadutta969
    @agnivadutta969 8 หลายเดือนก่อน +1

    How is the optimal code running on an example like:
    arr = [1,0,1,0,1,0,1,0], k=1.
    Isn't the left pointer traversing near about n times as well?

    • @priyanshugagiya
      @priyanshugagiya 8 หลายเดือนก่อน +1

      You are asking
      If we access value 3 times in one loop it will be 3n
      I hope you got why it would be n

  • @vigneshsenthil7980
    @vigneshsenthil7980 2 หลายเดือนก่อน

    C++ solution
    TC- O(N)
    SC -O(1)
    int longestOnes(vector& nums, int k) {
    int i =0 , j =0 ;
    while(j < nums.size()){
    if(nums[j] == 0 ) --k;
    if(k < 0){
    if(nums[i] == 0) ++k;
    ++i;
    }
    ++j;
    }
    return j - i;
    }

  • @MaheshPatil-of1zy
    @MaheshPatil-of1zy 5 หลายเดือนก่อน

    Optimal Approach Not Working

  • @aditya_raj7827
    @aditya_raj7827 4 หลายเดือนก่อน

    can anyone tell me what's wrong in this code ?
    int longestOnes(vector& nums, int k) {

    int n = nums.size();
    int l = 0, r= 0, maxlen = 0,zeros = 0;
    while(r < n){
    if(nums[r] == 0)zeros++;
    if(zeros k){
    if(nums[l] == 0){
    zeros--;
    }
    l++;
    }
    }
    }
    return maxlen;
    }

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 8 หลายเดือนก่อน

    public int longestOnes(int[]nums,int k){
    int l=0,r;
    for(r=0;r

  • @priyankapatil6783
    @priyankapatil6783 5 หลายเดือนก่อน

    How about this solution
    public int longestOnes(int[] nums, int k) {
    int i=0;
    int l=0;
    for(i=0;i

  • @angeldeveloper
    @angeldeveloper 8 หลายเดือนก่อน

    🙌🏻

  • @AkashKumarTiwary-u4b
    @AkashKumarTiwary-u4b 7 หลายเดือนก่อน

    god

  • @AbhishekKumar-td5zu
    @AbhishekKumar-td5zu 24 วันที่ผ่านมา

    ❣❣❣❣❣

  • @cenacr007
    @cenacr007 6 หลายเดือนก่อน

    us

  • @pritamiitismdhanbad561
    @pritamiitismdhanbad561 8 หลายเดือนก่อน

    I don't know why but whenever i am watching these problem statements of two pointer, the first idea striking on my mind is a dp state🥲...then soon understanding dp is having at least 2 states so gotta optimise it

  • @ramakrishnakcr4417
    @ramakrishnakcr4417 8 หลายเดือนก่อน

    understood

  • @adityapandey23
    @adityapandey23 4 หลายเดือนก่อน

    Understood

  • @shaiksoofi3741
    @shaiksoofi3741 5 หลายเดือนก่อน

    understood

  • @watch2-grow
    @watch2-grow 3 หลายเดือนก่อน

    Understood

  • @SuhaniSingh-j9q
    @SuhaniSingh-j9q 5 หลายเดือนก่อน

    understood

  • @sibiranganath
    @sibiranganath 3 หลายเดือนก่อน

    Understood

  • @Shivi32590
    @Shivi32590 4 หลายเดือนก่อน

    understood