Find missing number in an array(using summation and XOR operation)
Find the missing number in the array which contains a series of consecutive numbers in range from 1 to n. Use summation formula for natural numbers 1 to n. We can also use the XOR operation.
Пікірлер: 59
This is great, thanks! Another advantage of XOR is that there no possibility of overflow, as there is with summation. (One very minor mistake is at 4:55 you wrote "0 XOR a = 0".)
4:37 best way to understand XOR of n variables is that for odd number of ones(a in your case) the result is one(or a ) else it is 0. If numbers are not sorted and duplicate entries are also there. How we can find the missing number in linear time and constant space ?
always great and concise. thank you. I would like to see the algorithm implemented in c code.
Exactly what I needed. Thanks!
This is great video, exactly what i needed. Indian teachers are ❤️
If we have scanner class then how to get sum of number.
How to find multiple missing number in array ? like an array is= {4,3,5} so the missing numbers is = {1,6}
nice video! problem statement can be made a bit clearer: "find missing number in unsorted array", since for sorted array as in example input, it's too easy, if a[i+1] != a[i]+1 then return a[i]+1
you are put the add right time by the way thanks for nice explanation .............
Very clear, I am looking for implementation of algorithm using any programming language. Please help if possible, Thank you very much for good algorithm videos.
Love you brother. You explained so nicely. Thank you so much.
That makes a lot of sense. Thank you sir!
In video at (4:56), in XOR table 0 ^ a = a, while explaining 0 ^ a = 0. But I liked the explanation. It's too nicely explained.
Thank you so much for making this video, awesome.
Why we did xor of given numbers within same array .we can find the xor of given array and expected array. Can anyone explain me pls
Thanks a lot sir.. You are doing a great job
Very clear. I have a question, for your example case, could we just search from 1, to 2, to 3, until 5, when we look at 3,we know next one should be 4, and but it is actually 5; --- this assumes that array is sorted. will XOR algo work even when array is not sorted?
@vivekanandkhyade
7 жыл бұрын
yes XOR will work on unsorted array too..
@sivaganesh4489
4 жыл бұрын
Even 1st approach also work for unsorted array but it must be sequence for both methods
What if multiple numbers are missing
Sir, I have a doubt how can i ask you❓️ Pls rply
How to find multiple missing numbers in array?
Awesome Sir
very good explain, thx a lot!!
Love u bro.. awesome ❤️❤️❤️
Sir ,can you provide how to find third largest element in an array logic
@harshivam
Жыл бұрын
just store max and second max in a variable and like how you find max just use a if statement where you check the third ma != max1,max2
Asymptotically both gives O(n) But logically 2nd one will have to iterate 2 times. So first one must be efficient, logically.
@darshank3012
3 жыл бұрын
If the n value is high, the summation might cross the integer's max value limit, then the first approach might not work well.
Superb sir!!!!
but how to code this in xor
Thanks for the explanation :)
@vivekanandkhyade
7 жыл бұрын
welcome Odalys..!
0 XOR a = a. U wrote it as 0 XOR a = 0 by mistake right?
thank you sir...
sir, if you take 1 instead of ' a ' it is better for understand to us.
Aren't you assuming here that your array is sorted? What if it is not?
@arindam_.
5 жыл бұрын
So the assumption is that the array is sorted. If not, sort the array first and then start your operation.
Thank you a-lot.
Explanation s super but write full java code we can understand better
thanks sir
The quickest way is the binary search which is logn
I know this method Any other method??
thanks
Super sir
I don't see why the second solution is better than the first one in terms of time complexity.
@ramakantasamal7482
6 жыл бұрын
Agree on time complexity it does not help much but 1st one could cause overflow while summing
Both logic has O(n) time complexity. Why not just traverse and check if a[i] != a[i-1] + 1 from (i = 1 to n-1). When the condition is true, a[i-1] is the answer. This approach is far easier and has O(n) time complexity as well. We can also use binary search and get O(log n ) time complexity.
@jaybhatt6775
3 жыл бұрын
binary search has higher space complexity
@AnkitRaj-jn8ew
3 жыл бұрын
@@jaybhatt6775 Not necessarily. You can use an iterative approach for binary search to avoid stack trace. It won't need any extra space and will be more efficient than the 2 approaches discussed here.
nice
some problems in this video
give me reply
(n+1)*(n+2)/2 is actual formula, let say we have 7 is absent from range 1-9, with your formula (n*n+1)/2) 36 is coming and my array sum is 38 ,now result is 2, which is wrong.
@getItToDeepak
5 жыл бұрын
n = 9, so 9*10/2 = 45, sum of numbers = 38. Now 45-38 = 7. You got the answer
@bryand7958
5 жыл бұрын
(n * n + 1) /2 is not the formula n * (n + 1) / 2 ==> (n^2 + n)/2
1 2 3 4…… 5 6 7 8 Ms in my bank account
thank you sir,