A course consists of numChapters distinct chapters numbered from 0 to numChapters - 1.
The chapters are taught sequentially, one chapter per day. After the last chapter is taught, the course starts again from chapter 0.
Given that a student was absent from day firstDay to day lastDay, both inclusive, determine the number of distinct chapters the student missed.
numChapters — number of distinct chapters.firstDay — first day the student was absent.lastDay — last day the student was absent.The chapter taught on day d is:
(d % numChapters)
Return the number of distinct chapters taught between firstDay and lastDay, inclusive.
Input:
numChapters = 4
firstDay = 3
lastDay = 5
Chapters taught:
Day: 0 1 2 3 4 5 6 7
Chapter: 0 1 2 3 0 1 2 3
The student missed:
Day 3 → Chapter 3
Day 4 → Chapter 0
Day 5 → Chapter 1
Distinct chapters missed:
{0, 1, 3}
Therefore:
Answer = 3
Input:
numChapters = 4
firstDay = 1
lastDay = 6
Chapters:
1, 2, 3, 0, 1, 2
Distinct chapters:
{0, 1, 2, 3}
Answer = 4
1 ≤ numChapters ≤ 10^9
0 ≤ firstDay ≤ lastDay ≤ 10^18
Goal: Find an efficient solution without iterating through every day.
Willingness • Pending
Willingness • Pending
Willingness • Pending
VISA • Pending