Find missing number in an array

Sdílet
Vložit
  • čas přidán 25. 06. 2019
  • This video shows three techniques on how to find the missing number in an array. The techniques are based on hashing, sum formula and XOR. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)

Komentáře • 139

  • @deepjyotidebnath4122
    @deepjyotidebnath4122 Před 2 lety +10

    Great video ! 🙌 I understand the XOR operation by this video.

  • @dikshakatake5739
    @dikshakatake5739 Před 4 lety +11

    Great video ! 🙌 No need to research the best approach just watch your videos and you are good to go ! Thanks🙌🙌

    • @techdose4u
      @techdose4u  Před 4 lety +2

      Welcome :)

    • @unknow2096
      @unknow2096 Před 3 lety +1

      @@techdose4u How to find more then one missing element???

    • @akashkewar
      @akashkewar Před rokem

      @@unknow2096 one simple approach could be, create a set containing (0-n) elements, loop via that list and remove the element from the set. The elements remaining in the set are your missing elements.

  • @nitin-7870
    @nitin-7870 Před rokem

    that was just amazing, before this video i was doing like sorting all the element in the array and finding all the element whether if it is at correct index for not

  • @khushichaurasia7599
    @khushichaurasia7599 Před 3 lety +7

    Your explaining level ....😍😍😍😍

  • @aaditpalande7478
    @aaditpalande7478 Před rokem +3

    Thank you sir , Just one question that how can I build my mind to think for the solution in such various approaches

  • @killeraloo3247
    @killeraloo3247 Před 9 měsíci

    mast vide thi, sare methods samajh aa gye. Thanq.

  • @divakarkumar1461
    @divakarkumar1461 Před 3 lety +3

    Sort the array then
    For(int i=0;i

    • @sarvesh545
      @sarvesh545 Před 10 dny

      what if the range is random means like 55 to 78

  • @iamnimish
    @iamnimish Před rokem +1

    XORing all the elements & then XORing the numbers till n.
    And then XORing both tof the obtained results.
    This brings us the left out element as XOR of a no with itself is zero(like in binary).

  • @Cloud-577
    @Cloud-577 Před 2 lety

    note that xor of any number repeats every four digits. so you ca use mod to predetermine the xor of Len(nums). then to find the missing number find the xor of the nums values ONLY. finally return the xor of Len(nums).^ xor of the nums values

  • @creativeMinds1603
    @creativeMinds1603 Před 27 dny

    Great explanation 😃

  • @meruguharesh4027
    @meruguharesh4027 Před 2 lety

    nice explanation sir

  • @Sat-Kabir-Saheb
    @Sat-Kabir-Saheb Před 4 lety +14

    If the given array is sorted , then binary search can be applied ( logn) time

    • @techdose4u
      @techdose4u  Před 4 lety +7

      Yes you are correct, but only if the array is sorted, while XOR works in all cases :)

  • @aayushverma1264
    @aayushverma1264 Před 4 lety +2

    We can also try subtracting method. if (arr[i] - arr[i-1] == 2) then missing number = arr[i] - 1. Time complexity is O(n) and Space Complexity is O(1).

    • @TedAsiamah
      @TedAsiamah Před 4 lety +6

      This wont work for an unsorted array.

    • @rickseth9207
      @rickseth9207 Před 11 měsíci

      tried this but the problem is if the element missing is after the first or before the first place then problem arises

  • @surajtopal9940
    @surajtopal9940 Před 2 lety +2

    XOR one is really cool . I tried with -negative value and array is not sorted still working perfect . Thank a lot sir ji

    • @har.preet.singh.
      @har.preet.singh. Před 2 lety +2

      Hey brother! Can you please share the code, if possible? It'll be very nice of you, if you can.

    • @surajtopal9940
      @surajtopal9940 Před 2 lety

      @@har.preet.singh. sorry brother , you are late. Actually I practice in online compiler we cannot save program .

    • @surajtopal9940
      @surajtopal9940 Před 2 lety

      try yourself brother , if you face any difficulty send your code here then i will help you .

    • @har.preet.singh.
      @har.preet.singh. Před 2 lety

      @@surajtopal9940 Here's my python code which isn't working.
      def func(N,A):
      new=[]
      for n in range(max(A)+1):
      new.append(-1)
      for i in range(N):
      if A[i]>=0:
      new[A[i]]=True
      for j in range(1,max(A)+1):
      if new[j]==True:
      return j+1
      break
      L=[]
      N=int(input('Enter the number of elements: '))
      print('Now, enter the',N, 'elements:')
      for n in range(N):
      L.append(int(input()))
      print(func(N,L))

    • @surajtopal9940
      @surajtopal9940 Před 2 lety

      @@har.preet.singh. brother this is different approach use XOR operation. It is very easy. You just to XOR all the value of the array one by one and put it in the result variable after than you need to do the loop till n and XOR all in the result1 then result ^ result1 and you will get your answer.

  • @atiquesiddiqui710
    @atiquesiddiqui710 Před 3 lety +2

    Very very nice content ❤️
    Just sir please us the explanation of pseudo code if you can

    • @techdose4u
      @techdose4u  Před 3 lety +1

      Yea I am explaining it from past some time.

    • @atiquesiddiqui710
      @atiquesiddiqui710 Před 3 lety

      TECH DOSE sir you are doing a great Work I haven’t found a channel like you on CZcams honestly sir very very nice 👍🏻 content and to the point 🔥

    • @atiquesiddiqui710
      @atiquesiddiqui710 Před 3 lety

      TECH DOSE sir plzz add a video on number of pairs in an array it would be great 👍🏻

  • @anuragsoni2256
    @anuragsoni2256 Před 3 lety +1

    Thanks! for the video

  • @oldguardkiller8408
    @oldguardkiller8408 Před rokem +1

    We can Also Use Cyle Sort

  • @shivamkumar-hu3bv
    @shivamkumar-hu3bv Před 2 lety +2

    From where n=5 comes ??

  • @rajukumar-sv3vj
    @rajukumar-sv3vj Před 9 měsíci

    sir very good explain thankyou sir ji

  • @IrenJahan-ru5tx
    @IrenJahan-ru5tx Před rokem

    Good video

  • @rashibansal7357
    @rashibansal7357 Před 4 lety +3

    what if we sort the array and then start comparing if each element is equal to its index, wherever the condition is a false return that index value.

  • @vishalbhatane1608
    @vishalbhatane1608 Před 3 lety

    Thanks

  • @reenabeeton9856
    @reenabeeton9856 Před 3 lety +3

    Write a Swift program to check if one of the first 4 elements in a given array of integers is a 7. The length of the array may be less than 4. PLS MAKE VIDEO ON IT

  • @dineshverma-in7on
    @dineshverma-in7on Před 2 lety +1

    use binary search, time complexity will be logN

  • @ashuiet
    @ashuiet Před 4 lety

    For input int[] a = {-1, 1, 2, 3, 4, 5, 6, 7} missing Nums : 18 but it should be '0' can you suggest why I am getting .because as per the algo explain by instructor it should be pass for boundary value also :
    public class MissingNumber2 {
    private static int missingNumber(int[] input, int n) {
    int x1 = input[0];
    int x2 = 1;
    for (int i = 1; i < n; i++) {
    x1 = x1 ^ input[i];
    }
    for (int i = 2; i

  • @mujahidpasha4440
    @mujahidpasha4440 Před 4 lety +1

    Hi, have you ever come across below question?
    Given a stream of timestamp and CPU utilisations we need to return the time interval where CPU utilisations was max. Ex input 2d array. [[StartTime, EndTime, cpuUtilization]]

    • @mujahidpasha4440
      @mujahidpasha4440 Před 4 lety

      That's the only information I have about the question... What would be your approach to solve this?

  • @audioplatform6299
    @audioplatform6299 Před 3 lety +1

    nice solution!

  • @cb559
    @cb559 Před 4 lety +2

    1 and 2 are impratical solution it wont work If negative number is present or missing number not in between. see problem 41 leet code

  • @kartikbhanderi9210
    @kartikbhanderi9210 Před 4 lety +5

    n*(n+1)/2 - sum of array elements
    Will also work 😄🍻

    • @techdose4u
      @techdose4u  Před 4 lety +1

      True. But still O(N) 😁

    • @rakibulislam2452
      @rakibulislam2452 Před 4 lety

      @@techdose4u in python no issue with integer overflow dude

    • @JohnLee-xl2ut
      @JohnLee-xl2ut Před 3 lety +1

      Is it n because it is the biggest number in the array?

    • @kartikbhanderi9210
      @kartikbhanderi9210 Před 3 lety +1

      @@JohnLee-xl2ut it's the formula of arithmetic progression , where we are taking 'n' as last term...

    • @JohnLee-xl2ut
      @JohnLee-xl2ut Před 3 lety

      @Kartik Bhanderi thanks mate i taught it was always the biggest number. But it is the last number in an array thanks mate :)

  • @kommunagasheshu3601
    @kommunagasheshu3601 Před rokem +1

    As well as please explain code for the problem.

  • @suhailsiddiqui840
    @suhailsiddiqui840 Před 2 lety

    I think this one is more efficient, in very few cases you have to iterate over whole array. If array is in order.
    for(int i=0;i

    • @akashkewar
      @akashkewar Před rokem

      The BIG O complexity is still O(N).

  • @natnaelghirma2617
    @natnaelghirma2617 Před 2 lety +3

    Sir, what about the time it takes to make the second array(the complete one)?

    • @akashkewar
      @akashkewar Před rokem

      You can compute second xor while looping via first array, so it is just O(1* N). You don't really create the second array.

  • @sarvesh545
    @sarvesh545 Před 10 dny

    what if there are more than one element is missing in an unsorted array

  • @dovyraj1272
    @dovyraj1272 Před 4 lety +2

    Can v use binary search after sorting

    • @techdose4u
      @techdose4u  Před 4 lety

      You can, if you can related your index with array values.

  • @har.preet.singh.
    @har.preet.singh. Před 2 lety +2

    Can you please share the code of the third approach?

  • @FANSasFRIENDS
    @FANSasFRIENDS Před 2 lety +1

    Sir, kindly explain what is overflow for this case.

    • @yaswanthp2294
      @yaswanthp2294 Před rokem

      Overflow occurs when we sum large numbers.
      In java integer has some range. If sum goes beyond range, it will cause overflow and doesn't give proper results.

  • @yaswanthp2294
    @yaswanthp2294 Před rokem

    Lovely

  • @bkmeher9005
    @bkmeher9005 Před rokem

    great

  • @shubhamnehulkar135
    @shubhamnehulkar135 Před 4 lety +2

    what if i have multiple missing numbers??

    • @techdose4u
      @techdose4u  Před 4 lety +2

      So that will be a totally different question.

  • @ravikamble8142
    @ravikamble8142 Před 4 lety

    in case 1,2,3,4,5 how to find? i am getting 0 .

  • @rajankhunt7002
    @rajankhunt7002 Před 2 lety

    GOOD BLOG

  • @jaydalsaniya6986
    @jaydalsaniya6986 Před 4 lety +1

    What if I do XOR operation between sets in which a={1,2,3} and b={1,2,3,4,5} do i get the answer as {4,5} ???

    • @techdose4u
      @techdose4u  Před 4 lety +3

      No. You will get your answer = (4 XOR 5). You won't get 4,5 as individual answers and so you will never know about it. If you have only one number missing and you knew the original array then only you can get your answer as missing element. Here, in your example you have 2 missing numbers and so you won't get the missing elements. I hope you got it :)

  • @sanjanasain1999
    @sanjanasain1999 Před 2 lety

    Your voice is same as apna college channel...

  • @vinaykumar-sg7xd
    @vinaykumar-sg7xd Před 4 lety +1

    why u initialized i=2 in second loop

    • @techdose4u
      @techdose4u  Před 4 lety

      Please mention the time while asking question so as to make it easier to refer.

    • @vinaykumar-sg7xd
      @vinaykumar-sg7xd Před 4 lety

      @@techdose4u in the xor code you provided in the comments

  • @rohankumarshah5679
    @rohankumarshah5679 Před 2 lety

    brute better optimal -------> awesome.

  • @nishchalbhardwaj4835
    @nishchalbhardwaj4835 Před 4 lety +1

    the sum method won't work if there is a zero in the array

    • @techdose4u
      @techdose4u  Před 4 lety

      The sum technique works for number starting from 1 probably. But for contiguous array, you can use the XOR method which works.

    • @pandeyshubham1
      @pandeyshubham1 Před 3 lety

      No it works for zero as well. My code got accepted using sum method in leetcode.

  • @nirajjain8099
    @nirajjain8099 Před 2 lety

    Sir can you explain the code for XOR method ??

  • @nitinmahajan3320
    @nitinmahajan3320 Před 3 lety

    Can’t we just: 1. initialise counter to array’s zero-th element. 2. Iterate through array increment this counter, wherever the counter and array element does not match - is the answer.

    • @CodeSuccessChronicle
      @CodeSuccessChronicle Před 3 lety

      the array may not be in sorted form to be able to have a match with value and it's index probably.

  • @praveenperumalsamy8005
    @praveenperumalsamy8005 Před 3 lety +1

    Method 2 sum formula not work for all
    As if 1, 2,5 now answer must be 3,4 but using tis formula we get 7 wrong

  • @surendharv795
    @surendharv795 Před 5 měsíci

    But the third approach will not work of array elements are repeated.

  • @JohnLee-xl2ut
    @JohnLee-xl2ut Před 3 lety

    How did the n become 5? is it because it is the biggest?

    • @abhijeetbhowmik2264
      @abhijeetbhowmik2264 Před 3 lety

      Isn't this confusing? Array is unsorted. So there is extra complexity to search for largest number. Only then method second can be used.

    • @abhijeetbhowmik2264
      @abhijeetbhowmik2264 Před 3 lety

      Also if a sorted array is given , simply missing number is a[i] + 1 where a[i+1] - a[i] != 1

  • @anujtiwari6842
    @anujtiwari6842 Před 11 měsíci

    can you pls give its source code.. it would be really helpful

  • @jatinahuja6929
    @jatinahuja6929 Před 3 lety +1

    Number of terms toh aapne 4 le rkhi hai likha 5 hai

  • @unknow2096
    @unknow2096 Před 3 lety +1

    How to find more then one missing element???

    • @techdose4u
      @techdose4u  Před 3 lety

      Try map

    • @unknow2096
      @unknow2096 Před 3 lety

      @@techdose4u what do you mean

    • @techdose4u
      @techdose4u  Před 3 lety

      @@unknow2096 you can keep a hashmap to keep track of what all elements you saw. Rest all in the range of 1 to N will be missing.

  • @probot1436
    @probot1436 Před 11 měsíci

    New 2 Method
    1.Match the array position no OR
    2.find Even or Odd no

  • @shaikhzaki3358
    @shaikhzaki3358 Před 9 měsíci

    If the array doesn't contain any missing than

  • @MAHTABKHAN-vx9xq
    @MAHTABKHAN-vx9xq Před 2 lety

    There are lot of numbers in a list
    All of them are whole numbers below 100
    They are in not any order
    Just random whole numbers below 100
    We have to find list of numbers which are missing in the series?
    Anyone solve it ?

  • @souravgorai9481
    @souravgorai9481 Před 4 lety +5

    wont work if there are multiple missing number. Eg.-> i/p- 2 3 1 6 o/p=4.
    This example wont work

  • @InvincibleMan99
    @InvincibleMan99 Před 2 měsíci

    What about -ve numbers
    [-1, -3] should return 1

    • @sarvesh545
      @sarvesh545 Před 10 dny

      then we can use the 2nd method!

  • @neerajsinghdhakar9554
    @neerajsinghdhakar9554 Před 4 lety +1

    can u please share the xor source code

    • @techdose4u
      @techdose4u  Před 4 lety +2

      // Function to get the missing number
      int getMissingNo(int a[], int n)
      {
      // For xor of all the elements in array
      int x1 = a[0];
      // For xor of all the elements from 1 to n+1
      int x2 = 1;
      for (int i = 1; i < n; i++)
      x1 = x1 ^ a[i];
      for (int i = 2; i

    • @vinaykumar-sg7xd
      @vinaykumar-sg7xd Před 4 lety +1

      @@techdose4u why u initialized i =2 in second loop

    • @arif3012
      @arif3012 Před 4 lety +1

      public static int getMissingNo(int arr[] , int n)
      {
      int a=1,b=1;
      for(int i=0;i

    • @VishalPatel_imvishal
      @VishalPatel_imvishal Před 3 lety +2

      @@vinaykumar-sg7xd because second loop is for XOR elements from 1 to n. x2 is initialized with 1 so if we start with 1 in second loop then it will become 0 in XOR process. so to eliminate that issue, x2 is initialized with 1 and loop started from 2

  • @waseemakramshaikh3638

    Methode 2 won't work if 0 present in array

  • @techyshivang2634
    @techyshivang2634 Před 4 lety

    2nd method also not work for negative number

    • @akarunx
      @akarunx Před 2 lety

      The problem statement is supposed to be "Find the missing number form an array containing n distinct numbers in the range [0, n]" , Good one from Tech Dose.

  • @talibqureshi6368
    @talibqureshi6368 Před 4 lety +1

    What if we have multiple numbers missing
    Ex - [3,4,5,7,8,10,11]
    How to find first missing number?
    Please Help

    • @chirantanacharyya822
      @chirantanacharyya822 Před 3 lety

      traverse through the array and check the difference between two consecutive numbers

    • @yaswanthp2294
      @yaswanthp2294 Před rokem

      Just use first approach as shown in video.
      Xor will not work in this case

  • @abhaymanoharsaxena1174

    in second method the n=4 not 5

  • @zeeu
    @zeeu Před 2 lety +1

    will help if the array is in order
    function missingNum(arr) {
    for(i=arr.length-1;i>=0;i--) {
    if(arr[i] - arr[i-1] == 2) {
    return arr[i]
    }
    }
    return -1;
    }

    • @ZohaibKhan-ug5zg
      @ZohaibKhan-ug5zg Před rokem

      Why you are returning the arr[i] ?this will not give us missing number in array for example 1,2,4 and there is 3 missing number then how we get 3 number by returning the arr[i]

    • @ZohaibKhan-ug5zg
      @ZohaibKhan-ug5zg Před rokem

      For correct answer we have to return arr[i]-1 rather than returning the a[i]

  • @red_app1418
    @red_app1418 Před 3 lety

    find missing number in arere

  • @VishalPatel_imvishal
    @VishalPatel_imvishal Před 3 lety

    Java code: ide.geeksforgeeks.org/6tkkn1JzX0

  • @UmeshKumar-zc4zc
    @UmeshKumar-zc4zc Před 3 lety

    None of the method will work in case of duplicates method and when numbers are randomly arranged.

    • @techdose4u
      @techdose4u  Před 3 lety

      This was a simple version where question did not had duplicates.

    • @UmeshKumar-zc4zc
      @UmeshKumar-zc4zc Před 3 lety +2

      Then you should add that too.
      I mean you must specify it's limitations as you are explaining three methods.
      Anyway that's good at all.
      Thanks to you.

    • @techdose4u
      @techdose4u  Před 3 lety +1

      Sure. I will include other version as well.

  • @gajsinghrathore8800
    @gajsinghrathore8800 Před 10 měsíci

    i found out better approach T.C->O(LOGN) (IF ARRAY IS SORTED) WE CAN USE BINARY SEARCH
    int s = 0;
    int e = N - 1;

    while (s mid + 1 || arr[mid]

  • @abhishekrawat664
    @abhishekrawat664 Před rokem

    Yrr 7 8 9 11 12 ke liye work nahi karega :)

  • @ravikamble8142
    @ravikamble8142 Před 4 lety +2

    sir code?

    • @techdose4u
      @techdose4u  Před 4 lety

      Please find it on geeks. I am currently uploading CODE for all new videos.

  • @satyaprakash4598
    @satyaprakash4598 Před 3 lety +1

    Your two approach not working.
    Ex :- [6,7,8,9]

    • @sagarlandge3271
      @sagarlandge3271 Před 3 lety +1

      Your input is wrong
      It should contain all the Integers from 1 to n except one missing no
      Eg [1,2,3,4,5,7,8,9]