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.
Here is my C++ solution and it's simple approach.**
#include<string>#include<iostream>#include<algorithm>// convert a string to a numberunsignedlonglongstr2num(conststd::string&x){// process string from left to rightunsignedlonglongresult=0;for(autoc:x){// shift digitsresult*=10;// add new digit on the right-hand sideresult+=c-'0';// was ASCII}returnresult;}intmain(){// available digitsstd::stringpan="0123456789";// unlike other problems, zero is allowed this time// remove a few digits if test case requires thisunsignedintmaxDigit;std::cin>>maxDigit;pan.erase(maxDigit+1);// all divisorsconstunsignedintprimes[]={2,3,5,7,11,13,17};// resultunsignedlonglongsum=0;// look at all permutationsdo{// let's assume it's a great number ;-)boolok=true;// check each 3-digit substring for divisibilityfor(unsignedinti=0;i+2<maxDigit;i++){// check pan[1..3] against primes[0],// check pan[2..4] against primes[1],// check pan[3..5] against primes[2] ...std::stringcheck=pan.substr(i+1,3);if(str2num(check)%primes[i]!=0){// nope ...ok=false;break;}}// passed all tests, it's great indeed !if(ok)sum+=str2num(pan);}while(std::next_permutation(pan.begin(),pan.end()));std::cout<<sum<<std::endl;return0;1.}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Project Euler #43: Sub-string divisibility
You are viewing a single comment's thread. Return to all comments →
Here is my C++ solution and it's simple approach.**