Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/2229.html

2229. Check if an Array Is Consecutive (Easy)

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.

Example 3:

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 <= 105
  • 0 <= nums[i] <= 105

Companies:
Turbot

Related Topics:
Array

Similar Questions:

Solution 1. Sorting

  • // OJ: https://leetcode.com/problems/check-if-an-array-is-consecutive/
    // Time: O(N)
    // Space: O(1)
    class Solution {
    public:
        bool isConsecutive(vector<int>& A) {
            sort(begin(A), end(A));
            for (int i = 1; i < A.size(); ++i) {
                if (A[i] != A[i - 1] + 1) return false;
            }
            return  true;
        }
    };
    
  • class Solution:
        def isConsecutive(self, nums: List[int]) -> bool:
            mi, mx = min(nums), max(nums)
            n = len(nums)
            return len(set(nums)) == n and mx == mi + n - 1
    
    
    
  • class Solution {
        public boolean isConsecutive(int[] nums) {
            int mi = nums[0];
            int mx = nums[0];
            Set<Integer> s = new HashSet<>();
            for (int v : nums) {
                mi = Math.min(mi, v);
                mx = Math.max(mx, v);
                s.add(v);
            }
            int n = nums.length;
            return s.size() == n && mx == mi + n - 1;
        }
    }
    
  • func isConsecutive(nums []int) bool {
    	s := make(map[int]bool)
    	mi, mx := nums[0], nums[0]
    	for _, v := range nums {
    		s[v] = true
    		mi = min(mi, v)
    		mx = max(mx, v)
    	}
    	return len(s) == len(nums) && mx == mi+len(nums)-1
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
    func min(a, b int) int {
    	if a < b {
    		return a
    	}
    	return b
    }
    

Solution 2.

  • // OJ: https://leetcode.com/problems/check-if-an-array-is-consecutive/
    // Time: O(N)
    // Space: O(N)
    class Solution {
    public:
        bool isConsecutive(vector<int>& A) {
            int mn = *min_element(begin(A), end(A));
            vector<bool> seen(A.size());
            for (int n : A) {
                n -= mn;
                if (n < 0 || n >= A.size() || seen[n]) return false;
                seen[n] = true;
            }
            return true;
        }
    };
    
  • class Solution:
        def isConsecutive(self, nums: List[int]) -> bool:
            mi, mx = min(nums), max(nums)
            n = len(nums)
            return len(set(nums)) == n and mx == mi + n - 1
    
    
    
  • class Solution {
        public boolean isConsecutive(int[] nums) {
            int mi = nums[0];
            int mx = nums[0];
            Set<Integer> s = new HashSet<>();
            for (int v : nums) {
                mi = Math.min(mi, v);
                mx = Math.max(mx, v);
                s.add(v);
            }
            int n = nums.length;
            return s.size() == n && mx == mi + n - 1;
        }
    }
    
  • func isConsecutive(nums []int) bool {
    	s := make(map[int]bool)
    	mi, mx := nums[0], nums[0]
    	for _, v := range nums {
    		s[v] = true
    		mi = min(mi, v)
    		mx = max(mx, v)
    	}
    	return len(s) == len(nums) && mx == mi+len(nums)-1
    }
    
    func max(a, b int) int {
    	if a > b {
    		return a
    	}
    	return b
    }
    
    func min(a, b int) int {
    	if a < b {
    		return a
    	}
    	return b
    }
    

Discuss

https://leetcode.com/problems/check-if-an-array-is-consecutive/discuss/1943451/

All Problems

All Solutions