Skip to content

Instantly share code, notes, and snippets.

@haridutt12
Created December 2, 2016 12:15
Show Gist options
  • Save haridutt12/8b25f36cb4bf2f326fcb684fcc471747 to your computer and use it in GitHub Desktop.
Save haridutt12/8b25f36cb4bf2f326fcb684fcc471747 to your computer and use it in GitHub Desktop.
/*Kristen loves playing with and comparing numbers. She thinks that if she takes two different positive numbers, the one whose digits sum to a larger number is better than the other. If the sum of digits is equal for both numbers, then she thinks the smaller number is better. For example, Kristen thinks that is better than and that is better than .
Given an integer, , can you find the divisor of that Kristin will consider to be the best?
Input Format
A single integer denoting .
Constraints
Output Format
Print an integer denoting the best divisor of .
Sample Input 0
12
Sample Output 0
6*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int n,i,q,temp,r,d;
cin>>n;
int a[100000];
int flag=0,flag1=0;
if(n==1)
cout<<"1";
else{
for(i=2;i<n;i++)
{
d=i;
if(n%i==0)
{
int t=0,r;
while(d>0)
{
r=d%10;
t+=r;
d=d/10;
}
flag++;
a[flag]=i;
a[flag++]=t;
flag1++;
}
}
if(flag1==0)
cout<<n;
else
{
int max=a[2];
for(int j=2;j<=flag;j+=2)
{
if(a[j]>max)
max=a[j];
}
//cout<<max;
int count=0;
for(int j=2;j<=flag;j+=2)
{
if(a[j]==max)
{ count++;
int q=j;
}
}
// cout<<q;
int b[100000],s=0;
if(count==1)
cout<<a[q-1];
else
{
for(int j=2;j<=flag;j+=2)
{
if(a[j]==max)
{
b[s++]=a[j-1];
}
}
int min=b[1];
for(int j=1;j<=s;j++)
{
if(a[j]<min)
min=a[j];
}
cout<<min;
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment