Given an unsorted array of numbers, write a function that returns true if array consists of consecutive numbers.
Examples:
If array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers from 1 to 5.
If array is {83, 78, 80, 81, 79, 82}, then the function should return true because the array has consecutive numbers from 78 to 83.
If the array is {34, 23, 52, 12, 3 }, then the function should return false because the elements are not consecutive.
If the array is {7, 6, 5, 5, 3, 4}, then the function should return false because 5 and 5 are not consecutive.
I came up with the following algo:
find the max and min of the array
max-min+1 should be the size of array
check for duplicates
check for all consecutive numbers in between
How can I achieve the 4th path? (The complexity should be O(n)
)
Other suggestions are most welcome.