Find missing number in an array(using summation and XOR operation)

แชร์
ฝัง
  • เผยแพร่เมื่อ 15 ต.ค. 2024
  • 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.

ความคิดเห็น • 60

  • @LarryRuane
    @LarryRuane 7 ปีที่แล้ว +25

    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".)

  • @abhijayrajvansh
    @abhijayrajvansh 3 ปีที่แล้ว +1

    This is great video, exactly what i needed. Indian teachers are ❤️

  • @naboulsikhalid7763
    @naboulsikhalid7763 6 หลายเดือนก่อน

    always great and concise. thank you. I would like to see the algorithm implemented in c code.

  • @lojian
    @lojian 7 ปีที่แล้ว +2

    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
      @vivekanandkhyade  7 ปีที่แล้ว +4

      yes XOR will work on unsorted array too..

    • @sivaganesh4489
      @sivaganesh4489 4 ปีที่แล้ว

      Even 1st approach also work for unsorted array but it must be sequence for both methods

  • @sanapalavenkatesh5273
    @sanapalavenkatesh5273 3 ปีที่แล้ว

    you are put the add right time by the way thanks for nice explanation .............

  • @PradeepSingh-vm1gl
    @PradeepSingh-vm1gl 3 ปีที่แล้ว

    Love you brother. You explained so nicely. Thank you so much.

  • @nasirfshah
    @nasirfshah 5 ปีที่แล้ว

    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 ?

  • @sakshimittal1166
    @sakshimittal1166 2 ปีที่แล้ว

    That makes a lot of sense. Thank you sir!

  • @kirtivardhan80
    @kirtivardhan80 6 ปีที่แล้ว +2

    Asymptotically both gives O(n)
    But logically 2nd one will have to iterate 2 times.
    So first one must be efficient, logically.

    • @darshank3012
      @darshank3012 4 ปีที่แล้ว

      If the n value is high, the summation might cross the integer's max value limit, then the first approach might not work well.

  • @manishswarnakar6993
    @manishswarnakar6993 6 ปีที่แล้ว +2

    How to find multiple missing number in array ?
    like an array is= {4,3,5}
    so the missing numbers is = {1,6}

  • @lodashnotebook5390
    @lodashnotebook5390 5 ปีที่แล้ว +1

    Exactly what I needed. Thanks!

  • @skkar2k2
    @skkar2k2 4 ปีที่แล้ว

    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.

  • @AnkitRaj-jn8ew
    @AnkitRaj-jn8ew 4 ปีที่แล้ว

    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
      @jaybhatt6775 3 ปีที่แล้ว

      binary search has higher space complexity

    • @AnkitRaj-jn8ew
      @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.

  • @Blingblingblingling
    @Blingblingblingling 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

  • @ibrahimkhaleel2822
    @ibrahimkhaleel2822 ปีที่แล้ว

    Sir,
    I have a doubt how can i ask you❓️
    Pls rply

  • @maheshwars.j6195
    @maheshwars.j6195 5 ปีที่แล้ว +3

    What if multiple numbers are missing

  • @durgabhavani9955
    @durgabhavani9955 5 ปีที่แล้ว +1

    Sir ,can you provide how to find third largest element in an array logic

    • @harshivam
      @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

  • @tapanjeetroy8266
    @tapanjeetroy8266 5 ปีที่แล้ว

    Thanks a lot sir.. You are doing a great job

  • @mangeshkumargabhane4671
    @mangeshkumargabhane4671 6 ปีที่แล้ว

    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.

  • @indianmusicsongssaregama1406
    @indianmusicsongssaregama1406 7 ปีที่แล้ว

    If we have scanner class then how to get sum of number.

  • @Copernicus_Song
    @Copernicus_Song 4 ปีที่แล้ว

    very good explain, thx a lot!!

  • @aaatatataapaatuuuu987
    @aaatatataapaatuuuu987 5 ปีที่แล้ว

    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

  • @nagesh007
    @nagesh007 8 หลายเดือนก่อน

    Awesome Sir

  • @mustapharaimilawal8053
    @mustapharaimilawal8053 5 ปีที่แล้ว

    Thank you so much for making this video, awesome.

  • @karan6551
    @karan6551 3 ปีที่แล้ว

    Love u bro.. awesome ❤️❤️❤️

  • @mohamedmusaraf675
    @mohamedmusaraf675 4 ปีที่แล้ว

    Superb sir!!!!

  • @kumarvivek02
    @kumarvivek02 6 ปีที่แล้ว

    Aren't you assuming here that your array is sorted? What if it is not?

    • @arindam_.
      @arindam_. 6 ปีที่แล้ว

      So the assumption is that the array is sorted. If not, sort the array first and then start your operation.

  • @rohitdas6005
    @rohitdas6005 5 ปีที่แล้ว

    How to find multiple missing numbers in array?

  • @vickyjain3403
    @vickyjain3403 5 ปีที่แล้ว

    sir, if you take 1 instead of ' a ' it is better for understand to us.

  • @akashgowda6923
    @akashgowda6923 4 ปีที่แล้ว

    Explanation s super but write full java code we can understand better

  • @salmankhatik4985
    @salmankhatik4985 2 ปีที่แล้ว

    but how to code this in xor

  • @NoorAli-uh4uq
    @NoorAli-uh4uq 4 ปีที่แล้ว

    Thank you a-lot.

  • @odalyspaz959
    @odalyspaz959 7 ปีที่แล้ว

    Thanks for the explanation :)

  • @cengizandak4241
    @cengizandak4241 5 ปีที่แล้ว

    The quickest way is the binary search which is logn

  • @vikrammehta5874
    @vikrammehta5874 4 ปีที่แล้ว

    I know this method
    Any other method??

  • @adarshnair9846
    @adarshnair9846 6 ปีที่แล้ว +2

    0 XOR a = a. U wrote it as 0 XOR a = 0 by mistake right?

  • @SonuGupta-wj6dg
    @SonuGupta-wj6dg 2 ปีที่แล้ว

    nice

  • @columbiars
    @columbiars 7 ปีที่แล้ว

    I don't see why the second solution is better than the first one in terms of time complexity.

    • @ramakantasamal7482
      @ramakantasamal7482 7 ปีที่แล้ว

      Agree on time complexity it does not help much but 1st one could cause overflow while summing

  • @sachiiinnnn9734
    @sachiiinnnn9734 3 ปีที่แล้ว

    thanks

  • @kumargourav4976
    @kumargourav4976 6 ปีที่แล้ว

    thank you sir,

  • @santhosh........
    @santhosh........ 5 ปีที่แล้ว

    Super sir

  • @fuadhasan0362
    @fuadhasan0362 6 ปีที่แล้ว

    thank you sir...

  • @techdiscussion3617
    @techdiscussion3617 6 ปีที่แล้ว

    thanks sir

  • @shubhamsunny6024
    @shubhamsunny6024 5 ปีที่แล้ว

    (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
      @getItToDeepak 5 ปีที่แล้ว

      n = 9, so 9*10/2 = 45, sum of numbers = 38. Now 45-38 = 7. You got the answer

    • @bryand7958
      @bryand7958 5 ปีที่แล้ว

      (n * n + 1) /2 is not the formula
      n * (n + 1) / 2 ==> (n^2 + n)/2

  • @nileshandakshat3713
    @nileshandakshat3713 5 ปีที่แล้ว

    some problems in this video

  • @sahjaykumar
    @sahjaykumar หลายเดือนก่อน

    Bro you learn communication. Tumhe thoda speaking course karna chahiye.. itna slow bol raha hai.

  • @nileshandakshat3713
    @nileshandakshat3713 5 ปีที่แล้ว

    give me reply

  • @anerdwitdacamera204
    @anerdwitdacamera204 6 ปีที่แล้ว

    1 2 3 4…… 5 6 7 8 Ms in my bank account