The input begins with the number t of test cases in a single line (t<=10)
. In each of the next t lines there are two or more numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000)
separated by a space.
Print Each number in Separate line which can be used further for summation
Input
2
50 100
100 50 105
Output
50
100
100
50
105
Now this is the code that i've written that is Giving me Output
import java.util.Scanner;
import java.util.StringTokenizer;
public class Generation {
public static void main(String[] str) {
Scanner keyboard = new Scanner(System.in);
int inputSize;
do {
System.out.println("Enter the value of T Size");
inputSize = keyboard.nextInt();
keyboard.nextLine();
if (inputSize < 2 || inputSize > 10) {
System.out.println("Not a Valid Input Size");
}
} while (inputSize < 2 || inputSize > 10);
String[] inputValue = new String[inputSize];
int tokenCount = 0;
for (int i = 0; i < inputSize; i++) {
System.out.println("Enter the inputs");
inputValue[i] = keyboard.nextLine();
StringTokenizer strToken = new StringTokenizer(inputValue[i], " ");
tokenCount += strToken.countTokens();
}
keyboard.close();
//suppose this is 2nd part
int[] splitedString = new int[tokenCount];
int tempTokenCount = 0;
for (int i = 0; i < inputSize; i++) {
String[] tempSplitArray = inputValue[i].split(" ");
for (int j = 0; j < tempSplitArray.length; j++) {
splitedString[tempTokenCount] = Integer
.parseInt(tempSplitArray[j]);
tempTokenCount++;
}
}
/*for (String s : inputValue) {
System.out.println(s);
}*/
for (Integer s : splitedString) {
System.out.println(s);
}
}
}
Now my question is how can i optimize the 2nd part where i have to use two for loop which result in O(npower2)
time complexity. What is the workaround for such situations ?