ConsecutiveNumbers
SRM 402 · 2008-05-24 · by srbga
Problem Statement
Your little son has written some consecutive positive integers on a piece of paper, in no particular order. Each integer was written exactly once. He then erased one of the integers and showed you the remaining ones. Do you know which number your son erased?
Formally, a list of positive integers is said to be consecutive if there are two positive integers a and b such that every integer between a and b, inclusive, appears in the list exactly once.
For example, if the remaining numbers are (10,7,12,8,11), the only possible missing number is 9. If the remaining numbers are (2,3), the missing number could be either 1 or 4. If the remaining numbers are (3,6), your son must have made a mistake.
You are given the remaining numbers in a
Constraints
- numbers will contain between 1 and 50 elements, inclusive.
- Each element of numbers will be between 1 and 1000000000, inclusive.
{10,7,12,8,11}
Returns: {9 }
The first example in the problem statement.
{3,6}
Returns: { }
The third example in the problem statement.
{5,6,7,8}
Returns: {4, 9 }
There original list might be {4,5,6,7,8} or {5,6,7,8,9}.
{1000000000}
Returns: {999999999, 1000000001 }
{1,6,9,3,8,12,7,4,11,5,10}
Returns: {2 }
{5,5}
Returns: { }
no duplications
{1}
Returns: {2 }
Only positive integers are allowed.
{1,3,5}
Returns: { }
only one gap
Submissions are judged against all 186 archived test cases, of which 8 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ConsecutiveNumbers with a public method vector<int> missingNumber(vector<int> numbers) · 186 test cases · 2 s / 256 MB per case