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:

1
2
3
4
5
6
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:

1
2
3
4
5
6
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:

1
2
3
4
5
6
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^5
  • 0 <= 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

  1. Find the minimum value mn in the array and its length n.
  2. Compute the maximum value mx in the array.
  3. If mx - mn + 1 != n, return false (the range is not correct).
  4. Insert all elements into a set.
  5. If the set size is not equal to n, return false (there are duplicates).
  6. Otherwise, return true.

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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;
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
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
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
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;
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
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
    }
}
1
2
3
4
5
6
7
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
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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.