I have a list of number which is sorted and want to print the continuous numbers range and if any number is missing in the series, we will start a new range. Note: There are possibility that multiple numbers are missing in the series continuously. example:
Input:
list = {301, 302, 303, 304, 305, 307, 308, 310, 312, 318, 319, 322, 390}
Output: range
301 - 305 //306 is missing so it breaks and new range starts
307 - 308 //309 is missing so breaks
310 // 311 is missing so breaks
312 // 313, 314, 315, 316, 317 are missing break (1 or more number can be missed)
318 - 319
322
390
My Logic:
int count = list.get(0);
String output = list.get(0) + "-";
for(int n: list){
if(count++ != n){
output = output+(count-2);
System.out.println(output);
output = n+"-";
count++;
}
}
It's failing, if there are more then 2 numbers is missing in between.