STAYIN - Editorial

PROBLEM LINK: STAYIN Problem - CodeChef

Practice
Contest

Author: Shreyansh Singh
Tester: Shreyansh Singh
Editorialist: Shreyansh Singh

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Implementation, Math

PROBLEM:

Given a non-negative integer, find it’s second smallest factor (factors exclude 1), largest digit from unit place, and increase all even digits by 1.

QUICK EXPLANATION:

Find second number that divides the given number, iterate over the digits (continuously applying modulus on number) and find the largest digit, iterate over digits and construct new number with even digits increased by 1.

EXPLANATION:

Iterate over a range starting from 2 and ending at the given number, find second number that divides the given number, and print it.
For the largest digit from unit place, use a loop (ideally a while loop) that finds the last digit by using % operator and divides the remaining number by 10 to get rid of the last digit.
For constructing the new number, again iterate over the digits, find last digit, stick it to the new number by finding the last digit with modulo operator, multiplying the new number by 10 and adding the last digit. Note that this will give you the answer in the form with digits reversed, and you need to reverse it to get the answer.

SOLUTIONS:

Setter's Solution

#include <bits/stdc++.h>

using namespace std;

int ssf(int n) {
int count = 0;
for(int i = 2; i <= n; ++i) {
if(n % i == 0)
++count;
if(count == 2)
return i;
}
return -1;
}

int loc(int n) {
int m, curr_pos = 0, max_pos, max = INT_MIN;
while(n) {
++curr_pos;
m = n % 10;
if(m > max) {
max = m;
max_pos = curr_pos;
}
n /= 10;
}
return max_pos;
}

int reverse(int n) {
int ans = 0, m;
while(n) {
m = n % 10;
ans *= 10;
ans += m;
n /= 10;
}
return ans;
}

int inc(int n) {
int m, ans = 0, d;
while(n) {
m = n % 10;
if(m % 2 == 0)
d = m + 1;
else
d = m;
ans *= 10;
ans += d;
n /= 10;
}
return reverse(ans);
}

int main() {
int t;
cin >> t;
while(t–) {
int n;
cin >> n;
cout << ssf(n) << " " << loc(n) << " " << inc(n) << “\n”;
}
return 0;
}