Check if an Array Is Consecutive - Problem
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.
Input & Output
Example 1 — Consecutive Array
$
Input:
nums = [1,3,2,5,4]
›
Output:
true
💡 Note:
The minimum is 1, array length is 5. Expected range [1,2,3,4,5] - all numbers present, so it's consecutive.
Example 2 — Missing Number
$
Input:
nums = [1,0,3]
›
Output:
false
💡 Note:
The minimum is 0, array length is 3. Expected range [0,1,2] but we have [0,1,3] - missing 2, so not consecutive.
Example 3 — Duplicate Numbers
$
Input:
nums = [1,1,2,3]
›
Output:
false
💡 Note:
Contains duplicates. For consecutive array [1,2,3,4], we need unique numbers 1,2,3,4 but have 1,1,2,3.
Constraints
- 1 ≤ nums.length ≤ 105
- -106 ≤ nums[i] ≤ 106
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array with potentially unordered elements
2
Find Range
Determine expected consecutive range [min, min+n-1]
3
Verify
Check if all expected numbers exist without duplicates
Key Takeaway
🎯 Key Insight: A consecutive array has exactly n unique numbers covering the range [min, min+n-1]
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code