1
// Time Complexity : O(N)
2
// Space Complexity : O(1)
3
var summaryRanges = function (nums) {
4
// Create a list of string to store the output result...
5
const output = [];
6
// Start traversing the array from idx = 0 till idx < sizeofarray in a while loop.
7
let idx = 0;
8
while (idx < nums.length) {
9
// Initialize beg and last index for identifying the continuous element in the array...
10
let beg, last;
11
// Mark the number at current index as beginning element of the range...
12
beg = nums[idx];
13
// Traverse the array beggining from current index & find the last element whose difference from previous element is exactly 1, i.e. nums[idx + 1] == nums[idx] + 1...
14
while (idx + 1 < nums.length && nums[idx + 1] == nums[idx] + 1) idx++;
15
// Set this element as last element of the range...
16
last = nums[idx];
17
// If continuous element isn't present...
18
if (beg == last) output.push(beg + "");
19
// If present...
20
else output.push(beg + "->" + last);
21
idx++;
22
}
23
return output; // Return the output result list...
24
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0