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

Поделиться
HTML-код
  • Опубликовано: 9 окт 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".)

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

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

  • @abhijayrajvansh
    @abhijayrajvansh 3 года назад +1

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

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

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

  • @PradeepSingh-vm1gl
    @PradeepSingh-vm1gl 3 года назад

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

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

  • @sanapalavenkatesh5273
    @sanapalavenkatesh5273 3 года назад

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

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

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

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

  • @lodashnotebook5390
    @lodashnotebook5390 5 лет назад +1

    Exactly what I needed. Thanks!

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

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

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

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

  • @mohamedmusaraf675
    @mohamedmusaraf675 4 года назад

    Superb sir!!!!

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

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

    Awesome Sir

  • @tapanjeetroy8266
    @tapanjeetroy8266 5 лет назад

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

  • @karan6551
    @karan6551 3 года назад

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

  • @adarshnair9846
    @adarshnair9846 6 лет назад +2

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

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

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

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

  • @mustapharaimilawal8053
    @mustapharaimilawal8053 5 лет назад

    Thank you so much for making this video, awesome.

  • @cengizandak4241
    @cengizandak4241 5 лет назад

    The quickest way is the binary search which is logn

  • @indianmusicsongssaregama1406
    @indianmusicsongssaregama1406 7 лет назад

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

  • @rohitdas6005
    @rohitdas6005 5 лет назад

    How to find multiple missing numbers in array?

  • @NoorAli-uh4uq
    @NoorAli-uh4uq 4 года назад

    Thank you a-lot.

  • @odalyspaz959
    @odalyspaz959 7 лет назад

    Thanks for the explanation :)

  • @SonuGupta-wj6dg
    @SonuGupta-wj6dg 2 года назад

    nice

  • @santhosh........
    @santhosh........ 5 лет назад

    Super sir

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

  • @vikrammehta5874
    @vikrammehta5874 4 года назад

    I know this method
    Any other method??

  • @fuadhasan0362
    @fuadhasan0362 6 лет назад

    thank you sir...

  • @kumargourav4976
    @kumargourav4976 6 лет назад

    thank you sir,

  • @techdiscussion3617
    @techdiscussion3617 6 лет назад

    thanks sir

  • @salmankhatik4985
    @salmankhatik4985 2 года назад

    but how to code this in xor

  • @sachiiinnnn9734
    @sachiiinnnn9734 3 года назад

    thanks

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

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

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

  • @nileshandakshat3713
    @nileshandakshat3713 5 лет назад

    some problems in this video

  • @anerdwitdacamera204
    @anerdwitdacamera204 6 лет назад

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

  • @nileshandakshat3713
    @nileshandakshat3713 5 лет назад

    give me reply