Count Prime-Numbered Lockers Under Inspection
A warehouse contains lockers numbered with positive integers. Several inspection zones specify inclusive ranges of locker numbers.
Count the different prime-numbered lockers covered by at least one inspection zone.
Count Prime-Numbered Lockers Under Inspection
Implement the following method:
int countInspectedPrimeLockers(List<String> inspectionZones)
- Each element in
inspectionZones is formatted as "firstLocker,lastLocker".
- It represents the inclusive range from
firstLocker through lastLocker.
- The method returns the number of different prime-numbered lockers covered by the given zones.
Rules
- A prime number is greater than
1 and has exactly two positive divisors: 1 and itself.
- Inspection zones may overlap.
- Both endpoints of every inspection zone are included.
- A locker covered by multiple zones is counted only once.
Constraints
1 ≤ inspectionZones.size() ≤ 100,000
1 ≤ firstLocker ≤ lastLocker ≤ 1,000,000
- Every element in
inspectionZones contains exactly one comma.
- Every element is formatted as
"firstLocker,lastLocker".
inspectionZones never contains null values.
Examples
Example 1
countInspectedPrimeLockers( inspectionZones = List.of("3,12", "20,25") )
Output: 5
The covered prime-numbered lockers are 3, 5, 7, 11, and 23.
Example 2
countInspectedPrimeLockers( inspectionZones = List.of("4,10", "7,15", "10,12") )
Output: 4
Together, the overlapping zones cover lockers from 4 through 15. The prime-numbered lockers are 5, 7, 11, and 13.
Example 3
countInspectedPrimeLockers( inspectionZones = List.of("24,28", "32,35") )
Output: 0
None of the covered locker numbers are prime.
Example 4
countInspectedPrimeLockers( inspectionZones = List.of("17,17", "15,19", "17,23") )
Output: 3
The covered prime-numbered lockers are 17, 19, and 23. Locker 17 is counted only once despite appearing in every zone.