Sorted Subsegments Hackerrank Solution