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

preview_player
Показать описание


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.

You can follow me across social media, all my handles are below:

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
Рекомендации по теме
Комментарии
Автор

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 👍

summerray
Автор

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!!

aryanmaniyar
Автор

#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR,

shubhamagarwal
Автор

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 💯

simplify
Автор

Striver Bhaiya, im glad to have a Guru like you who makes video even when you are at Google .. Thank you for your help...Aapki wajah se main logic build karpaya hoon ..

vinayakrodd
Автор

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

mehulthuletiya
Автор

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.

HarshKumar-ipnr
Автор

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

AnmolGupta-ojlm
Автор

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.

thechampion
Автор

Happy Teachers Day to the Best Teacher in this world Sir

sivaharshithab
Автор

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<int, int> prevXors;
prevXors[curXor]++;
for(int i=0;i<a.size();i++){
curXor^=a[i];

cnt+=prevXors[curXor^b];
}
prevXors[curXor]++;
}

return cnt;
}

priyapandey
Автор

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.

mohdnabeel
Автор

Very nice explanation sir, Thank you!

VishalYadav-gkkg
Автор

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

rohitkumarpilania
Автор

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

cinime
Автор

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

sumitkanth
Автор

Understood boss..
We miss you Striver

habeeblaimusa
Автор

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

AbcXyz-lksh
Автор

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

aniketsinha
Автор

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

harshkaira