We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Here’s a brief of the “Jim and the Jokes” problem from HackerRank:
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:
You’re given a list of special event dates (each date is a (month, day) pair).
For each date, treat the month as the base and the day as the number: “day (in base month).”
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 :
longlongcheck_valid(longlongn,intbase){longlongans=0;longlongd=1;while(n){intrem=n%10;if(rem>=base)return-1;// invalid digit for baseans+=d*rem;d*=base;n/=10;}returnans;}longlongsolve(vector<vector<int>>dates){unordered_map<longlong,longlong>freq;// decimal value -> frequencyfor(auto&d:dates){intbase=d[0];longlongval=d[1];if(base<=1)continue;// skip invalid baselonglongres=check_valid(val,base);if(res==-1)continue;// invalid in that basefreq[res]++;}longlongcount=0;for(auto&p:freq){longlongn=p.second;if(n>1)count+=(n*(n-1))/2;// nC2}returncount;}
Input: n lines of (month, day) Output: Single integer: total number of valid jokes possible with the event list.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Jim and the Jokes
You are viewing a single comment's thread. Return to all comments →
Specially for the PSIT Students :)
Here’s a brief of the “Jim and the Jokes” problem from HackerRank:
The classic example:** why do programmers mix up Christmas and Halloween? Dec 25 (in decimal, 25) = Oct 31 (in octal, 31).**
Problem Statement Highlights:
You’re given a list of special event dates (each date is a (month, day) pair).
For each date, treat the month as the base and the day as the number: “day (in base month).”
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 :
Input: n lines of (month, day) Output: Single integer: total number of valid jokes possible with the event list.