Find missing number in an array

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

Комментарии • 143

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

  • @divakarkumar1461
    @divakarkumar1461 3 года назад +3

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

    • @ffproplayershere
      @ffproplayershere 3 месяца назад

      what if the range is random means like 55 to 78

  • @deepjyotidebnath4122
    @deepjyotidebnath4122 3 года назад +10

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

  • @surajdevv
    @surajdevv 2 месяца назад +4

    Sum of n natural number approach is easy but does not work on different test cases only works for certain test cases

  • @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 2 года назад

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

  • @khushichaurasia7599
    @khushichaurasia7599 3 года назад +7

    Your explaining level ....😍😍😍😍

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

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

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

  • @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. 3 года назад +2

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

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

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

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

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

    • @har.preet.singh.
      @har.preet.singh. 3 года назад

      @@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 3 года назад

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

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

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

  • @shivamkumar-hu3bv
    @shivamkumar-hu3bv 2 года назад +2

    From where n=5 comes ??

  • @FANSasFRIENDS
    @FANSasFRIENDS 2 года назад +1

    Sir, kindly explain what is overflow for this case.

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

      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.

  • @oldguardkiller8408
    @oldguardkiller8408 Год назад +1

    We can Also Use Cyle Sort

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

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

  • @nanogyanx
    @nanogyanx 2 месяца назад +1

    An excellent video

  • @kommunagasheshu3601
    @kommunagasheshu3601 2 года назад +1

    As well as please explain code for the problem.

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

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

  • @rajukumar-sv3vj
    @rajukumar-sv3vj Год назад

    sir very good explain thankyou sir ji

  • @dineshverma-in7on
    @dineshverma-in7on 2 года назад +1

    use binary search, time complexity will be logN

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

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

  • @creativeMinds1603
    @creativeMinds1603 4 месяца назад

    Great explanation 😃

  • @ffproplayershere
    @ffproplayershere 3 месяца назад

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

  • @har.preet.singh.
    @har.preet.singh. 3 года назад +2

    Can you please share the code of the third approach?

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

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

  • @natnaelghirma2617
    @natnaelghirma2617 3 года назад +3

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

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

      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.

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

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

    Your voice is same as apna college channel...

  • @surendharv795
    @surendharv795 9 месяцев назад

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

  • @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 RUclips 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 👍🏻

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

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

  • @shubhamnehulkar135
    @shubhamnehulkar135 4 года назад +2

    what if i have multiple missing numbers??

    • @techdose4u
      @techdose4u  4 года назад +2

      So that will be a totally different question.

  • @InvincibleMan99
    @InvincibleMan99 5 месяцев назад

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

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

    Sir can you explain the code for XOR method ??

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

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

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

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

    nice explanation sir

  • @IrenJahan-ru5tx
    @IrenJahan-ru5tx Год назад

    Good video

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

  • @jatinahuja6929
    @jatinahuja6929 4 года назад +1

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

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

    Java code: ide.geeksforgeeks.org/6tkkn1JzX0

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

  • @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 2 года назад

      The BIG O complexity is still O(N).

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

    nice solution!

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

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

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

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

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

  • @jaydalsaniya6986
    @jaydalsaniya6986 5 лет назад +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  5 лет назад +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 :)

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

  • @ENagamani-yl1ub
    @ENagamani-yl1ub 24 дня назад

    How to solve of array is jumbled

  • @JohnLee-xl2ut
    @JohnLee-xl2ut 4 года назад

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

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

      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 4 года назад

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

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

  • @zeeu
    @zeeu 3 года назад +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]

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

    Methode 2 won't work if 0 present in array

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

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

    If the array doesn't contain any missing than

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

    Lovely

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

    great

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

    GOOD BLOG

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

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

    Thanks

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

    in second method the n=4 not 5

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

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

  • @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 2 года назад

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

  • @UmeshKumar-zc4zc
    @UmeshKumar-zc4zc 4 года назад

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

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

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

    • @UmeshKumar-zc4zc
      @UmeshKumar-zc4zc 4 года назад +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  4 года назад +1

      Sure. I will include other version as well.

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

    find missing number in arere

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

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

  • @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 Год назад

    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]

  • @satyaprakash4598
    @satyaprakash4598 4 года назад +1

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

    • @sagarlandge3271
      @sagarlandge3271 4 года назад +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]

  • @Prashant_godbole
    @Prashant_godbole Месяц назад

    waste of time