Number of Subarrays with xor K | Brute - Better - Optimal

Поделиться
HTML-код
  • Опубликовано: 6 июл 2024
  • Problem Link: bit.ly/3jLfElm
    Notes/C++/Java/Python codes: takeuforward.org/data-structu...
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course
    01:08 Problem Statement
    03:05 Brute force approach
    03:46 Pseudocode
    05:43 Complexity
    06:11 Better approach
    07:23 Pseudocode
    07:38 Complexity
    08:04 Optimal approach
    08:11 Intuition
    15:18 Pseudocode / Dry-run
    21:28 Code
    23:21 Complexity

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

  • @takeUforward
    @takeUforward  10 месяцев назад +31

    Please watch our new video on the same topic: ruclips.net/video/eZr-6p0B7ME/видео.html

  • @takeUforward
    @takeUforward  Год назад +230

    We deserve one like, isn't it 😍

  • @summerray1795
    @summerray1795 10 месяцев назад +24

    for anyone not able to solve this one try to relate it to the count subarrays with sum k using prefix sum intuition that code is exactly similiar just dry run once and you will be able to solve it , may be most people are not able to solve it in mind or paper because it is difficult to solve xor in mind than the addition or subtraction , I hope this helps 👍

    • @rajasarkar2397
      @rajasarkar2397 3 дня назад

      i was just typing the same. Thanks buddy

  • @aryanmaniyar3475
    @aryanmaniyar3475 10 месяцев назад +12

    After understanding the "count subarray with sum K" problem and trying this one on my own, I was able to solve it without looking at the video solution beforehand. Thank you so much sir :) Understood!!

  • @simplify5251
    @simplify5251 Год назад +15

    I haven't watched this lecture but I would like to thank you for all of your previous efforts. Currently solving your DSA sheet and whenever I'm stuck at a problem your explanation make it a piece of cake. Thanku bhaiya 💯

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm 18 часов назад

    The Best explanation I get. Yesterday only encounter a question in contest Subarray with AND k. So instantly came here to learn.

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

    Understood! Super awesome explanation as always, thank you very very much for your effort!!

  • @stith_pragya
    @stith_pragya 5 месяцев назад +1

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

  • @mehulthuletiya497
    @mehulthuletiya497 Год назад +8

    01:08 Problem Statement
    03:05 Brute force approach
    03:46 Pseudocode
    05:43 Complexity
    06:11 Better approach
    07:23 Pseudocode
    07:38 Complexity
    08:04 Optimal approach
    08:11 Intuition
    15:18 Pseudocode / Dry-run
    21:28 Code
    23:21 Complexity

    • @mysticlunala8020
      @mysticlunala8020 Год назад +2

      Itna time question solve krne me diya hota to zyada fayda hota. Khair.

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

      Arre, Mene pura video dekhne ke bad mein hi pura samaj kar Timestamp add Kiya hua hain... Bina pura samje kese mein add kar sakta hu

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

      @@mehulthuletiya497 koi na 💀

  • @HarshKumar-ip5nr
    @HarshKumar-ip5nr Год назад +4

    understood. for those who are confused why we initialize over the map as {0, 1}, xor equals zero should be present otherwise we will generate one answer less than expected. Do dry run and you will get it.

    • @Nimbus2k_rider
      @Nimbus2k_rider Месяц назад

      We can avoid initializing map if we put one more condition "if(xr == k)cnt++;"

  • @dhruvsolanki4473
    @dhruvsolanki4473 11 месяцев назад +1

    Amazing concept explanation!

  • @VishalYadav-gk1kg
    @VishalYadav-gk1kg 2 месяца назад +1

    Very nice explanation sir, Thank you!

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

    Understood boss..
    We miss you Striver

  • @NazeerBashaShaik
    @NazeerBashaShaik 3 месяца назад

    Understood, thank you.

  • @thechampion808
    @thechampion808 Год назад +2

    Basically we have to find and check whether x^xr = k with current element. calculate xr with current ele and for x we have derived formula. now if you have xor value with x in hashmap, increment count then add current xor to hashmap.

  • @YourCodeVerse
    @YourCodeVerse 8 месяцев назад

    Understood✅🔥🔥

  • @sayyidzyanashraf
    @sayyidzyanashraf 24 дня назад

    شرح جميل جدا... احبك سيدي ❤❤❤

  • @AbcXyz-lk6sh
    @AbcXyz-lk6sh Год назад

    Great and detailed lecture as always. But please do include java code too

  • @AmanChauhan-hr1wh
    @AmanChauhan-hr1wh Год назад +1

    nice explanation striver sir❤

  • @AniketKumar-hf2bo
    @AniketKumar-hf2bo 5 месяцев назад

    understood ,tnx for the great explanation ❤❤💕💕👌👌👌👌

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 3 месяца назад

    Great Lecture........

  • @saikrishna872
    @saikrishna872 Год назад +1

    UNDERSTOOD

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

    Understood 💯💯💯

  • @rohitkumarpilania94
    @rohitkumarpilania94 Год назад +1

    Jaldi videos banado bhai, padhne ka man ho raha hai boht zyada

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

    Understood!

  • @pulkitgupta669
    @pulkitgupta669 10 месяцев назад

    Understood ❤❤

  • @her_soulmate
    @her_soulmate 10 месяцев назад

    Understood 🎉

  • @divyatejaswinivengada6368
    @divyatejaswinivengada6368 6 месяцев назад

    understood!!

  • @mohdnabeel702
    @mohdnabeel702 Год назад +15

    Hey guys just a heads up, the link in the sheet leading to GFG platform considers even non contiguous array subsets as answers, therefore leading to failed testcases.

    • @user-xb1st2pm6i
      @user-xb1st2pm6i Год назад

      Exactly.

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

      thats actually a problem of DP dont so with this approach, use recursion

    • @Yonatan2479
      @Yonatan2479 Год назад +1

      thanks for mentioning this. You saved me alot of time

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

      @@Yonatan2479 Dont mention it ✌️

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

      hey thanks !

  • @konankikeerthi
    @konankikeerthi 26 дней назад

    Thanks bro. understood

  • @deepanshudahiya8966
    @deepanshudahiya8966 Месяц назад

    understood it clearly

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

    Nice video love u bro

  • @culeforever5408
    @culeforever5408 8 месяцев назад +1

    understood :)

  • @sumitkanth5349
    @sumitkanth5349 10 месяцев назад

    23:57 here your map stores x values with 0 ( cnt+= mpp[x] ) if x not exists, so if we have uniques x every time then your space complexity will we O(2n)
    1st n for every unique xr and 2nd n will be for unique x :) tell me if I am wrong or right ! btw you are the best teacher >>

  • @priyapandey8951
    @priyapandey8951 8 месяцев назад +1

    Bhaiya, I think at 22:44 instead of checking the presence of x in the map like that, it would be better if we manually check that by using conditional statement. Both are giving the same result but in your case unnecessary values are getting stored in the map because of this like cnt+=mpp[x]. If x is not present it does give zero but x is also getting added to map which is not required.
    int subarraysWithXorK(vector < int > a, int b) {
    int cnt=0;
    int curXor=0;
    unordered_map prevXors;
    prevXors[curXor]++;
    for(int i=0;i

    • @rishabhsinha7250
      @rishabhsinha7250 Месяц назад +1

      this is actually the most optimal solution, thanks bro

  • @per.seus._
    @per.seus._ 11 месяцев назад +1

    understood

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

    Understood

  • @user-vk8db3ph2v
    @user-vk8db3ph2v 11 месяцев назад

    Understood! 🫡

  • @shaiksoofi3741
    @shaiksoofi3741 4 месяца назад

    underatood

  • @harshitjaiswal9439
    @harshitjaiswal9439 4 месяца назад

    understood.

  • @siddhijaiswal8038
    @siddhijaiswal8038 Год назад +4

    is there anyone who is unable to think of optimized solution on their own?i hope it's not me alone!!!

    • @utsavseth6573
      @utsavseth6573 11 месяцев назад +9

      of course its difficult, no one can invent solutions like this, then also code properly, without any errors satisfying those test cases, and that too in a 40 minutes exam/interview. All we can do is to study as many questions as possible, and hope if by practice and luck, those questions come, maybe with a slight variation. Solving an entire new question altogehter is too diffuclt.

    • @sp_9467
      @sp_9467 11 месяцев назад +1

      @@utsavseth6573 Exactly

  • @user-lq2ld6cg6v
    @user-lq2ld6cg6v 3 месяца назад

    why we have not taken the condition where if (xr == k) we increment the count because that also will be a subarray starting from 0th index , right , having the xor as k

  • @meme_eternity
    @meme_eternity 10 дней назад

    Great

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

  • @harshkaira945
    @harshkaira945 11 месяцев назад +1

    dont use the link from SDE sheet to get to CN site , just search the question or u will get to the wrong question

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

    I can understand your optimal solution but at first go when I tried my self I'm not able to come up with this approach. how to improve this thing.

    • @senseiAree
      @senseiAree 9 месяцев назад +3

      Trial and error... striver's problem solving videos are not about problem solving... he teaches how to approach a problem... not how to solve one particular problem... that is ... each and every problem solving video is this DSA playlist of his are different..
      Each having different approaches... learn the approach and understand when and why we need them... a strong grasp in the sorting algorithms and its logical implementation around when, why and how we implement it would help you... most of the problem solving questions are based on these approaches or the combination of these approaches. Good luck brother ❤

  • @MindBrowsingg
    @MindBrowsingg Год назад +5

    Code not working for gfg platform

  • @skasrafali8707
    @skasrafali8707 2 месяца назад

    striver please complete the string parts.

  • @aniketsinha2826
    @aniketsinha2826 8 месяцев назад +1

    it is same as longest subarray whose sume is k !!

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

    bhaiya unique no occurences wala que kara dena
    bahut pareshan ho chuka hu is que me

    • @mehulthuletiya5638
      @mehulthuletiya5638 Год назад +1

      Arre, Map + Set ka use karlo ho jayega Jo basic concept aate hoge to.
      Intuition:
      Map (Frequency + size) + Set (Unique element + size)
      If ( map size == set size)
      Means have a unique no of occurrence.

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

    how to proceed with subsets in this problem!

  • @sauravchandra10
    @sauravchandra10 Год назад +2

    For those jinka GFG pe ye code run nahi horha, the problem there is asking for subsets, which need not be continuous. So to solve that problem, we have to apply concept of dynamic programming. Jinko dekhna hai how this works they can look for video number 17 in striver's DP playlist.
    And jinko sirf code submit krna hai, here is the code:
    class Solution{
    public:
    int recur(int index, int val, int k, vector &arr, vector &dp){
    if(index==arr.size()){
    if(val==k) return 1;
    return 0;
    }
    if(dp[index][val]!=-1) return dp[index][val];
    int pick=recur(index+1,val^arr[index],k,arr,dp);
    int notPick=recur(index+1,val,k,arr,dp);
    return dp[index][val]=pick+notPick;
    }
    int subsetXOR(vector arr, int N, int K) {
    vector dp(N+1,vector(5000,-1));
    return recur(0,0,K,arr,dp);
    }
    };

  • @ish8591
    @ish8591 8 месяцев назад

    the intuition was like maths theorem like to prove this that .....

  • @vibhanshusharma9143
    @vibhanshusharma9143 14 часов назад

    What to do it xor -> and😢

  • @VINAYSINGH-wc8sq
    @VINAYSINGH-wc8sq Год назад +2

    Bhai sliding window ka bhi playlist bna do

    • @113_manshi4
      @113_manshi4 Год назад

      He's making videos following his A2Z sheet so

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

    i can do dsa

  • @lostcyrus8578
    @lostcyrus8578 Год назад +4

    Bhaiya pls videos thodi jaldi lao na

    • @takeUforward
      @takeUforward  Год назад +15

      haan aur ek aaj aaega
      ye week arrays khatam kr dengey, fir next week se binary search suru!

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

      @@takeUforward Yes bhaiya waiting for it🙌❤️ You are doing great work💫

    • @VINAYSINGH-wc8sq
      @VINAYSINGH-wc8sq Год назад

      ​@@takeUforward bhai sliding window ka bhi playlist

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

      @@takeUforward damn Mann salute 🫡 to your efforts, you are an inspiration

  • @SAACHIPANDEY
    @SAACHIPANDEY 5 дней назад

    7:40

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

    Good explanation but same problem with slightly modified. You have already have so many videos. Please make unique problem videos

    • @takeUforward
      @takeUforward  Год назад +19

      The reason you find it easy is because I teach you problem solving and not mugging up :) This is the reason we follow patterns, how will I produce unique patterns now ;)

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

      @@takeUforward exactly

  • @alphaCont
    @alphaCont 6 месяцев назад

    ok

  • @sonubedi6680
    @sonubedi6680 6 месяцев назад

    Bhai teri avaz ben 10 alien force/ultimate alien wali hai

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

    us

  • @royalsingh9400
    @royalsingh9400 Год назад +1

    Mujhe problem samajh aa jata par code nhi kar pata. Kya karu mai

    • @Krishnayadav-fu3uv
      @Krishnayadav-fu3uv Год назад

      mere sath bhi same hota hai bhai , maine bhi abhi start krra hai , mayB hamne itna code nai krra ki problem smjhne baad code kr paye . solution mile toh batao

    • @HimanshuTiwari-ou3uj
      @HimanshuTiwari-ou3uj Год назад +2

      Easy tag ke 50-60 problems bana lo gfg pe, aise jismein solution naa dekhna pade 100% tum karhi lo waise probs , toh aise probs jo tumne pehle kabhi dekha naa ho aur tum khud se bana le rhe ho agr, toh yeh confidence boost karega, then with time confidence + implementation practice mil ke tough probs bhi banne lageinge!

  • @priyankasetiya1358
    @priyankasetiya1358 21 день назад

    public class Solution {
    public int solve(ArrayList A, int B) {
    Map map=new HashMap();
    map.put(0,1);
    int xor=0;
    int count=0;
    for(int n:A){
    xor=xor^n;
    int x=xor^B;
    if(map.containsKey(x)){
    count+=map.get(x);
    }
    map.put(xor,map.getOrDefault(xor,0)+1);
    }
    return count;
    }
    }

  • @gunasundarchoppa4089
    @gunasundarchoppa4089 2 месяца назад +1

    such a poor explanation when compared to other problem explanations

  • @luvsharma5936
    @luvsharma5936 4 месяца назад

    he don't know how to teach

    • @shashwatraii
      @shashwatraii 3 месяца назад +2

      You don't know how to study

  • @helloworld004
    @helloworld004 6 месяцев назад

    #include
    int subarraysWithSumK(vector < int > a, int b) {
    // Write your code here
    int num=0;
    mapm1;
    m1[0]++;
    int xr=0;
    for(int i=0;i

  • @nousadali4808
    @nousadali4808 4 месяца назад

    UnderStood

  • @RituSingh-ne1mk
    @RituSingh-ne1mk 6 месяцев назад

    Understood!

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

    Understood

  • @AbjSir
    @AbjSir 8 месяцев назад

    understood

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

    Understood !

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

    Understood

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

    Understood

  • @AzizurRahaman-zq4io
    @AzizurRahaman-zq4io 8 месяцев назад

    Understood

  • @dewanandkumar8589
    @dewanandkumar8589 6 месяцев назад

    Understood

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 5 месяцев назад

    Understood

  • @user-st9ff2vh6l
    @user-st9ff2vh6l 6 месяцев назад

    Understood

  • @user-sm7zo5zd9t
    @user-sm7zo5zd9t 4 месяца назад

    Understood

  • @mohammadhanif3016
    @mohammadhanif3016 5 месяцев назад

    Understood

  • @AnuragParoha-ck5ds
    @AnuragParoha-ck5ds 3 месяца назад

    Understood

  • @abhishekprasad010
    @abhishekprasad010 3 месяца назад

    Understood

  • @user-sm7zo5zd9t
    @user-sm7zo5zd9t 4 месяца назад

    Understood

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

    Understood

  • @codingp110
    @codingp110 2 месяца назад

    Understood

  • @MATH_VIBE
    @MATH_VIBE 10 месяцев назад

    Understood

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

    understood

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

    understood

  • @firebout7675
    @firebout7675 6 месяцев назад

    understood

  • @user-rg7bi1qp2l
    @user-rg7bi1qp2l 5 месяцев назад

    understood

  • @jashanuppal9678
    @jashanuppal9678 4 месяца назад

    understood

  • @chiragbansod8252
    @chiragbansod8252 3 месяца назад

    understood

  • @ashishpradhan6250
    @ashishpradhan6250 Месяц назад

    understood

  • @user-sl9xm3cm3p
    @user-sl9xm3cm3p Месяц назад

    understood

  • @abhinanda7049
    @abhinanda7049 9 месяцев назад

    understood

  • @RishabhKumar0094
    @RishabhKumar0094 26 дней назад

    understood

  • @MJBZG
    @MJBZG 14 дней назад

    understood