FastPrepDistinct Chapters Missed During a Cyclic Absence

Distinct Chapters Missed During a Cyclic Absence

Visa logoVisa● EasyNEW GRADINTERNOA
Learn

Problem statement

A chemistry teacher teaches exactly one chapter per day, moving through a book in order. The book contains numChapters chapters numbered from 0 through numChapters - 1. On day i, where days are zero-indexed, the teacher covers chapter i % numChapters.

A student is absent on every day from firstDay through lastDay, inclusive. Return the number of distinct chapters taught during that absence.

Function

countDistinctChaptersMissed(numChapters: int, firstDay: int, lastDay: int) → int

Examples

Example 1

numChapters = 4firstDay = 3lastDay = 5return = 3

Days 3, 4, and 5 cover chapters 3, 0, and 1. The student misses three distinct chapters.

Example 2

numChapters = 3firstDay = 2lastDay = 8return = 3

The seven-day absence contains at least one complete three-chapter cycle, so every chapter is missed.

Constraints

  • 1 ≤ numChapters ≤ 10^9
  • 1 ≤ firstDay ≤ lastDay ≤ 10^9

Source note: The gallery contains two independent HackerRank captures of the cyclic chapter-absence task; the newer image preserves the complete visible example and constraints.

More Visa problems

See Visa hiring insights
public int countDistinctChaptersMissed(int numChapters, int firstDay, int lastDay) {
  // Write your code here.
}
numChapters4
firstDay3
lastDay5
expected3
Checking account…