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.
#include<iostream>#include<string>usingnamespacestd;intmain(){// Declarations.intn;strings;cin>>n;// For each querywhile(n--){cin>>s;boolvalid=false;longfirstx=-1;// Try each possible starting numberfor(inti=1;i<=s.length()/2;++i){longx=stol(s.substr(0,i));firstx=x;// Build up sequence starting with this numberstringtest=to_string(x);while(test.length()<s.length()){test+=to_string(++x);}// Compare to originalif(test==s){valid=true;break;}}valid?cout<<"YES "<<firstx<<endl:cout<<"NO"<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Separate the Numbers
You are viewing a single comment's thread. Return to all comments →
Here's a C++ version of johnpearson's algorithm: