ORIGINAL CODING QUESTION
Reminder days for two habits
One habit reminder fires on days a, 2a, 3a and so on, and another fires on days b, 2b, 3b and so on. Count the days from 1 to x inclusive on which at least one reminder fires. A day when both fire counts once.
Original exercise · easy. Editorial role relevance does not imply a company has asked this question.
Input and output
Three integers x, a and b. Print the day count.
Constraints
1 ≤ x ≤ 100,000; 1 ≤ a, b ≤ 1000.
Example 1
Input
10 2 3
Output
7
Days 2, 3, 4, 6, 8, 9 and 10 have a reminder.
Example 2
Input
20 4 4
Output
5
Both habits share days 4, 8, 12, 16 and 20, each counted once.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
A loop over every day works, but there is a direct formula.
Hint 2
There are floor(x / a) multiples of a up to x; days divisible by both were counted twice.
Hint 3
Answer = floor(x/a) + floor(x/b) - floor(x/L), where L is the least common multiple a * b / gcd(a, b).
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →