//input numbers are duplicate and not in sequence and you have to eliminate duplicate and print in sequence
//input - 1,2,3,4,5-10,6-14,11-25,20-30,13-16
//output - 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
import java.util.*;
class EliminateDuplicateAndExpandNumbersInSequence
{
public static void main(String[] args) {
printNumbers(args[0]);
}
static String[] split(String input)
{
return input.split("[,-]");
}
static void printNumbers(String data)
{
String inputs[] = split(data);
TreeSet<Integer> set = new TreeSet<Integer>();
for(String s : inputs )
set.add(Integer.parseInt(s));
int num1 = set.first();
int num2 = set.last();
for(int i = num1 ; i <= num2 ; i++)
{
if(i == num2)
System.out.println(i);
else
System.out.print(i + ",");
}
}
}
//input - 1,2,3,4,5-10,6-14,11-25,20-30,13-16
//output - 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
import java.util.*;
class EliminateDuplicateAndExpandNumbersInSequence
{
public static void main(String[] args) {
printNumbers(args[0]);
}
static String[] split(String input)
{
return input.split("[,-]");
}
static void printNumbers(String data)
{
String inputs[] = split(data);
TreeSet<Integer> set = new TreeSet<Integer>();
for(String s : inputs )
set.add(Integer.parseInt(s));
int num1 = set.first();
int num2 = set.last();
for(int i = num1 ; i <= num2 ; i++)
{
if(i == num2)
System.out.println(i);
else
System.out.print(i + ",");
}
}
}
No comments:
Post a Comment