This function, find_missing_number
, takes an unsorted array of unique integers that form a sequence and returns the nth
missing number in that sequence, defaulting to the first missing number if nth
is not specified. The smallest number in the sequence is assumed to be 1, and all numbers are positive.
- Example 1:
arr = [4, 3, 6, 2, 1, 7] puts find_missing_number(arr) # Output: 5
- Example 2:
arr = [4, 3, 6, 2, 1, 7, 9] puts find_missing_number(arr, nth: 2) # Output: 8
- Example 3:
arr = [4, 3, 6, 2, 1, 7, 9] puts find_missing_number(arr, nth: 3) # Output: 10
Ruby: Ensure you have Ruby installed (version 2.5+).
RSpec: Used for running tests. Install it if you don't have it by running:
gem install rspec
- Running the Function:
- You can directly call the function in your Ruby code or use the examples provided.
- Running the Tests:
-
The tests are written using RSpec. To run the tests, execute the following command in your terminal:
rspec find_missing_number_spec.rb
find_missing_number.rb
Contains thefind_missing_number
functionfind_missing_number_spec.rb
Contains the RSpec tests for the functionREADME.md
This file with instructions on how to use the solution
- The function works efficiently for typical sequences and handles edge cases such as an empty array or when the nth missing number is negative or outside the given sequence range.