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.
voidseparateNumbers(strings){longlongnumber,next;stringnew_string;boolpossible=false;// try to build the beautiful string starting with the first number, if it's not possible increase the number of digits and try againfor(intdigits=1;digits<=s.size()/2;digits++){// get the first number with the current number of digits from the string given// also keep the number for later to print if it's goodnumber=stoll(s.substr(0,digits));// get the next number, for beautiful conditionnext=number+1;// concatenate numbers with the next beautiful number until the minimum length is achievednew_string=s.substr(0,digits)+to_string(next);while(new_string.size()<s.size()){next++;new_string+=to_string(next);}// if the length is equal and the number is the same, it's possibleif(new_string.size()==s.size()&&new_string==s){possible=true;// early exit, no need to continue checkingbreak;}}// print resultif(possible){cout<<"YES "<<number<<endl;}else{cout<<"NO"<<endl;}}
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 →
c++