We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Another Java solution, building up a test sequence string for each possible starting number and then comparing to the original.
publicstaticvoidmain(String[]args){Scannerin=newScanner(System.in);intq=in.nextInt();for(inta0=0;a0<q;a0++){Strings=in.next();booleanvalid=false;longfirstx=-1;// Try each possible starting numberfor(inti=1;i<=s.length()/2;++i){longx=Long.parseLong(s.substring(0,i));firstx=x;// Build up sequence starting with this numberStringtest=Long.toString(x);while(test.length()<s.length()){test+=Long.toString(++x);}// Compare to originalif(test.equals(s)){valid=true;break;}}System.out.println(valid?"YES "+firstx:"NO");}}
Separate the Numbers
You are viewing a single comment's thread. Return to all comments →
Another Java solution, building up a test sequence string for each possible starting number and then comparing to the original.