-1

Hi guys im learning c# currently and Im trying to run threw some interview questions to try and understand them and learn in the process.

I found a question, How do you find the missing number in a given integer array of 1 to 100?

I think I get the General Idea of having to get the sum, Then the Sum of the Sequence then Minus X - Y;

But im confused about the code example below, Why is he using * (arr.Length + 2) When the Equation is n*(n+1)/2 I know its correct but shouldnt this be (arr.Length + 1) * (arr.Length) / 2 According tot he formula

Im terribly confused.

//array to find the missing number between 1 and 10
            // Simplicity, We will take number 1 to 10 i where Number 5 is missing in the sequence.
            int[] arr = { 1, 2, 3, 4, 6, 7, 8, 9, 10 };

            int missingNumber,Totalsum;
            // Accoreding to series rule, calculating sum of total numbers upto 10
            //sum of first n natutal numbers=n*(n+1)/2
            Totalsum = (arr.Length + 1) * (arr.Length + 2) / 2;

            // Missing number is calculating.
            foreach (int item in arr)
            {
                Totalsum = Totalsum - item;
            }
            missingNumber = Totalsum;

            Console.WriteLine("missing number  : {0}",missingNumber);

Not a Assignment. Just someone trying to learn programming by them selves looking up random questions to learn practically

Where i got the question

https://simpleprogrammer.com/programming-interview-questions/

The Explanation I found

https://www.interviewsansar.com/find-missing-number-between-1-to-n-in-array-in-c/

Someone
  • 3
  • 3
  • Since one number is _missing_, array length is one short of what you want to find. In other words: You need f(10) but array length would give you f(9). That's why you need f(arr.Length +1) here. – Fildor Oct 12 '20 at 13:47
  • @Someone, My mistake, I deleted my comment. – Mujahid Daud Khan Oct 12 '20 at 13:48
  • I'm not sure I'd have attacked this problem like this... – Caius Jard Oct 12 '20 at 13:48
  • Ah But then its just +1 right? So why the + 2? – Someone Oct 12 '20 at 13:48
  • There is a good number of typo here and there. I know it's not related to your question But I will recommend any interview/rank/ programing test on the marker where you have multiple answers per questions with detailed explanation. Like : https://www.geeksforgeeks.org/find-the-missing-number/ – xdtTransform Oct 12 '20 at 13:50
  • Because f(x) = x * (x+1) / 2 , so if you put x = n+1 it becomes f(n+1) = (n+1) * ( n+1+1) / 2 – Fildor Oct 12 '20 at 13:50
  • @Someone, Seems like the equation is wrong here: https://stackoverflow.com/a/2113827/1735196 – Mujahid Daud Khan Oct 12 '20 at 13:50
  • While learning a new language I will advice beeing carefull with the interview questions. Because many of them require niche knowledge that doesn't relate to programming. And are more about math. For example your next question in the "Top 50 Programming Interview Questions" is "_How do you find the duplicate number on a given integer array?_" while the best solution is "tortoise and hare" algorythm, the hashset solution is Imo a better programming solution. By that I mean that understanding hashset is more important that knowing "tortoise and hare". – xdtTransform Oct 12 '20 at 14:53

1 Answers1

2

The comment gives the answer to your question:

// Accoreding to series rule, calculating sum of total numbers upto 10
//sum of first n natutal numbers=n*(n+1)/2

It's just that it's a number sequence that "should have 10 numbers but actually only has 9"

The formula needs n to be 10, but the array has a length of 9. 9+1 is n, 9+2 is n+1

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • 1
    Ah your right because x needs to be n and n is 10 Thank you sir that is a very easy to understand explanation. Actually that makes so much sense after reading this i feel like an idiot for asking this, thank you for taking the time to answer this – Someone Oct 12 '20 at 13:54
  • @Someone :D I guess this is one of those ... do you know those pictures where they subtitle "Wait for it ..." and suddenly you realize something awkward in the pic, but you really need some time to realize but "what has been seen cannot be unseen"? – Fildor Oct 12 '20 at 14:09
  • Its exactly like that – Someone Oct 13 '20 at 06:29