2229. Check if an Array Is Consecutive π ο
Descriptionο
Given an integer array nums
, return true
if nums
is consecutive, otherwise return false
.
An array is consecutive if it contains every number in the range [x, x + n - 1]
(inclusive), where x
is the minimum number in the array and n
is the length of the array.
Example 1:
Input: nums = [1,3,4,2] Output: true Explanation: The minimum value is 1 and the length of nums is 4. All of the values in the range [x, x + n - 1] = [1, 1 + 4 - 1] = [1, 4] = (1, 2, 3, 4) occur in nums. Therefore, nums is consecutive.
Example 2:
Input: nums = [1,3] Output: false Explanation: The minimum value is 1 and the length of nums is 2. The value 2 in the range [x, x + n - 1] = [1, 1 + 2 - 1], = [1, 2] = (1, 2) does not occur in nums. Therefore, nums is not consecutive.
Example 3:
Input: nums = [3,5,4] Output: true Explanation: The minimum value is 3 and the length of nums is 3. All of the values in the range [x, x + n - 1] = [3, 3 + 3 - 1] = [3, 5] = (3, 4, 5) occur in nums. Therefore, nums is consecutive.
Constraints:
1 <= nums.length <= 105
0 <= nums[i] <= 105
Solutionsο
Solution 1: Hash Tableο
We can use a hash table $\textit{s}$ to store all the elements in the array $\textit{nums}$, and use two variables $\textit{mi}$ and $\textit{mx}$ to represent the minimum and maximum values in the array, respectively.
If all elements in the array are distinct and the length of the array equals the difference between the maximum and minimum values plus $1$, then the array is consecutive, and we return $\textit{true}$; otherwise, we return $\textit{false}$.
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\textit{nums}$.
Python3ο
class Solution:
def isConsecutive(self, nums: List[int]) -> bool:
mi, mx = min(nums), max(nums)
return len(set(nums)) == mx - mi + 1 == len(nums)
Javaο
class Solution {
public boolean isConsecutive(int[] nums) {
int mi = nums[0], mx = 0;
Set<Integer> s = new HashSet<>();
for (int x : nums) {
if (!s.add(x)) {
return false;
}
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}
return mx - mi + 1 == nums.length;
}
}
C++ο
class Solution {
public:
bool isConsecutive(vector<int>& nums) {
unordered_set<int> s;
int mi = nums[0], mx = 0;
for (int x : nums) {
if (s.contains(x)) {
return false;
}
s.insert(x);
mi = min(mi, x);
mx = max(mx, x);
}
return mx - mi + 1 == nums.size();
}
};
Goο
func isConsecutive(nums []int) bool {
s := map[int]bool{}
mi, mx := nums[0], 0
for _, x := range nums {
if s[x] {
return false
}
s[x] = true
mi = min(mi, x)
mx = max(mx, x)
}
return mx-mi+1 == len(nums)
}
TypeScriptο
function isConsecutive(nums: number[]): boolean {
let [mi, mx] = [nums[0], 0];
const s = new Set<number>();
for (const x of nums) {
if (s.has(x)) {
return false;
}
s.add(x);
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}
return mx - mi + 1 === nums.length;
}
JavaScriptο
/**
* @param {number[]} nums
* @return {boolean}
*/
var isConsecutive = function (nums) {
let [mi, mx] = [nums[0], 0];
const s = new Set();
for (const x of nums) {
if (s.has(x)) {
return false;
}
s.add(x);
mi = Math.min(mi, x);
mx = Math.max(mx, x);
}
return mx - mi + 1 === nums.length;
};