Check if an Array Is Consecutive
EasyUpdated: Jul 3, 2025
Practice on:
Problem
Given an integer array nums, return true ifnums _isconsecutive , 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.
Examples
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 <= 10^50 <= nums[i] <= 10^5
Solution
Method 1 – Hash Set and Range Check
Intuition
If the array is consecutive, it must contain all numbers from the minimum to the maximum value without any duplicates or missing numbers. Using a set helps us check for duplicates and missing numbers efficiently.
Approach
- Find the minimum value
mnin the array and its lengthn. - Compute the maximum value
mxin the array. - If
mx - mn + 1 != n, return false (the range is not correct). - Insert all elements into a set.
- If the set size is not equal to
n, return false (there are duplicates). - Otherwise, return true.
Code
C++
class Solution {
public:
bool isConsecutive(vector<int>& nums) {
int mn = *min_element(nums.begin(), nums.end());
int mx = *max_element(nums.begin(), nums.end());
int n = nums.size();
if (mx - mn + 1 != n) return false;
unordered_set<int> s(nums.begin(), nums.end());
return s.size() == n;
}
};
Go
func isConsecutive(nums []int) bool {
n := len(nums)
mn, mx := nums[0], nums[0]
s := make(map[int]struct{})
for _, v := range nums {
if v < mn { mn = v }
if v > mx { mx = v }
s[v] = struct{}{}
}
if mx-mn+1 != n { return false }
return len(s) == n
}
Java
class Solution {
public boolean isConsecutive(int[] nums) {
int mn = nums[0], mx = nums[0], n = nums.length;
Set<Integer> s = new HashSet<>();
for (int v : nums) {
mn = Math.min(mn, v);
mx = Math.max(mx, v);
s.add(v);
}
if (mx - mn + 1 != n) return false;
return s.size() == n;
}
}
Kotlin
class Solution {
fun isConsecutive(nums: IntArray): Boolean {
val n = nums.size
var mn = nums[0]
var mx = nums[0]
val s = mutableSetOf<Int>()
for (v in nums) {
mn = minOf(mn, v)
mx = maxOf(mx, v)
s.add(v)
}
if (mx - mn + 1 != n) return false
return s.size == n
}
}
Python
def isConsecutive(nums: list[int]) -> bool:
mn: int = min(nums)
mx: int = max(nums)
n: int = len(nums)
if mx - mn + 1 != n:
return False
return len(set(nums)) == n
Rust
use std::collections::HashSet;
impl Solution {
pub fn is_consecutive(nums: Vec<i32>) -> bool {
let mn = *nums.iter().min().unwrap();
let mx = *nums.iter().max().unwrap();
let n = nums.len() as i32;
if mx - mn + 1 != n { return false; }
let s: HashSet<_> = nums.iter().collect();
s.len() as i32 == n
}
}
Complexity
- ⏰ Time complexity:
O(n), where n is the length of the array. - 🧺 Space complexity:
O(n), for the set.