optimization - Possible Workaround for Optimal solution for split function in java while reading multiple lines? -


the input begins number t of test cases in single line (t<=10). in each of next t lines there 2 or more numbers m , n (1 <= m <= n <= 1000000000, n-m<=100000) separated space.

print each number in separate line can used further summation

input

2  50 100  100 50 105 

output

50  100  100  50  105 

now code i've written 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;         {             system.out.println("enter value of t size");             inputsize = keyboard.nextint();             keyboard.nextline();             if (inputsize < 2 || inputsize > 10) {                 system.out.println("not valid input size");             }         } while (inputsize < 2 || inputsize > 10);          string[] inputvalue = new string[inputsize];         int tokencount = 0;         (int = 0; < inputsize; i++) {             system.out.println("enter inputs");             inputvalue[i] = keyboard.nextline();             stringtokenizer strtoken = new stringtokenizer(inputvalue[i], " ");             tokencount += strtoken.counttokens();         }         keyboard.close();          //suppose 2nd part          int[] splitedstring = new int[tokencount];         int temptokencount = 0;         (int = 0; < inputsize; i++) {             string[] tempsplitarray = inputvalue[i].split(" ");             (int j = 0; j < tempsplitarray.length; j++) {                 splitedstring[temptokencount] = integer                         .parseint(tempsplitarray[j]);                 temptokencount++;             }          }         /*for (string s : inputvalue) {             system.out.println(s);         }*/         (integer s : splitedstring) {             system.out.println(s);         }      }  } 

now question how can optimize 2nd part have use 2 loop result in o(npower2) time complexity. workaround such situations ?

you concerned performance, , see have knowledge of issues (taking power time complexity) think don't understand means.

just because program has nested loops, doesn't mean (much) less efficient 1 single loop. outer loop iterates on each line , inner loop iterates on tokens (which vary in number) total number of iterations total number of tokens , has nothing power law.

the main problems performance have code simple thing , using temporary arrays, scanners, parsers add overhead of it. can read file containing 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 {                 = keyboard.nextint();                 system.out.println(i);             } catch(exception e) {                 // whatever                   system.err.println(e.getmessage());                 system.exit(1);             }         }     } } 

this of class , in addition catches exceptions. thing doesn't read initial count of lines. actually, counting lines hard in scanner doesn't support it, maybe can without or have solution.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -