L9. Binary Subarrays With Sum | 2 Pointers and Sliding Window Playlist

Notes/Codes/Problem links under step 10 of A2Z DSA Course: takeuforward.org/strivers-a2z...
Entire playlist: • Two Pointer and Slidin...
Follow us on our other social media handles: linktr.ee/takeuforward

Пікірлер: 72

  • @SuvradipDasPhotographyOfficial
    @SuvradipDasPhotographyOfficialАй бұрын

    This is an Excellent Question, truly amazing tutorial striver. Kudos to you bro. Striver only heap and stack queues are left, hoping to get those series soon. Take care of your health bro. The entire DSA community will forever be indebted to you.

  • @user-ww3cg2cc7d
    @user-ww3cg2cc7d3 ай бұрын

    It becomes hard problem when we try to solve using sliding window but how easily Striver was able to explain it is just Awesome and much appreciated❤❤❤

  • @amansingh-hb9ko
    @amansingh-hb9ko3 ай бұрын

    just a small correction in first while condition r should less only size not equal to while(r

  • @Funzee

    @Funzee

    Ай бұрын

    do u know where is the article of this code

  • @moonlight-td8ed

    @moonlight-td8ed

    Ай бұрын

    @@Funzee there isnt one i guess

  • @LogicArena01
    @LogicArena0129 күн бұрын

    Amazing ❤ love your teaching style and love how you teach us how to think from the scratch ❤

  • @RohitKumar-hn6wj
    @RohitKumar-hn6wj3 ай бұрын

    best series on sliding windows. thanks a lot.

  • @mayurbhat9479
    @mayurbhat9479Ай бұрын

    Understood. While solving this with the sliding window, I got the wrong answer and wondered why. Thanks for the explanation.

  • @Demon01Editz
    @Demon01EditzАй бұрын

    Code : class Solution { public: int lessequaltok(vector& nums,int goal){ if(goal return 0; int l = 0; int r = 0; int ans = 0; int n = nums.size(); int sum = 0; while(r sum += nums[r]; while(sum > goal){ sum -= nums[l]; l++; } ans += (r-l+1); r++; } return ans; } int numSubarraysWithSum(vector& nums, int goal) { return lessequaltok(nums,goal)-lessequaltok(nums,goal-1); } };

  • @augustinradjou3909
    @augustinradjou39093 ай бұрын

    Long time I was waiting to get these types of problems...good one...

  • @atg878
    @atg878Ай бұрын

    thanks a lot ❤❤

  • @hareshnayak7302
    @hareshnayak73022 ай бұрын

    Understood,Thanks striver for this amazing video.

  • @huungryyyy
    @huungryyyy29 күн бұрын

    thanku striver bhaiya🙂🙂

  • @Zomb-zj4ip
    @Zomb-zj4ip29 күн бұрын

    understood, thank you

  • @rethickpavan4264
    @rethickpavan426417 күн бұрын

    Brilliant 🤯

  • @stith_pragya
    @stith_pragya2 ай бұрын

    UNDERSTOOD...Thank You So Much for this wonderful video........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @pranavmisra5870
    @pranavmisra5870Ай бұрын

    amazing

  • @subhadrosamaddar6336
    @subhadrosamaddar6336Ай бұрын

    incredible

  • @ashishpradhan6250
    @ashishpradhan625012 күн бұрын

    superb

  • @user-gb5id1nt8v
    @user-gb5id1nt8v2 ай бұрын

    I never thought of a solution this way. Awesome

  • @shubhtandon8315
    @shubhtandon831512 күн бұрын

    Just Wowww!

  • @abirsaha297
    @abirsaha29728 күн бұрын

    the solution is amazing ...opened up my mind..superb explanation but i wonder striver did it so easily when will i be able to build such intuition ... i got great confidence throughout the playlist but the way sir brought up the solution amazed me but also gave lot of questions as to when will i be able to think like this

  • @jritzeku

    @jritzeku

    21 күн бұрын

    Honestly for subarray sum problems having negative values or 0s , prefixSum is the way to go. It's slightly less performant but more intuitive. I tried learning the optmized approach several times but its too clever and unlikely to come up with during interview given that we have so many other data structures, algs patterns to worry about already lol. Problems w/ prefixSum: subarray sum equals k, binary sum equals k , nice subarrays(SAME logic as binary sum w/ slight change).

  • @angeldeveloper
    @angeldeveloper3 ай бұрын

    Thanks a ton ❤

  • @techyguyaditya
    @techyguyaditya3 ай бұрын

    At 16:15 I am getting problem here. Why are we considering all the count of r-l+1 when we only get 1 count instead of 4 in the case of 1001? The same problem got reflected in leetcode too, getting wrong answer in test cases. Solution: watch lecture 11 to understand in detail. The code is correct if goal is

  • @codeman3828
    @codeman38283 ай бұрын

    Understood. Thanks!

  • @shashankvashishtha4454
    @shashankvashishtha44546 күн бұрын

    understood

  • @torishi82
    @torishi82Ай бұрын

    Awesome bhai. Understood.

  • @heetpatel6602
    @heetpatel66023 ай бұрын

    striver thanks!

  • @shrutiagarwal-ux1qz
    @shrutiagarwal-ux1qz2 ай бұрын

    how did you get the intution of using this approach

  • @anujvijaypatilb22ee010
    @anujvijaypatilb22ee010Ай бұрын

    class Solution { public: int count(vector& nums, int goal) { int left = 0; int right = 0; int sum = 0; int count = 0; // if() while (right sum += nums[right]; while (sum > goal && left

  • @evilgame6197
    @evilgame6197Ай бұрын

    in my entire lifetime I will never be able to come to this solution by my own

  • @jritzeku

    @jritzeku

    24 күн бұрын

    And thats completely fine. Consider them as like Mathematical formulas. Recall that without having learned formulas, it would be nearly impossible to solve even simple math problems. Unfortunately these patterns are not covered in traditional CS curriculums where we just learn data structures, and common algorithms like traversals ,insertions ,deletions. Also another common pattern/strategy is to use the prefixSum solution. You shouldn't feel like you have to come up with these patterns on your own...just like math formulas lol.

  • @devanshsingh2

    @devanshsingh2

    9 күн бұрын

    @@jritzeku Thanks for the motivation dude 🥲😊

  • @--Sreekarsai
    @--Sreekarsai3 ай бұрын

    Why there is no code

  • @yaxprajapati115
    @yaxprajapati115Ай бұрын

    How will we identify if the question is of this type ? or normal sliding window?

  • @KartikeyTT

    @KartikeyTT

    28 күн бұрын

    this type -> we have to count the no of subarrays normal sliding windows -> we have to find longest length subarray watch video 1 of the playlist and refer type 2 and type 3 problems in the vid

  • @SaurabhKumar-tq8zn
    @SaurabhKumar-tq8znАй бұрын

    why you added r-l+1 , why not +1

  • @purushottam108
    @purushottam10825 күн бұрын

    class Solution { public: int fun(vector& arr, int goal){ if(goal int l = 0, r = 0 , cnt = 0; int sum = 0; while(r sum += arr[r]; while(sum > goal){ sum -= arr[l]; l++; } cnt += r - l + 1; r++; } return cnt; } int numSubarraysWithSum(vector& nums, int goal) { return fun(nums,goal) - fun(nums,goal - 1); } };

  • @satyareddy8671
    @satyareddy8671Ай бұрын

    super anna super logic what is vision what a thought

  • @huungryyyy
    @huungryyyy29 күн бұрын

    Here is the solution class Solution { public: int Calculate(vector& nums, int target) { int front=0; int end=0; int count=0; int sum =0; if(target

  • @ashimaanand992
    @ashimaanand9923 ай бұрын

    Shouldn't the complexity be O(n^2) and not O(2*n)

  • @HimanshuGupta-yv4lf
    @HimanshuGupta-yv4lfАй бұрын

    Thanks for the tutorial but, this gives result for

  • @shashanknakashe3339
    @shashanknakashe3339Ай бұрын

    this is the solution that raj bhaiya told us do at last goal-goal-1; class Solution { public: int numSubarraysWithSum(vector& nums, int goal) { int l=0; int r= 0; int count =0; int sum = 0 ; while(r sum = sum+ nums[r]; while(sum>goal){ sum = sum -nums[l]; l++; } if(sum

  • @ManishKumar-dk8hl
    @ManishKumar-dk8hl3 ай бұрын

    class Solution { public int helpMe(int[] nums,int goal){ int l=0; int r=0; int sum=0; int cnt=0; if(goal

  • @chakrabarti9634

    @chakrabarti9634

    3 ай бұрын

    Why goal - goal-1 please help😊

  • @Rahul_Mongia

    @Rahul_Mongia

    Ай бұрын

    @@chakrabarti9634 Dekh bhai example se samjthe hai let goal=2 Calculate helpMe(nums, goal) This function will count all subarrays with sums less than or equal to 2. Subarrays with sum 0: [] Subarrays with sum 1: [1], [0], [1], [0], [1], [1,0], [0,1], [1], [1,0] Subarrays with sum 2: [1,0,1], [0,1,0], [1,0,1], [1,0], [0,1,0,1], [1,0,1], [1,0,1] Calculate helpMe(nums, goal - 1) This function will count all subarrays with sums less than or equal to 1. Subarrays with sum 0: [] Subarrays with sum 1: [1], [0], [1], [0], [1], [1,0], [0,1], [1], [1,0] Find the Exact Count The number of subarrays with sum exactly 2 is helpMe(nums, 2) - helpMe(nums, 1). Aya samjh....

  • @siddhantksingh2514
    @siddhantksingh2514Ай бұрын

    r less than n not r less than equal to

  • @harshdhochak8361
    @harshdhochak836129 күн бұрын

    here is the c++ code; class Solution { public: // it will calculate for

  • @thaman701
    @thaman7013 ай бұрын

    Striver be like ----goli ki speed se videos upload kr dunga.😂❤

  • @PratapSingh-yg8tc
    @PratapSingh-yg8tc2 ай бұрын

    It becomes hard problem when we try to solve using sliding window but for you everything is so easy ,how we can think like you

  • @ajithshetty1684
    @ajithshetty1684Ай бұрын

    But how to return this value in function in leetcode, wont it be recursive?

  • @ajithshetty1684

    @ajithshetty1684

    Ай бұрын

    class Solution: def numSubarraysWithSum(self, nums: List[int], goal: int) -> int: def fun1(nums,goal): if goal return 0 l = 0 r = 0 sum = 0 cnt = 0 while r goal: sum = sum-nums[l] l=l+1 cnt+=r-l+1 r=r+1 return cnt return fun1(nums,goal)-fun1(nums,goal-1)

  • @rishabsharma5307
    @rishabsharma530717 күн бұрын

    Solution using the same method as L7 TC: O(2*n) SC: O(n) in worst case ``` int numSubarraysWithSum(vector& nums, int goal) { int i, j, sum, count, n = nums.size(); queue mq; i = j = sum = count = 0; int k = goal > 0 ? 1 : 0; while(j { sum += nums[j]; if(nums[j] == k) mq.push(j); while(sum > goal) { sum -= nums[i]; if(nums[i] == 1) mq.pop(); ++i; } if(sum == goal) { if(goal > 0) count += mq.front() - i + 1; else count += j-i+1; } ++j; } return count; } ```

  • @divyanshsingh6668
    @divyanshsingh666823 күн бұрын

    class Solution { public: int ans(vector& nums, int goal){ if (goal

  • @abhaysinhgaikwad
    @abhaysinhgaikwad3 ай бұрын

    anyone fix this class Solution { public int numSubarraysWithSum(int[] arr, int k) { int n = arr.length, count = 0, sum = 0, r= 0, l = 0; while(r sum += arr[r]; while(sum > k){ sum -= arr[l]; l++; } if(sum == k){ count += r - l + 1; } r++; } return count; } }

  • @tejaswaniguttula5961

    @tejaswaniguttula5961

    3 ай бұрын

    @abhaysinhgaikwad Hi brother, class Solution { public int func(int[] arr, int k) { int sum = 0, l = 0, r = 0, count = 0; if (k while (r sum += arr[r]; while (sum > k) { sum -= arr[l]; l++; } if (sum

  • @dayashankarlakhotia4943
    @dayashankarlakhotia49433 ай бұрын

    public int numSubarraysWithSum(int[]nums,int goal){ int prefixZero=0,sum=0,ans=0,i=0,j=0; while(j

  • @raparthipranay785
    @raparthipranay7852 ай бұрын

    its not working for nums = [0,0,0,0,0] goal = 0

  • @saikumarpeddineni

    @saikumarpeddineni

    2 ай бұрын

    No it works in this case too

  • @raparthipranay785

    @raparthipranay785

    Ай бұрын

    Yeah

  • @KartikeyTT
    @KartikeyTT3 ай бұрын

    it should be r

  • @manikanta6183

    @manikanta6183

    3 ай бұрын

    Since the array is 0-Indexed. Indexing -> 0,1,2,3 for example nums = {6,4,3,7} nums.size() would be 4, So ultimately we would be accessing the index which is out of bounds

  • @KartikeyTT

    @KartikeyTT

    3 ай бұрын

    @@manikanta6183 yeah thats what i meant

  • @manikanta6183

    @manikanta6183

    3 ай бұрын

    ​@@KartikeyTTMy bad, I thought you were asking the question 😂

  • @KartikeyTT

    @KartikeyTT

    3 ай бұрын

    @@manikanta6183 haha

  • @alisheheryar1770
    @alisheheryar177025 күн бұрын

    I am watching and I understand the code. But I can't give you a like on Instagram. Instagram use hi nahien krta apka bhai.

  • @tadadadadada
    @tadadadadadaАй бұрын

    import java.util.HashMap; class Solution { public int numSubarraysWithSum(int[] nums, int goal) { int sum = 0; int count = 0; HashMap prefixSums = new HashMap(); prefixSums.put(0, 1); // There's one way to have a sum of 0, by taking no elements. for (int num : nums) { sum += num; // If sum - goal has been seen before, it means there's a subarray ending at the current index // which sums to the goal. if (prefixSums.containsKey(sum - goal)) { count += prefixSums.get(sum - goal); } // Add the current sum to the map of prefix sums. prefixSums.put(sum, prefixSums.getOrDefault(sum, 0) + 1); } return count; } }

  • @9911587855
    @9911587855Ай бұрын

    Why can't we use this solution for the original subarray problem without binary elements?

  • @sumitdas2147

    @sumitdas2147

    26 күн бұрын

    Well, I had the same doubt, I realized that in the original problem, the goal or K can be negative. This algorithm fails to handle negative sum value. Also try dry running for this case {-1, -1, 1} with k = 0, you will get the answer as zero. But the correct answer should be 1. Why this algorithm fails? It is because the overall sum b/w left & right can be less than K, but the current element pointed by right is where we are not sure of it, whether it is less than equal to K or not. And this algo add that case in the overall count. Basically, this algo fails to handle negative integers. If someone has a better explanation, please continue this thread.