Problem
Submissions

Solution: Missing Number

Statement

Naive approach

A naive approach would be to first sort the array using quick sort and then traverse the array with two adjacent pointers. Since the integers are sorted, the difference between two adjacent array elements should be 11 if there is no missing integer. We can start by having the first pointer at index 0 and the second pointer at index 1, moving both 11 step forward each time. If the difference is greater than 1, our missing value would be the value of the first pointer ++ 11.

The time complexity for this approach becomes O(nlogn)+O(n)O(n \log n) + O(n). The space complexity is O(n)O(n).

Optimized approach using cyclic sort

The intuition behind using this approach uses the unique property where each element should match its index, given data in a specific range. The algorithm starts by sorting the array so that each element is placed in its correct position (i.e., its value matches its index). After sorting, the first instance where an element does not match its index indicates the missing number.

Let’s walkthrough the detailed steps of this algorithm:

Problem
Submissions

Solution: Missing Number

Statement

Naive approach

A naive approach would be to first sort the array using quick sort and then traverse the array with two adjacent pointers. Since the integers are sorted, the difference between two adjacent array elements should be 11 if there is no missing integer. We can start by having the first pointer at index 0 and the second pointer at index 1, moving both 11 step forward each time. If the difference is greater than 1, our missing value would be the value of the first pointer ++ 11.

The time complexity for this approach becomes O(nlogn)+O(n)O(n \log n) + O(n). The space complexity is O(n)O(n).

Optimized approach using cyclic sort

The intuition behind using this approach uses the unique property where each element should match its index, given data in a specific range. The algorithm starts by sorting the array so that each element is placed in its correct position (i.e., its value matches its index). After sorting, the first instance where an element does not match its index indicates the missing number.

Let’s walkthrough the detailed steps of this algorithm: