Bloomberg | Phone | Find all ranges of consecutive numbers from Array

Difficulty: EASYCategory: InterviewExperienceFrequency: 1Asked On 2021-01-08Added By Anonymous
Source: Link
Company: Bloomberg
Role: unknown
Interview Stage: Phone
Location: unknown
Year: unknown

Original Experience:
Given a sorted array arr[] consisting of N integers without any duplicates, the task is to find the ranges of consecutive numbers from that array. Examples: Input: arr[] = {1, 2, 3, 6, 7}
Output: 1->3, 6->7
Explanation:
There are two ranges of consecutive number from that array.
Range 1 = 1 -> 3
Range 2 = 6 -> 7 Input: arr[] = {-1, 0, 1, 2, 5, 6, 8}
Output: -1->2, 5->6, 8
Explanation:
There are three ranges of consecutive number from that array.
Range 1 = -1 -> 2
Range 2 = 5 -> 6
Range 3 = 8
Comments