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.
vector<int>divisors;for(inti=1;i<=n;i++){if(n%i==0)divisors.push_back(i);// if i divides n, it is a divisor}vector<pair<int,int>>digital_sum;for(autoj:divisors){// calculate digital_sum of each j and store the divisor and sum in a pair, which is pushed back into digital_sum vector intsum=0;inttemp=j;while(temp!=0){sum+=temp%10;temp=temp/10;}pair<int,int>temp1;temp1.first=j;temp1.second=sum;digital_sum.push_back(temp1);}intbest_divisor=1,best_sum=1;//check and keep updating best_divisor and best_sumfor(autok:digital_sum){if((k.second>best_sum)||(k.second>=best_sum&&k.first<best_divisor)){best_divisor=k.first;best_sum=k.second;}}cout<<best_divisor;
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Best Divisor
You are viewing a single comment's thread. Return to all comments →
C++