EDITORIAL - Help Moss Head

PROBLEM

Help Moss Head

PROBLEM LINK:

https://www.codechef.com/STRM2021/problems/STRAWHATS

Author: Venkatesh Dhongadi
Tester: Venkatesh Dhongadi
Editorialist: Venkatesh Dhongadi

DIFFICULTY:

Easy

PREREQUISITES:

None.

PROBLEM:

The Straw hat pirates are as usual sailing in the open sea. Moss head Zoro is hungry and insists the chef Sanji to cook something delicious. Sanji will cook food only if Zoro wins the game of finding Devil numbers created by the Sniper King Usop. In this game Zoro is supposed to determine the difference between the number of Marines (M) that their captain Luffy has defeated and the reverse of that number M.
Usop tells Zoro to count the number of Marines ( M ) Luffy has defeated.
Consider, Luffy kicks off 69 marines, it’s reverse is 96. Their difference is 27 (69 - 96 = 27). Similarly, if Luffy defeats 100 marines, the reverse will be 1 and their difference is 99 (100 - 1 = 99).
Usop gives moss head a range of number of marines [ i ... j ], that their captain Luffy has defeated. And another number X.
Now a Devil Number is defined as a number where | i - reverse(i) | is evenly divisible by a number X.
Can you help the world famous pirate hunter Zoro ?

EXPLANATION:

Simple brute-force is enough to solve this problem. Run a loop from a to b and for each number x check if : abs(x - rev(x)) % k == 0

Setter's Solution
#include <iostream>
  
using namespace std;

bool isOk(int x, int mod) {
    int n = x;
    int m = 0;
    while (x > 0) {  //simple logic to reverse a number
        m = m * 10 + x % 10;
        x /= 10;
    }
    int delta = abs(n - m);  //finding the absolute value
    delta %= mod;
    return (delta == 0);
}

int main() {
    int l, r, k;
    cin >> l >> r >> k;
    int ans = 0;
    for (int i = l; i <= r; i++) {
        if (isOk(i, k)) {
            ++ans;
        }
    }
    cout << ans << endl;
    return 0;
}

Feel free to share your approach here. Suggestions are always welcomed. :slight_smile: