-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_228_SummaryRanges.cpp
57 lines (47 loc) · 1.38 KB
/
_228_SummaryRanges.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Source - https://leetcode.com/problems/summary-ranges/
Author - Shivam Arora
*/
#include <bits/stdc++.h>
using namespace std;
vector<string> summaryRanges(vector<int>& nums) {
int n = nums.size();
if(n == 0) {
vector<string> result;
return result;
}
string rangeString = to_string(nums[0]);
int rangeLength = 1;
vector<string> result;
for(int i = 1; i < n; i++) {
if(nums[i] == nums[i - 1] + 1)
rangeLength += 1;
else {
if(rangeLength > 1) {
result.push_back(rangeString + "->" + to_string(nums[i - rangeLength] + (rangeLength - 1)));
} else
result.push_back(rangeString);
rangeString = to_string(nums[i]);
rangeLength = 1;
}
}
if(rangeLength > 1)
result.push_back(rangeString + "->" + to_string(nums[n - rangeLength] + (rangeLength - 1)));
else
result.push_back(rangeString);
return result;
}
int main()
{
int n;
cout<<"Enter the length of the array: ";
cin>>n;
vector<int> nums(n);
cout<<"Enter the elements: ";
for(int i = 0; i < n; i++)
cin>>nums[i];
vector<string> result = summaryRanges(nums);
cout<<"Summary ranges: ";
for(int i = 0; i < result.size() - 1; i++)
cout<<result[i]<<", ";
cout<<result[result.size() - 1]<<endl;
}