• + 0 comments

    Specially for the PSIT Students :)

    Here’s a brief of the “Jim and the Jokes” problem from HackerRank:

    1. Jim tells programmer jokes based on dates: a joke works if two dates (month, day) can be written as numbers in different bases so that they’re numerically equal.

    The classic example:** why do programmers mix up Christmas and Halloween? Dec 25 (in decimal, 25) = Oct 31 (in octal, 31).**

    Problem Statement Highlights:

    1. You’re given a list of special event dates (each date is a (month, day) pair).

    2. For each date, treat the month as the base and the day as the number: “day (in base month).”

    3. Find all distinct pairs of dates such that converting the day to decimal using the month's value as the base gives the same result for both dates.

    Extra Details & Constraints:

    • All numbers will use only the digits 0-9 (so bases above 10 can’t have normal digits).

    • If a day has a digit greater than or equal to the month, it’s invalid in that base, so skip that possibility.

    • You need to count all distinct pairs: if the same date appears multiple times, count all unique pairs among them (e.g., if the same pair appears 3 times, 3C2 = 3 possible jokes).

    • No need to worry about leap days.

    Here is the simple solution in CPP :

    long long check_valid(long long n, int base) {
        long long ans = 0;
        long long d = 1;
        while (n) {
            int rem = n % 10;
            if (rem >= base) return -1; // invalid digit for base
            ans += d * rem;
            d *= base;
            n /= 10;
        }
        return ans;
    }
    
    long long solve(vector<vector<int>> dates) {
        unordered_map<long long, long long> freq; // decimal value -> frequency
    
        for (auto &d : dates) {
            int base = d[0];
            long long val = d[1];
    
            if (base <= 1) continue; // skip invalid base
    
            long long res = check_valid(val, base);
            if (res == -1) continue; // invalid in that base
    
            freq[res]++;
        }
    
        long long count = 0;
        for (auto &p : freq) {
            long long n = p.second;
            if (n > 1) count += (n * (n - 1)) / 2; // nC2
        }
        return count;
    }
    

    Input: n lines of (month, day) Output: Single integer: total number of valid jokes possible with the event list.