108k views
2 votes
Given a sorted integer array without duplicates, return the summary of its ranges.?

User Mikaela
by
6.6k points

1 Answer

5 votes

Final answer:

To summarize the ranges in a sorted integer array without duplicates, iterate through the array and compare adjacent elements. Whenever there is a gap of more than one between two consecutive elements, add the range to the summary.

Step-by-step explanation:

In a sorted integer array without duplicates, a range refers to a consecutive sequence of numbers. To find the summary of its ranges, you can iterate through the array and compare adjacent elements. Whenever there is a gap of more than one between two consecutive elements, it means a range has ended, and you can add the range to the summary. Here is an example:

Input: [1, 2, 3, 6, 8, 9]

Output: ['1->3', '6', '8->9']

User Davidlt
by
7.3k points