0

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 ?

skaffman
  • 381,978
  • 94
  • 789
  • 754
Ankur Anand
  • 3,699
  • 1
  • 21
  • 39

1 Answers1

0

You are concerned with performance, and I see you have some knowledge of the issues (taking about power time complexity) but I think you don't really fully understand what this means.

Just because your program has nested loops, doesn't mean it is (much) less efficient than one with a single loop. The outer loop iterates over each line and the inner loop iterates over the tokens (which vary in number) so the total number of iterations is the total number of tokens and has nothing to do with any power law.

The main problems with performance is that you simply have too much code to do a very simple thing and you are using temporary arrays, scanners, parsers which will all add to the overhead of it. You can read a file containing its with the following code:

import java.util.Scanner;

public class Scan {

    public static void main(String args[]) {

        Scanner keyboard = new Scanner(System.in);

        int i;

        while (keyboard.hasNext()) {
            try {
                i = keyboard.nextInt();
                System.out.println(i);
            } catch(Exception e) {
                // Whatever  
                System.err.println(e.getMessage());
                System.exit(1);
            }
        }
    }
}

This will do most of what your class does and in addition catches exceptions. The only thing it doesn't do is read the initial count of lines. Actually, counting lines is hard in the scanner as it doesn't really support it, but maybe you can do without that or have another solution.

rghome
  • 7,212
  • 8
  • 34
  • 53