I came across this code. It scans through the array elements only once. But I am confused regarding having two nested while loops increase the complexity to O(n^2). The code is as follows:
def summaryRanges(nums):
x, size = 0, len(nums)
ans = []
while x < size:
c, r = x, str(nums[x])
while x + 1 < size and nums[x + 1] - nums[x] == 1:
x += 1
if x > c:
r += "->" + str(nums[x])
ans.append(r)
x += 1
return ans
I am learning algorithms so please correct me if I am going wrong somewhere. Thank you!!