Missing Arithmetic number

There is a sequence of numbers representing an arithmetic progression in order. It is given that one of the numbers is missing in the progression. What is the minimum (most efficient) worst case complexity for finding the missing elements?

Θ ( n log n ) \Theta(n \log n) Θ ( 1 ) \Theta(1) Θ ( log ( n ) ) \Theta(\log(n)) Θ ( n ) \Theta(n)

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Toby M
Dec 5, 2020

Let a i a_i be an element in the arithmetic progression, where 1 i n 1 ≤ i ≤ n .

First find the common difference by calculating a n a 1 n 1 = a 1 + ( n 1 ) d a 1 n 1 = d \frac{a_n - a_1}{n-1} = \frac{a_1 + (n-1)d - a_1}{n - 1} = d , as the missing number cannot be a 1 a_1 or a n a_n (otherwise there would be a shorter progression with no missing number). We can perform a binary search by splitting the progression into two, then calculating a n / 2 a 1 d \frac{a_{\lfloor n/2 \rfloor} - a_1}{d} and a n a n / 2 d \frac{a_{n} - a_{\lfloor n/2 \rfloor} }{d} . This will give us n / 2 1 {\lfloor n/2 \rfloor}-1 , but if there are not this number of elements in the given range, it must contain the missing number.

Try this out with a list of 10 numbers in an arithmetic progression, say 1 , 3 , 5 19 1, 3, 5 \cdots 19 to get a feel for this process. Now rinse and repeat until you find the missing number: and since a binary search has complexity O ( log n ) O(\log n) , the answer is O ( log n ) O(\log n) .

For the lazy: since this is multiple choice, use the process of elimination. The time complexity is at most O ( n ) O(n) as we can find the missing element by calculating the successive differences (such as a 2 a 1 , a 3 a 2 a_2 - a_1, a_3 - a_2 ). Hence we can rule out O ( n log n ) O(n \log n) . The time complexity is definitely not O ( 1 ) O(1) either as this task is more complex than finding a given number in a sorted list. Now you either have O ( n ) O(n) or O ( log n ) O(\log n) , and it probably will be the least complex of the two choices.

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...