Find the missing number | GeeksforGeeks

Поділитися
Вставка
  • Опубліковано 31 бер 2016
  • Explanation for the article: www.geeksforgeeks.org/find-the...
    This video is contributed by Harshit Jain.

КОМЕНТАРІ • 48

  • @senthilmurugan1553
    @senthilmurugan1553 4 роки тому

    Geeks for geeks excellent platform to learn

  • @marouane55
    @marouane55 5 років тому +2

    We can solve this problem in log(n). If the array is sorted using binary search
    arr = list(range(0,79)) + list(range(80,139))
    def find_mission(start, end):
    if start >= end:
    return start
    expected = int((start+ end) / 2)
    if arr[expected] != expected :
    end = expected
    else:
    start = expected + 1
    return find_mission(start, end)
    print(arr)
    print(find_mission(0, len(arr) - 1))

  • @dwstyagi
    @dwstyagi 4 роки тому +1

    in the algorithm, u say that in step 2 is take XOR of all n natural number but in code u r taking xor from 2 to n+1, n+1 is fine but why 2 not 0?

  • @soulsscience2198
    @soulsscience2198 5 років тому

    dude what is the reason for incrementing the "n"value...?

  • @SahitiSeemakurti
    @SahitiSeemakurti 8 років тому +5

    what if you need to find 2 missing numbers? how do you do this with XOR?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 років тому +7

      You can refer to our following articles for the answer:
      1. www.geeksforgeeks.org/find-two-missing-numbers-set-1-an-interesting-linear-time-solution/
      2. www.geeksforgeeks.org/find-two-missing-numbers-set-2-xor-based-solution/
      The second one discusses the solution using XOR.

  • @jm7709
    @jm7709 5 років тому +3

    Indexing from 1 to n. If a(i)!=i means if element value is not equal to index den break the loop and return i value.
    How about this??

    • @debakarr
      @debakarr 5 років тому +1

      The list is not sorted. So this wouldn't work.

  • @warpromo6636
    @warpromo6636 3 роки тому

    lol what r u talking about just do
    for(var i=0; i

  • @warpromo6636
    @warpromo6636 3 роки тому

    i have two solutions:
    for(var i=0; i

  • @spicytuna08
    @spicytuna08 6 років тому

    awesome algorithm.

  • @sangnguyenn2367
    @sangnguyenn2367 Рік тому

    Thank you so much

  • @itsmerahulsoni
    @itsmerahulsoni 5 років тому +1

    Thank you for this video. Could you please tell me if i have to find missing number for list not starting from 1 like [96,100, 99,98] which is 97 how can i use this approach?

    • @abhishekaman708
      @abhishekaman708 4 роки тому

      sort the array in ascending order. Take its first and last element. Perform any method. If your answer comes 0. Its either 1 less than first or 1 more than last element. For that condition has to be provided.

  • @neerajkumbhkar6965
    @neerajkumbhkar6965 3 роки тому +1

    this solution is not working for array containing 0 i.e {3,0,2} giving 6 not 2 why?

  • @sangnguyenn2367
    @sangnguyenn2367 Рік тому

    Thank you

  • @meghanayalmame5288
    @meghanayalmame5288 4 роки тому +1

    what if there is duplication of elements?
    whats the code
    pls help me out

    • @alokyadav6812
      @alokyadav6812 4 роки тому

      In python, We can convert the array into set and then you can apply the mentioned methods.

    • @warpromo6636
      @warpromo6636 3 роки тому

      @@alokyadav6812 no dummy
      arr = arr.sort()
      for(var i=0; i

  • @saisumanth1739
    @saisumanth1739 6 років тому +1

    How to solve,if i want to find missing number in whole numbers.Printing zero , if zero was missing

    • @SreeragNairisawesome
      @SreeragNairisawesome 6 років тому

      its mentioned that this problem will be in the range of 1 - n. But if the range changes to 0 - n, use the same method above : In method 1, if u keep subtracting all the elements of the array, you will get 0 as output.
      In method 2 , use condition statement to check if the number obtained after xoring is 0 or no.

  • @shashantbartwal8279
    @shashantbartwal8279 6 років тому

    we can use hashing tehnique here too,

    • @spicytuna08
      @spicytuna08 6 років тому

      do you have code? are you thinking about C++ map STL?

    • @onelinertech
      @onelinertech 5 років тому

      @@spicytuna08 just initialize an array of size n+1 with zero and increment the value of respective index when that number is found in array, at last traverse the array and print the index when a ZERO is encountered.

  • @nands4410
    @nands4410 7 років тому +1

    Why can't we just sort the entire list using inbuilt function sort(array,array+size) and then compare the sorted elements of the array with the iteration variable in the for loop if it is not equal then print the missing integer?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 років тому +10

      We can do that too. The time complexity of that solution will be O(nlogn) as we are sorting the array.
      However, the XOR technique gives us a time complexity of O(n).

    • @sarveshbajaj5178
      @sarveshbajaj5178 5 років тому

      @@GeeksforGeeksVideos Just run a loop from 1 to n and check with i..

  • @souravsaini8334
    @souravsaini8334 3 роки тому

    public class FIndMissingNumber {
    public static void main(String[] args) {
    int[] arr = {1, 2, 3, 4, 5, 6, 8, 9, 10, 11};
    findNumber(arr);
    }
    private static void findNumber(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
    if (arr[i + 1] - arr[i] != 1) {
    System.out.println(arr[i] + 1);
    break;
    } else if (arr[i + 1] - arr[i] == 1) {
    if (i == arr.length - 2) {
    System.out.println("Noting is missing");
    break;
    }
    }
    }
    }
    }

  • @abhinavkumar-ir1od
    @abhinavkumar-ir1od 7 років тому

    in the second for loop why i is initialized to 2

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  7 років тому +1

      This is because x2 is initialized as 1. And, we want to take XORs of all the numbers from 1 to n+1. So, we run a loop from 2 to n+1.

  • @ashithab7768
    @ashithab7768 6 років тому +1

    will xor work for integers?

  • @warpromo6636
    @warpromo6636 3 роки тому

    why the hell do you care about complexity when computers are super duper powerful.

    • @Bhatonia_Jaat
      @Bhatonia_Jaat 3 роки тому

      when it comes to a large database of an organization, it's important to have a faster and accurate algorithm.

  • @SUDHANSHUKUMAR-cw8lv
    @SUDHANSHUKUMAR-cw8lv 5 років тому

    sir, but this is valid only for 1 to 10, . we just want other then this number . ex- if i enter n=5, 21, 25, 22, 26, 23. and for this our output should be 24, but i am getting -136 . .... why? plz help me out soon

  • @jeedikantivenkat7814
    @jeedikantivenkat7814 4 роки тому

    Entirely wrong
    The code works only for the integers from 1 to 9
    Even if u use xor method also it's not at all giving the correct answer

    • @KnowledgeAmplifier1
      @KnowledgeAmplifier1 4 роки тому

      Jeedikanti check this , it may work..
      ua-cam.com/video/DEo1Rve-aj8/v-deo.html

    • @warpromo6636
      @warpromo6636 3 роки тому

      @@KnowledgeAmplifier1 lol what about this:
      for(var i=0; i

  • @shreyaswaghmare7690
    @shreyaswaghmare7690 4 роки тому

    Easy

  • @mathspulligal7460
    @mathspulligal7460 Рік тому

    Method 1 is better

  • @sangnguyenn2367
    @sangnguyenn2367 Рік тому +1

    😍😍😍😍😍😍😍😍😍😍😍😍😍😍

  • @nanusk991
    @nanusk991 6 років тому +6

    explanation is not good

  • @kaustavpaul9306
    @kaustavpaul9306 3 роки тому

    More Simple approach(sort the array athirst, then check if the "I"th element +1 != "I+1"th element, then print the number which is missing.) Code available below ->
    public class missingNumber {
    public static void main(String[] args) {
    int arr[] = {1, 2, 4, 6, 3, 7, 8};
    // 1 2 3 4 6 7 8 (5) missing
    sortArray(arr);
    getMissing(arr);
    }
    private static void getMissing(int[] arr) {
    int len = arr.length;
    for(int i=0; i