FastPrepConsecutive Number Ranges

Consecutive Number Ranges

Microsoft logoMicrosoft● MediumNEW GRADPHONE SCREEN
Learn

Problem statement

Given an integer array nums, return the maximal ranges of consecutive integer values present in the array.

Ignore duplicate values. Represent each range as [start,end], including singleton ranges where start = end. Return ranges in increasing order by start.

Function

consecutiveRanges(nums: int[]) → int[][]

Examples

Example 1

nums = [100,4,200,1,3,2,2]return = [[1,4],[100,100],[200,200]]

The distinct values 1,2,3,4 form one maximal range; 100 and 200 are singleton ranges.

Example 2

nums = []return = []

An empty array contains no consecutive-value ranges.

Constraints

  • 0 <= nums.length <= 100000
  • -10^9 <= nums[i] <= 10^9

More Microsoft problems

See Microsoft hiring insights
public int[][] consecutiveRanges(int[] nums) {
  // Write your code here.
}
nums[100,4,200,1,3,2,2]
expected[[1,4],[100,100],[200,200]]
Checking account…