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
Check if Array [1,3,2,5,4] is ConsecutiveInput Array:13254Min = 1, Length = 5 → Expected range: [1,2,3,4,5]Expected:12345✓ All expected numbers found → true
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]
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen