Check if an Array Is Consecutive - Problem

Given an integer array nums, determine if the array contains consecutive integers that form a complete sequence.

An array is considered consecutive if it contains every number in the range [x, x + n - 1] (inclusive), where:

  • x is the minimum number in the array
  • n is the length of the array

Return true if the array is consecutive, false otherwise.

Example: The array [4, 2, 1, 3] is consecutive because it contains all numbers from 1 to 4. However, [1, 3, 4] is not consecutive because it's missing 2.

Input & Output

example_1.py โ€” Basic Consecutive Array
$ Input: [1, 3, 2, 4]
โ€บ Output: true
๐Ÿ’ก Note: The array contains all numbers from 1 to 4. After sorting: [1,2,3,4] - each adjacent pair differs by 1, forming a complete consecutive sequence.
example_2.py โ€” Missing Number
$ Input: [1, 3, 4]
โ€บ Output: false
๐Ÿ’ก Note: The minimum is 1 and length is 3, so we expect [1,2,3]. However, the number 2 is missing from the array, making it non-consecutive.
example_3.py โ€” Single Element
$ Input: [5]
โ€บ Output: true
๐Ÿ’ก Note: A single element is always consecutive. The expected range is [5,5] which contains only the number 5, and it exists in the array.

Visualization

Tap to expand
๐Ÿ“š The Complete Encyclopedia CheckYour Book Collection (Unsorted):4213Volumes: [4, 2, 1, 3] - Mixed up order!Step 1: Create Quick-Lookup Catalog๐Ÿ“‹ Hash Set Catalog:1234O(1) lookup!Step 2: Determine Expected RangeMin Volume = 1, Collection Size = 4Expected: Volumes 1, 2, 3, 4 (complete set)Step 3: Instant Verificationโœ“ Volume 1 in catalog? YESโœ“ Volume 2 in catalog? YESโœ“ Volume 3 in catalog? YESโœ“ Volume 4 in catalog? YES๐ŸŽ‰ COMPLETE COLLECTION!Approach Comparison๐Ÿ”จ Brute Force: Check each shelf repeatedlyTime: O(nยฒ) - Very slow for large collections๐Ÿ“Š Sorting: Arrange first, then verify orderTime: O(n log n) - Faster but still sorting overheadโšก Hash Set: Instant catalog lookupTime: O(n) - Optimal solution!๐Ÿ’ก The key insight: Transform the search problem into a lookup problem!
Understanding the Visualization
1
Catalog Your Books
Create a quick-lookup catalog (hash set) of all books you have
2
Determine Expected Range
Find the lowest volume number and calculate expected range
3
Check Each Volume
For each expected volume, instantly check if it exists in your catalog
Key Takeaway
๐ŸŽฏ Key Insight: Instead of searching for each expected number repeatedly, create a hash set for instant O(1) lookups. This transforms an O(nยฒ) search problem into an O(n) lookup problem.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

O(n) to create hash set + O(n) to check all expected numbers = O(n)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash set stores all n numbers from the input array

n
2n
โšก Linearithmic Space

Constraints

  • 0 โ‰ค nums.length โ‰ค 105
  • -109 โ‰ค nums[i] โ‰ค 109
  • All elements in nums are integers
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28 Apple 22
42.3K Views
Medium Frequency
~15 min Avg. Time
1.9K 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