716. Check if an Array Is Consecutive

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.

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.

Now Your Turn!

Pick the correct output for the given input

Consider the array nums = [3, 5, 4]. Is it consecutive?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 105

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
bool isConsecutive(vector<int>& nums) {
// Your code goes here
}
};
Test Case

Input:

Nums