Find missing number in an array

แชร์
ฝัง
  • เผยแพร่เมื่อ 25 มิ.ย. 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 :)

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

  • @deepjyotidebnath4122
    @deepjyotidebnath4122 3 ปีที่แล้ว +10

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

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

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

  • @nitin-7870
    @nitin-7870 ปีที่แล้ว

    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

  • @dikshakatake5739
    @dikshakatake5739 4 ปีที่แล้ว +11

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      Welcome :)

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

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

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

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

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

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

  • @shivamkumar-hu3bv
    @shivamkumar-hu3bv 2 ปีที่แล้ว +2

    From where n=5 comes ??

  • @Cloud-577
    @Cloud-577 2 ปีที่แล้ว

    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

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

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

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

      what if the range is random means like 55 to 78

  • @iamnimish
    @iamnimish ปีที่แล้ว +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).

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

    Great explanation 😃

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

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

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

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

  • @khushichaurasia7599
    @khushichaurasia7599 3 ปีที่แล้ว +7

    Your explaining level ....😍😍😍😍

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

    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

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

    nice explanation sir

  • @Firstusee256
    @Firstusee256 4 ปีที่แล้ว +14

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +7

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

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

    • @f2s654
      @f2s654 4 ปีที่แล้ว +2

      sorting complexity nlogn we have to do it in N :P

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      😂

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

      Simply try XOR

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

      Yes

  • @rajukumar-sv3vj
    @rajukumar-sv3vj 10 หลายเดือนก่อน

    sir very good explain thankyou sir ji

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

    As well as please explain code for the problem.

  • @dovyraj1272
    @dovyraj1272 4 ปีที่แล้ว +2

    Can v use binary search after sorting

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

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

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

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

    We can Also Use Cyle Sort

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

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

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

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

  • @FANSasFRIENDS
    @FANSasFRIENDS 2 ปีที่แล้ว +1

    Sir, kindly explain what is overflow for this case.

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

      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.

  • @har.preet.singh.
    @har.preet.singh. 2 ปีที่แล้ว +2

    Can you please share the code of the third approach?

  • @shubhamnehulkar135
    @shubhamnehulkar135 4 ปีที่แล้ว +2

    what if i have multiple missing numbers??

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      So that will be a totally different question.

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

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

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

      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.

  • @dineshverma-in7on
    @dineshverma-in7on 2 ปีที่แล้ว +1

    use binary search, time complexity will be logN

  • @jaydalsaniya6986
    @jaydalsaniya6986 4 ปีที่แล้ว +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  4 ปีที่แล้ว +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 :)

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

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

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

    Thanks! for the video

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

      Welcome :)

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

      @@techdose4u How to do this questions in swift language.

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

      This wont work for an unsorted array.

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

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

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

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

    Sir can you explain the code for XOR method ??

  • @vinaykumar-sg7xd
    @vinaykumar-sg7xd 4 ปีที่แล้ว +1

    why u initialized i=2 in second loop

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

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

    • @vinaykumar-sg7xd
      @vinaykumar-sg7xd 4 ปีที่แล้ว

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

  • @IrenJahan-ru5tx
    @IrenJahan-ru5tx ปีที่แล้ว

    Good video

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

    Thanks

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

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

  • @atiquesiddiqui710
    @atiquesiddiqui710 4 ปีที่แล้ว +2

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Yea I am explaining it from past some time.

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

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

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

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

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

    nice solution!

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

    Methode 2 won't work if 0 present in array

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

  • @JohnLee-xl2ut
    @JohnLee-xl2ut 4 ปีที่แล้ว

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

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

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

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

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

    Your voice is same as apna college channel...

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

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

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

      then we can use the 2nd method!

  • @kartikbhanderi9210
    @kartikbhanderi9210 4 ปีที่แล้ว +5

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

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      True. But still O(N) 😁

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

      @@techdose4u in python no issue with integer overflow dude

    • @JohnLee-xl2ut
      @JohnLee-xl2ut 4 ปีที่แล้ว +1

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

    • @kartikbhanderi9210
      @kartikbhanderi9210 4 ปีที่แล้ว +1

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

    • @JohnLee-xl2ut
      @JohnLee-xl2ut 4 ปีที่แล้ว

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

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

    2nd method also not work for negative number

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

      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.

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

    great

  • @surajtopal9940
    @surajtopal9940 3 ปีที่แล้ว +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. 2 ปีที่แล้ว +2

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

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

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

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

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

    • @har.preet.singh.
      @har.preet.singh. 2 ปีที่แล้ว

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

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

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

    Lovely

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

    If the array doesn't contain any missing than

  • @MAHTABKHAN-vx9xq
    @MAHTABKHAN-vx9xq 2 ปีที่แล้ว

    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 ?

  • @nishchalbhardwaj4835
    @nishchalbhardwaj4835 4 ปีที่แล้ว +1

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

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

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

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

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

  • @neerajsinghdhakar9554
    @neerajsinghdhakar9554 4 ปีที่แล้ว +1

    can u please share the xor source code

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

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

    • @arif3012
      @arif3012 4 ปีที่แล้ว +1

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

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

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

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

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

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

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

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

    GOOD BLOG

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

    in second method the n=4 not 5

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

    How to find more then one missing element???

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

      Try map

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

      @@techdose4u what do you mean

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

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

  • @souravgorai9481
    @souravgorai9481 4 ปีที่แล้ว +5

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

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

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

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

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

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

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

      The BIG O complexity is still O(N).

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

    Java code: ide.geeksforgeeks.org/6tkkn1JzX0

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

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

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

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

  • @UmeshKumar-zc4zc
    @UmeshKumar-zc4zc 3 ปีที่แล้ว

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

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

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

    • @UmeshKumar-zc4zc
      @UmeshKumar-zc4zc 3 ปีที่แล้ว +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  3 ปีที่แล้ว +1

      Sure. I will include other version as well.

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

    find missing number in arere

  • @zeeu
    @zeeu 2 ปีที่แล้ว +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 2 ปีที่แล้ว

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

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

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

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

    • @sagarlandge3271
      @sagarlandge3271 3 ปีที่แล้ว +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]

  • @ravikamble8142
    @ravikamble8142 4 ปีที่แล้ว +2

    sir code?

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

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

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

    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]