Given an unsorted array of numbers, write a function that returns true if the array consists of consecutive numbers.
Examples:
a) If the array is {5, 2, 3, 1, 4}, then the function should return true because the array has consecutive numbers from 1 to 5.
b) If the array is {83, 78, 80, 81, 79, 82}, then the function should return true because the array has consecutive numbers from 78 to 83.
c) If the array is {34, 23, 52, 12, 3}, then the function should return false because the elements are not consecutive.
d) If the array is {7, 6, 5, 5, 3, 4}, then the function should return false because 5 and 5 are not consecutive.Method 1 (Use Sorting)
1) Sort all the elements.
2) Do a linear scan of the sorted array. If the difference between the current element and the next element is anything other than 1, then return false. If all differences are 1, then return true.
- C#
Array elements are consecutive
Time Complexity: O(n log n)
Space Complexity: O(1)
Method 2 (Use visited array)
The idea is to check for the following two conditions. If the following two conditions are true, then return true.
1) max – min + 1 = n where max is the maximum element in the array, min is the minimum element in the array and n is the number of elements in the array.
2) All elements are distinct.
To check if all elements are distinct, we can create a visited[] array of size n. We can map the ith element of input array arr[] to the visited array by using arr[i] – min as the index in visited[].
- C#
Array elements are consecutive
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 3 (Mark visited array elements as negative)
This method is O(n) time complexity and O(1) extra space, but it changes the original array, and it works only if all numbers are positive. We can get the original array by adding an extra step though. It is an extension of method 2, and it has the same two steps.
1) max – min + 1 = n where max is the maximum element in the array, min is the minimum element in the array and n is the number of elements in the array.
2) All elements are distinct.
In this method, the implementation of step 2 differs from method 2. Instead of creating a new array, we modify the input array arr[] to keep track of visited elements. The idea is to traverse the array and for each index i (where 0 ≤ i < n), make arr[arr[i] – min]] as a negative value. If we see a negative value again then there is repetition.
- C#
Array elements are consecutive
Note that this method might not work for negative numbers. For example, it returns false for {2, 1, 0, -3, -1, -2}.
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 4 (Using XOR property)
This method is O(n) time complexity and O(1) extra space, does not changes the original array, and it works every time.
- As elements should be consecutive, let’s find minimum element or maximum element in array.
- Now if we take xor of two same elements it will result in zero (a^a = 0).
- Suppose array is {-2, 0, 1, -3, 4, 3, 2, -1}, now if we xor all array elements with minimum element and keep increasing minimum element, the resulting xor will become 0 only if elements are consecutive
- C
- C++
- Java
- Python3
- C#
- Javascript
Array elements are consecutive
Time Complexity: O(n)
Auxiliary Space: O(1)
Please suggest if someone has a better solution which is more efficient in terms of space and time.

