Тёмный

Find the Missing and Repeating Number | GFG | C++ and Java | Brute-Better-Optimal-Optimal 

take U forward
Подписаться 698 тыс.
Просмотров 229 тыс.
50% 1

Опубликовано:

 

26 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 510   
@takeUforward
@takeUforward 4 года назад
Please watch the new video which covers it in more depth, and also prints it: ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-2D0D8HE6uak.html Drop "Understood" if you did understand, or else drop your doubt portion.. ^ _ ^ Code link: takeuforward.org/data-structure/find-the-repeating-and-missing-numbers/ . If you appreciate the channel's work, you can join the family: ru-vid.com/show-UCJskGeByzRRSvmOyZOz61igjoin
@sushmabn1391
@sushmabn1391 4 года назад
Yea...perfect liked it
@mohdbaqerhaider3440
@mohdbaqerhaider3440 4 года назад
you should try to named your channel as a portion of your channel name should have something from computer science or competative prog or ds and algo this will help to grow your channel fast , and your videos will appear in searches with any genral term of computer science as exam i found a channel named algods while i was sarching for some algorithm ananlysis topic.
@DeepakPrajapati-cr4sf
@DeepakPrajapati-cr4sf 4 года назад
Bro please make a video on Bit manipulation. BTW video is great but as I am not strong in bit manipulation so i didn't understand 4th approach.
@sahilchoudhary1473
@sahilchoudhary1473 4 года назад
Yes understand
@DeepakKumar-yl3ok
@DeepakKumar-yl3ok 4 года назад
If the Array 1 is : 1 1 2 3 5 6 and the Array 2 is :- 1 2 3 4 5 6 Xor both of them you will get 1^4 = 101 , so the right most bit which is 1 is 2nd bit is considered that Xor of the first set will give -> missing number and the xor of the second set give-> repeating number, but if we consider the first set bit which is 1 from left which is at the 0th position , the first set will give -> repeating number second set will give -> missing number why this is happening?
@bismeetsingh352
@bismeetsingh352 4 года назад
Xor solution is something that even the interviewer wont know.
@rahilsinha_1407
@rahilsinha_1407 3 года назад
😂
@the_shridhar
@the_shridhar 3 года назад
😆😆😆😆😆
@a.s.v.aditya123
@a.s.v.aditya123 3 года назад
True...
@willturner3440
@willturner3440 3 года назад
Satya vachan paaji 👐
@Rajat-Sharma1
@Rajat-Sharma1 3 года назад
😂😂
@tanmaythaker2905
@tanmaythaker2905 4 года назад
This channel is actually taking me forward towards my goal!
@_-6912
@_-6912 3 года назад
Same feeling here too Tanmay!
@roronoazoro3519
@roronoazoro3519 2 года назад
Have you reached your goal, it has been an year, just asking out of curiosity.
@notdumb3182
@notdumb3182 2 года назад
so what happened did you reached your goal ??
@sainipankaj
@sainipankaj 2 года назад
have you reached your goal It's 2 years
@Harsh-di5rl
@Harsh-di5rl Год назад
have you reached your goal its been more than 2 years
@aishwaryamajumdar6927
@aishwaryamajumdar6927 4 года назад
We could also use a bit mask (of size N) and set the indexes which are (arr[i] - 1). When an index is set in the mask already, it means that is the duplicate element. We traverse through the mask to see which index is unset and output that index + 1 as the missing element. Time- O(2N) ~ O(N) and Space - O(1).
@takeUforward
@takeUforward 4 года назад
How come space is O(1), what if we have numbers like 10000, then you have to create a huge mask, which cannot be stored and you have to eventually create an array
@aishwaryamajumdar6927
@aishwaryamajumdar6927 4 года назад
Although, this wouldn't work for a very large N :P
@_-6912
@_-6912 3 года назад
I felt the first solution of this video really good! Other solutions too are good but this was much easy to understand or I'm slow!
@sujan_kumar_mitra
@sujan_kumar_mitra 3 года назад
It's very difficult to remember the xor approach. I could only come up with the summation approach by myself
@vgenius2625
@vgenius2625 2 года назад
same :(
@SreekantShenoy
@SreekantShenoy 2 года назад
@@vgenius2625 check this shorter ans :) vector nums = {3, 1, 2, 5, 3}; int bothXor = 0; for (int i = 0; i < nums.size(); i++) bothXor ^= ((i + 1) ^ nums[i]); int repeatingNum; for (int i = 0; i < nums.size(); i++) { if (nums[abs(nums[i]) - 1] < 0) { repeatingNum = abs(nums[i]); break; } nums[nums[i] - 1] *= -1; } cout
@yhbarve
@yhbarve Год назад
That's great too! Did you solve similar questions, or did you just come up with it intuitively?
@rakeshsaraf2963
@rakeshsaraf2963 Год назад
another better approach is: int[] findTwoElement(int arr[], int n) { int[] res = new int[2]; long totalN = (long) n*(n+1)/2; long sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } for (int i = 0; i < n; i++) { int remain = n - Math.abs(arr[i]); if (arr[remain] > 0){ arr[remain] *= -1; }else{ res[0] = Math.abs(arr[i]); } } sum = sum - res[0]; res[1] = (int) (totalN - sum); return res; }
@rakeshsaraf2963
@rakeshsaraf2963 Год назад
just dry run it once and you will understand it
@prajjwalagarwal4314
@prajjwalagarwal4314 4 года назад
Can you cover up more no. of questions in a single video? The interview process has been started by many companies. it will be helpful for us to complete them in short period of time. Otherwise your channel is awesome :D
@hardikparmar4472
@hardikparmar4472 4 года назад
no reply from striver... i commented same on other video but there is no reply...
@prajjwalagarwal4314
@prajjwalagarwal4314 4 года назад
@i've abandoned myChild felt sad that you have abandoned your child tho.
@prajjwalagarwal4314
@prajjwalagarwal4314 4 года назад
@i've abandoned myChild Be in your limits sir. I asked if he could cover up more questions in a single video not asking for solutions. So better you mind your own business.
@hardikparmar4472
@hardikparmar4472 4 года назад
@i've abandoned myChild why don't you go fuck your self?
@acsp1054
@acsp1054 4 года назад
@i've abandoned myChild I guess you're sick. Don't be too hard on yourself !
@yuvrajoberoi7834
@yuvrajoberoi7834 4 года назад
Bro, I did this question before and stopped at the summation soln as it was optimal. I think if I tell the xor soln the interviewer would be sure that I had already done the question. Will it be a bad thing, as he may say that if you had known the soln then you should already tell the most optimal approach and save time for both of us?
@anshumandas5392
@anshumandas5392 4 года назад
Really a very important question. Striver, please do try to answer this. Becoz if we tell such solutions like the last one, it doesn't show our logical/thinking ability because no one can get such a solution by himself. Moreover, it may create a bad impact on the interviewer that we have mugged up the solution. I was getting this doubt a lot of times while preparing...
@hemanthvarmas
@hemanthvarmas 4 года назад
@@anshumandas5392 Yes, the question is a really good one. I was also asking this to myself for quite sometime. I didn't find any answer. Please reply to this bro.
@ramranjankumar6109
@ramranjankumar6109 4 года назад
Le Indian Education System- Ratt lo sara zahan. Fir toh paisa hi paisa hoga😂
@hritwiksom3032
@hritwiksom3032 4 года назад
Thing is both interviewer n interviewee know that questions are repeated, but never reveal it to the interviewer that you have faced the exact same problem before. If they were to ask you that, say that you have faced a similar problem before and always go from brute to optimal. Mostly stick with the square solution unless the interviewer really wants you to optimise the overflow condition, most cases they won't.
@hemanthvarmas
@hemanthvarmas 4 года назад
@@hritwiksom3032 Thanks mate for the insights ✌️✌️
@ayushmandloi5011
@ayushmandloi5011 2 года назад
in XOR approach we are actually using 0(2N) space , that baskets . so hashing could be more optimal
@ayushmandloi5011
@ayushmandloi5011 2 года назад
No we don't need to store them just keep xoring them in single variable
@fullysimplified7139
@fullysimplified7139 3 года назад
This one is too good. I saw this video earier and left it as I was not able to grasp the whole logic that time Saw this question again today and I knew where to go to learn the logic Thanks a ton!!!!
@kshitijmishra2716
@kshitijmishra2716 2 года назад
where?
@sarahdaniel6862
@sarahdaniel6862 3 года назад
1. Sum of n integers - sum of all array elements. 2. Find repeating element through sorting and add it to the answer got from 1. This gives the missing element. Eg. {4,3,6,2,1,1}; 1. 21-17=4; 2. 4+1=5; 1 is repeating and 5 is missing. Let me know if this approach is correct for all edge cases.
@nawalrathore3334
@nawalrathore3334 4 года назад
New name with same energy.... loving it♥️🙏
@roshangupta629
@roshangupta629 2 года назад
That's a Great Explanation Thanks Striver.
@christianoronaldo1662
@christianoronaldo1662 4 года назад
xor portion was confusing.
@praneethyennam9181
@praneethyennam9181 4 года назад
Haa
@sameer5628
@sameer5628 4 года назад
xor of same number result in zero. leaving behind the different number! take an example 1231234 in the array if u want to find out the number which have only one occurrence.. simply xor them you'll end up with 4 only! since x*x=0 where x=1,2,3. Here in the question (4,3,6,2,1,1) if u xor them with 1 ton meaning (443^6^2^1^1]*[14243444546] same number gets cancel results in num= [1^5] and which u have to compute the missing number and repeating one! soo to find 1 and 5 u have to do the remaining step!
@sameer5628
@sameer5628 4 года назад
A similar problem- wap to find unique number in an array of n numbers in which except one (unique number) rest all are present thrice! i/p = [1,2,3,1,2,3,4,1,2,3] o/p = 4
@yashsonawane2227
@yashsonawane2227 4 года назад
Hey, Ronaldo learning coding !!!
@devanshmesson2777
@devanshmesson2777 3 года назад
I have explained the XOR method in detail with the help of an example. You can check the link - github.com/zeph0yr/Competitive-programming/blob/master/Striver's%20SDE%20sheet/Day:1%20(Arrays)/Find%20Repeat%20and%20Missing%20Number.cpp
@twinklebajaj5461
@twinklebajaj5461 4 года назад
I wonder how beautifully someone can come up with such a crystal clear explanation! Hats off to you! Huge fan sir ! :p
@yashaswibiyahut1474
@yashaswibiyahut1474 4 года назад
Not understood the XOR portion.
@sameer5628
@sameer5628 4 года назад
xor of same number result in zero.. leaving behind the different number! take an example [1,2,3,1,2,3,4] in the array if u want to find out the number which have only one occurence.. simply xor them you'll end up with 4 only! since x^x=0 where x=1,2,3... Here in the question [4,3,6,2,1,1] if u xor them with 1 to n meaning [4^3^6^2^1^1]^[1^2^3^4^5^6] same number gets cancel.. results in num= [1^5] and which u have to compute the missing number and repeating one! soo to find 1 and 5 u have to do the remaining step!
@sameer5628
@sameer5628 4 года назад
A similar problem- wap to find unique number in an array of n numbers in which except one (unique number) rest all are present thrice! i/p = [1,2,3,1,2,3,4,1,2,3] o/p = 4
@shubhamladha5426
@shubhamladha5426 4 года назад
learn basics of bit masking then it be very easy to understand and only then it makes sense. it took me 2 days to completely understand and code this problem. theirs a webinar by prateek narang on bit masking go watch it you will be able to understand this q.
@saebalam2498
@saebalam2498 3 года назад
see my comment
@devanshmesson2777
@devanshmesson2777 3 года назад
I have explained the XOR method in detail with the help of an example. You can check the link - github.com/zeph0yr/Competitive-programming/blob/master/Striver's%20SDE%20sheet/Day:1%20(Arrays)/Find%20Repeat%20and%20Missing%20Number.cpp
@growmore1672
@growmore1672 2 года назад
one more approach(easy): gfg all test cases passed. TC - o(n) SC - o(1) int *findTwoElement(int *arr, int n) { // fill all elements of arr at position as // according to sorted manner // ex:: 3 2 1 2 after this 1st for loop changes to // 1 2 3 2 for(int i = 0;i
@Raju_Sharma852
@Raju_Sharma852 Год назад
You used the cyclic sort method. Wonderful
@jaiminsolanki5478
@jaiminsolanki5478 4 года назад
Understood Brother... Thank you for playing such a vital role in making our career!
@naro.tam_
@naro.tam_ 3 года назад
Take one bucket only then you will get one of (missing and repeting) element then xor it with the previous xor result we got that is missing^repetting.You will get 2nd number. No need to take 2 buckets
@akashkarn8429
@akashkarn8429 2 года назад
Understood completely. Thanks
@divyanshupandey1702
@divyanshupandey1702 3 года назад
i really got motivated after seeing even after getting placed you teach more than i study thanks for the lectures
@misbahsirnaik2189
@misbahsirnaik2189 4 года назад
Please try to complete the entire series ..thank you so much Sir 👍😀❤️ You are the best
@anuragtiwari9793
@anuragtiwari9793 Год назад
C++ solution: vector Solution::repeatedNumber(const vector &A) { int xorr = 0; for(int e: A){ xorr = xorr^e; } int n = A.size(); for(int i = 1; i
@sumanthchalavadi5579
@sumanthchalavadi5579 4 года назад
Your hardwork for us ........thank you very much striver
@rryann088
@rryann088 4 года назад
A M A Z I N G effort from you! A real thank you from the bottom of my heart! Please continue the whole process ! Much appreciated
@Iamshahrukhkhan-u2i
@Iamshahrukhkhan-u2i 4 года назад
Always learn a completely new & awesome thing from your videos bhaiya...thnx for your hard & continuos work..
@mianto2159
@mianto2159 4 года назад
Great work bro!! It helps a lot, please complete the series....
@yashaggarwal3226
@yashaggarwal3226 4 года назад
if we present the xor solution to the interviewer he would know that we have already see this problem and we were acting all along that we aren't working our way into the solution . So, doesn't it impact negatively on us ?? And on GFG there is a solution which take O(2N) time comp & O(1) space comp and it also seems bit more approachable in a interview So i think you should add that or mention that sol. in description or smthing BTW great course, love how you explain, thank you for your work
@takeUforward
@takeUforward 4 года назад
How can you know an algorithm without reading it? Everything is read previously, its just how you present! Also many programmers can figure this logic if they are deep into cp!
@yashaggarwal3226
@yashaggarwal3226 4 года назад
@@takeUforward Ok so all boils down to the way of presentation and Thanks for the prompt reply, Well just a new series suggestion from my side "mock recorded interview with white board showcasing not the problem but the way of presenting different sol.s and also dealing with tricky situation you might have faced in your interview exp. "
@vinaykumarkhardoniya9639
@vinaykumarkhardoniya9639 2 года назад
yo guys in the solution in which we make two equations can be optimised by using a simple trick. Insted of separetely calculating the sum of elements of array and sum of squares of elements of array separetely (which can cause problem if there are large elements in the array) we can simultaneously add and subtract the corresponding elements of the array and the actual array with elements like 1,2 ,......n; Here is the code hope it helps arrayvector Solution::repeatedNumber(const vector &a){ int n = a.size(); long long sqSum = 0; long long sum = 0; for(int i=0;i
@awalktoinfinity665
@awalktoinfinity665 4 года назад
1:39 thoda galat ho gya.. how come 2 is missing since 2 is present , 5 is missing na.. anyways itna chlta h :)
@rajgupta5158
@rajgupta5158 4 года назад
Ya , this approach (sorting) is tell the repeated no. Not the the missing number.
@abhishekjaiswal3673
@abhishekjaiswal3673 4 года назад
@@rajgupta5158 check the adjacent diff > 1 to find the missing number
@workforit1233
@workforit1233 4 года назад
@@abhishekjaiswal3673 kya baat ae bhai bahout bdia maza aagya
@sgr2683
@sgr2683 4 года назад
Can anybody explain me that first approach that how 2 is missing due to that I am also not able to understand that concept???
@awalktoinfinity665
@awalktoinfinity665 4 года назад
@@sgr2683 wo galti se bola h.. 5 is missing..chill
@roushanraj8530
@roushanraj8530 4 года назад
Amazing bro, keep doing all videos for your SDE sheet, this help us exponentially, Thank you for these awesome stuffs
@bong-techie
@bong-techie 2 года назад
please give the link of his sde sheet
@suravighosal9934
@suravighosal9934 Год назад
Understood. Thank you striver.
@DeepakKumar-yl3ok
@DeepakKumar-yl3ok 4 года назад
If the Array 1 is : 1 1 2 3 5 6 and the Array 2 is :- 1 2 3 4 5 6 Xor both of them you will get 1^4 = 101 , so the right most bit which is 1 is 2nd bit is considered that Xor of the first set will give -> missing number and the xor of the second set give-> repeating number, but if we consider the first set bit which is 1 from left which is at the 0th position , the first set will give -> repeating number second set will give -> missing number why this is happening?
@overclockinggames2419
@overclockinggames2419 4 года назад
Order of the container i.e first container gives missing no or repeating number doesn't matter because in the end we will traverse the arrey and check with both the missing and repeating number to find which one is missing or one which is repeating
@vigilanthuman
@vigilanthuman 2 года назад
Two buckets will take up the space of O(n) and have time complexity of O(n), then why not hashing solution? It has time complexity of O(2n) and space complexity of O(n) ? Which is better?
@prooAG
@prooAG 2 года назад
you don't need to store them in buckets , just store their xor.
@revanthreddy3174
@revanthreddy3174 2 года назад
this solution worked for a number like four, it would be more complex for a number in which X^Y is 2^p -1
@thekapilkhandelwal
@thekapilkhandelwal 4 года назад
Should i do these right now, just came in 3rd year? Or i should do these before placement session? Please telll!
@takeUforward
@takeUforward 4 года назад
Do one daily, no harm in this!
@charan775
@charan775 4 года назад
along with this you can keep practicing in codeforces
@vagabondfoodie5788
@vagabondfoodie5788 2 года назад
Hey do yo get placed?
@KartikayBhutani
@KartikayBhutani 4 года назад
There's actually one approach to find XOR of 1-N in O(N). So we probably don't have to run the second loop.
@takeUforward
@takeUforward 4 года назад
please read other comments, n the pinned comment also :)
@anubhavbansal8922
@anubhavbansal8922 4 года назад
I love your videos i watch them daily when i wake up
@amardhillon314
@amardhillon314 2 года назад
"Understood" that you are the best
@vaalarivan_p
@vaalarivan_p Год назад
7:35 - 11:50
@ravishankar9247
@ravishankar9247 2 года назад
Why did you add these extra lines in the code ? You also didn't mention about this in the video as well. Please explain x_count=0; for(int i=0;i
@return088
@return088 4 года назад
Wao straight to the point and explained very well
@samriddhashukla5345
@samriddhashukla5345 4 года назад
Bohot sundar bhaiya , please keep continuing :)
@josephsamuelm4608
@josephsamuelm4608 Год назад
The second solution does not pass all test cases in coding ninjas. It does not pass one test case. Can someone tell me what is that hidden test case
@SatyaMantha
@SatyaMantha Год назад
Striver, great work and explanation. However, it is very hard to remember the xor method. Personally i felt the mathematical one is easier.
@ManyaSmritiBIT
@ManyaSmritiBIT 4 года назад
Striver do make these kind of video, i never like or subscribe a channel but urs is an obvious one, plssss make more of these interview related videos.
@vamshimuluguri
@vamshimuluguri 4 года назад
Bro keep this type Videos everyday. This is very useful for everyone . RU-vid is recommended me btw. ❤️❤️
@ankitbaiskhiyar3106
@ankitbaiskhiyar3106 2 года назад
can we use slow and fast method to get the repeating elements in the array ??
@subhajitdas2784
@subhajitdas2784 2 года назад
Yes but then how will you get the missing number ?
@manistrikes
@manistrikes 2 года назад
@@subhajitdas2784 by having the summation of array and subtracting it with sum of 1 to n....by this we have repeating - missing element=diff.......and since we have repeating num via slow and fast cycle detection method we can directly use this equation and find the missing element
@samareshms4591
@samareshms4591 3 года назад
The space complexity of bit masking method is O(n) right If I am wrong please correct me
@samareshms4591
@samareshms4591 3 года назад
Ha bhai mujhe bhi yahi lag rha hai dono bucket ka size milaye toh N hoga toh space complexity O(N) hi hona chahiye
@Whirlwind03
@Whirlwind03 4 года назад
Here is my approach O(N) No need to sqaure the elements Using the Gaussian formula N = Len(arr) Sum1 = (N(N+1))//2 return Sum1 - sum(arr)
@takeUforward
@takeUforward 4 года назад
Think carefully that will never work, try running on test cases!
@Whirlwind03
@Whirlwind03 4 года назад
@@takeUforward Hye striver it actually passed all the test cases on leet code.
@takeUforward
@takeUforward 4 года назад
@@Whirlwind03 bro the problem you submitted at leetcode is different, please read the problem statement here carefully ! What you are doing is for missing number and no repeating ! Read the problem statement clearly bro !
@shreyansh2510
@shreyansh2510 4 года назад
super se bhi upar technique.............
@shubhi_115
@shubhi_115 3 года назад
Yes Sir we have understood , high tribute to you
@ragnarT3
@ragnarT3 4 года назад
Understood and keep up the good work
@RahulThakur-nj8kk
@RahulThakur-nj8kk 4 года назад
Your content is great try to include more sums in one video
@beinghappy9223
@beinghappy9223 2 года назад
Awesome explanation Striver 🔥
@software3089
@software3089 2 года назад
how in the world can someone solve this problem using last two methods without watching solution?
@ketanahuja8939
@ketanahuja8939 4 года назад
understood thanks
@sankalyadav9809
@sankalyadav9809 3 года назад
hey I understood all the optimal solutions, but I don't understand why the 3rd solution is more optimized because it takes O(5N) while the 1st one took only O(2N). so according to time complexity, 1st solution should be the more optimal solution
@darshika8808
@darshika8808 3 года назад
1st approach takes O(n) time and space complexity while the 3rd approach takes O(5n) (which is considered overall O(n)) time and O(1) space complexity , so , 3rd approach is much more optimized
@sankalyadav9809
@sankalyadav9809 3 года назад
@@darshika8808 Hmm got it 🙌, Btw thanks 👍
@mazinshamshad3838
@mazinshamshad3838 Год назад
Sir when I am running my code in VS Code it works perfectly fine but when it is not passes all the cases in the interview bit #include #include #include using namespace std; class Solution { public: vector repeatedNumber(const vector &A); }; bool compare(int a, int b) { return a < b; } vector Solution::repeatedNumber(const vector &A) { vector vr(2); // creating a vector of size 2 vector vrc(A.size()); for (int f = 0; f < A.size(); f++) { vrc[f] = A[f]; } sort(vrc.begin(), vrc.end(), compare); // sorting the vector for (int i = 0; i < A.size() - 1; i++) { if (vrc[i] == vrc[i + 1]) { vr[0] = vrc[i]; } } for (int i = 0; i < A.size(); i++) { if (vrc[i] != i + 1) { vr[1] = i + 1; break; } } return vr; } int main() { const vector v1{4, 1, 2, 5, 3, 6, 7, 8, 9, 10, 11, 12, 12}; Solution s; vector vr2 = s.repeatedNumber(v1); for (int x : vr2) { cout
@rajendragosavi2233
@rajendragosavi2233 2 года назад
Thank you for all your work. Just one dumb question - what is XOR here and how do i calculate ?
@amanvijayvargiya3468
@amanvijayvargiya3468 4 года назад
Last approach is magicallllllll😎😁
@ankitdebnath2597
@ankitdebnath2597 3 года назад
How can the last solution be most optimal? It is taking more space and more time than the hashing algorithm
@tanyacharanpahadi158
@tanyacharanpahadi158 4 года назад
Thank you for working so hard for us.
@satyamkumar2820
@satyamkumar2820 4 года назад
I was wondering if instead of rightmost bit, any set bit of (x^y) would work or not.
@takeUforward
@takeUforward 4 года назад
Yes it will, if you notice what I said, I said lets take..
@darshantank554
@darshantank554 2 года назад
Good solution but, how can you ensure that X is missing and Y is repeated? It could also be reversed
@siyangaming3242
@siyangaming3242 2 года назад
becoz in starting we already let the x as the repeating number and y as the missing number
@sarthakbehera8324
@sarthakbehera8324 2 года назад
you should make a linear check at the last to check which one is repeated and which one is missing.
@praneethyennam9181
@praneethyennam9181 4 года назад
hats off to your effects......
@bhoomikamaheshwari1833
@bhoomikamaheshwari1833 3 года назад
void solve(int arr[],int n) { int first_number=(n*(n+1))/2; int second_number; map m; for(int i=0; i 1) second_number = arr[i]; else first_number -= arr[i]; } cout
@dhrumishah1520
@dhrumishah1520 3 года назад
thank you for your guidance helped me a lot
@pranjalg_131
@pranjalg_131 4 года назад
Awesome Approach totally loved it
@gorakhchavan4213
@gorakhchavan4213 4 года назад
Bhaiya understood the concept but can you share a link to the code also.
@Rajat_maurya
@Rajat_maurya 2 года назад
one more approach is cycle sort TC -> O(n), SC -> O(1) int *findTwoElement(int *v, int n) { int i=0; static int ans[2]; while(i
@abhinavprasun3515
@abhinavprasun3515 4 года назад
The separation of the numbers into two buckets in last method is not only going to take O(n) time but also 0(n) space. Isn't it? So, Isn't the hashing approach much more better?
@takeUforward
@takeUforward 4 года назад
Please read the other comments, this has been answered. Also try to check the link in the description. You don’t need bucket, since at the end you are doing a xor... use two variables to keep the updated xor, that will help!
@abhinavprasun3515
@abhinavprasun3515 4 года назад
@@takeUforward Such a quick reply! Thank you bhaiya. Looking forward to the whole DSA series asap! 🙏
@SuperGilly123456789
@SuperGilly123456789 4 года назад
Understood. Love this series.
@riyanshpal4063
@riyanshpal4063 4 года назад
Bhaiya I have one more approch . time complexity- O(2n) space- O(1) but in my approach I am changing arr[i] to -arr[i]. will this be considered as good approach.
@abhijeetkumar2204
@abhijeetkumar2204 4 года назад
he already said about negation approach
@dhairyashiil
@dhairyashiil Год назад
Great Video 🔥
@tannukumari5144
@tannukumari5144 4 года назад
Just watched it, awesome explanation:)
@kainathussain6973
@kainathussain6973 2 года назад
Maintaing two variable for bucket 1 and bucket 2 is enough vector Solution::repeatedNumber(const vector &A) { int a=0; for(int i=0;i
@anjalirathore6427
@anjalirathore6427 2 года назад
Hello, Hashing, swapping at correct position and using xor . These are 3 approaches which is the 4th one? as u said in start of video if we tell all 4 solution
@utkarshbahukhandi3169
@utkarshbahukhandi3169 3 года назад
I have solved this using DFS by placing element into their respective positions and finding the repeating element and then simply traversing the array to find index not having correct element(missing number).
@AlbertoRodriguez-oe6jo
@AlbertoRodriguez-oe6jo 2 года назад
If modifying the input array is allowed, your method or swap sort is great approach, but this is not the case, as input array can't be modified, and creating a copy will take O(n) space.
@darshanupadhyay1989
@darshanupadhyay1989 4 года назад
An other approach is that we choose value and go to that index then minus it if it allready Minus then it's a repeat number and at last positive num is our missing num
@takeUforward
@takeUforward 4 года назад
Modifying the array is not a good option 😅
@bhaveshkumar6842
@bhaveshkumar6842 3 года назад
Thank you so much!
@shreyanaik6062
@shreyanaik6062 4 года назад
Understood sir...Thank you so much
@harshsheth6633
@harshsheth6633 4 года назад
Great series but can u plz tell me how to get most significant bit in c++. Because I am stuck at this point!!!
@takeUforward
@takeUforward 4 года назад
Google these stuffs, u will get to learn many things.
@harshsheth6633
@harshsheth6633 4 года назад
@@takeUforwardThank you!!
@ravishankar9247
@ravishankar9247 2 года назад
I am confused in the 4:16 because you took same variable name for sum of 'n' numbers and difference of X and Y.........:(
@sin78000000
@sin78000000 2 года назад
One of the best explanations I have ever seen. Keep up the good work
@rikeshtrieseverything
@rikeshtrieseverything Год назад
Why you are saying that missing number is in setbit bucket and repeating number is in unset bit bucket ? couldn't it be other way round ?
@paullater6230
@paullater6230 2 года назад
Superb explanation 👌 👏 👍
@adityamahimkar6138
@adityamahimkar6138 4 года назад
Ur explanation is nice and perfect 💯 but for me as a beginner mapping would be easy than xor approach 🤗
@takeUforward
@takeUforward 4 года назад
but in interview mapping will not get you selected!
@adityamahimkar6138
@adityamahimkar6138 4 года назад
@@takeUforward ohh thanks 😊
@it-series-music
@it-series-music Год назад
I did not understand, how did you write X-Y=S ? X-Y = Sum of n integers - sum of array isn't it
@rshishabhyadav7044
@rshishabhyadav7044 2 года назад
how to separate the set and unset bits means what to write as a code?
@beingvoid3246
@beingvoid3246 2 года назад
FInal Code (XOR approach) : class Solution{ public: int *findTwoElement(int *arr, int n) { int *a = new int[2]; int Xor = 0; for(int i = 0; i < n ; i ++){ Xor ^= (arr[i] ^ (i + 1)); } int temp = Xor , idx = 0; while(!(temp & 1)){ temp >>= 1; idx ++; } int basket_1 = 0 , basket_2 = 0; for(int i = 0 ; i < n ; i ++){ int curr = arr[i]; if((curr >> idx) & 1) basket_2 ^= arr[i]; else basket_1 ^= arr[i]; curr = i + 1; if((curr >> idx) & 1) basket_2 ^= (i + 1); else basket_1 ^= (i + 1); } bool found = false; for(int i = 0; i < n ; i ++){ if(basket_1 == arr[i]){ found = true; break; } } if(found){ a[0] = basket_1 , a[1] = basket_2; }else{ a[0] = basket_2 , a[1] = basket_1; } return a; } };
@vuyyurishanmukhaabhinavvar4096
@vuyyurishanmukhaabhinavvar4096 4 года назад
Loving it sir this DSA course
@chinmayraichur8984
@chinmayraichur8984 Год назад
Where is the explanation for the Brute force method at 1:04 ?
@nagame859
@nagame859 Год назад
XoR solution is dope as 🤩🤩🤩...
@syedsafdarali4287
@syedsafdarali4287 3 года назад
awsome explanation striver bro please keep these videos coming
@irrompible4588
@irrompible4588 3 года назад
is there any significance of taking the right most set bit for dividing in two buckets??..if possible reply. i mean if taking any other bit is going to create any error.?
@vivekswami2182
@vivekswami2182 2 года назад
Hey everyone! uhhhhh... this is striver here 🔥🔥
@amolgode9843
@amolgode9843 2 года назад
I use the Negative element approach but I also make array elements +ve again. int[] findTwoElement(int arr[], int n) { // code here int rep = -1; int miss = -1; for(int i = 0; i < n ; i++) { int abs_v = Math.abs(arr[i]); if(arr[abs_v - 1] > 0) { arr[abs_v -1] = -arr[abs_v -1]; }else { rep = abs_v; } } for(int i = 0 ; i < n ; i++) { if(arr[i] > 0) { miss = i+1; }else { arr[i] *= -1;//making positive again } } // System.out.println(Arrays.toString(arr)); return new int[]{rep,miss}; }
@misterbean6674
@misterbean6674 2 года назад
Why can't we use floyd's cycle algo to find x and then find y by xor1^x? This isn't working. Why so?
@kaustavbhattacharya618
@kaustavbhattacharya618 3 года назад
How could we found, if there is more than one number are missing. Could the index-based solution is the best choice.
Далее
Исповедь / Мася
2:47:10
Просмотров 183 тыс.
the TRUTH about C++ (is it worth your time?)
3:17
Просмотров 738 тыс.
I Solved 100 LeetCode Problems
13:11
Просмотров 169 тыс.
What is Competitive Programming?
6:26
Просмотров 2,5 млн
BS-9. Find Peak Element
32:53
Просмотров 190 тыс.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Исповедь / Мася
2:47:10
Просмотров 183 тыс.