PROBLEM LINK:
Practice
Div-1 Contest
Div-2 Contest
Div-3 Contest
Author: Daanish Mahajan
Testers: Felipe Mota and Radoslav Dimitrov
Editorialist: Vichitr Gandas
DIFFICULTY:
Cakewalk
PREREQUISITES:
None
PROBLEM:
Given an integer N. Find the largest integer between 1 and 10 (inclusive) which divides N.
EXPLANATION
Our task is to find the largest integer between 1 and 10 which divides N. So iterate over all integers between 1 and 10 and check if they divide N. Finally take maximum integer out of them who divides N.
Other possible way is to iterate from 10 to 1 and print first integer who divides N as that would be largest integer between 1 to 10 dividing N.
Time Complexity: \mathcal{O}(1) as we run a loop for constant (10) times only.
Space Complexity: \mathcal{O}(1) as no extra space is used.
SOLUTIONS:
Setter's Solution
# include<bits/stdc++.h>
using namespace std;
const int minv = 2, maxv = 1000;
int main()
{
int n, ans = 1; cin >> n;
for(int i = 10; i >= 1; i--){
if(n % i == 0){
ans = i; break;
}
}
cout << ans << endl;
}
Tester's Solution
#include <bits/stdc++.h>
using namespace std;
template<typename T = int> vector<T> create(size_t n){ return vector<T>(n); }
template<typename T, typename... Args> auto create(size_t n, Args... args){ return vector<decltype(create<T>(args...))>(n, create<T>(args...)); }
long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);
assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}
assert(l<=x && x<=r);
return x;
} else {
assert(false);
}
}
}
string readString(int l,int r,char endd){
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
return readInt(l,r,'\n');
}
string readStringLn(int l,int r){
return readString(l,r,'\n');
}
string readStringSp(int l,int r){
return readString(l,r,' ');
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n = readIntLn(1, 1000);
for(int i = 10; i >= 1; i--){
if(n % i == 0){
cout << i << '\n';
return 0;
}
}
return 0;
}
Editorialist's Solution
/***************************************************
@author: vichitr
Compiled On: 06 Feb 2021
*****************************************************/
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n; cin>>n;
for(int i=10;i>=1;i--){
if(n%i == 0){
cout<<i;
break;
}
}
}
signed main()
{
int t=1;
// cin >>t;
for(int i=1;i<=t;i++)
{
solve();
}
return 0;
}