Index above range
Input
clamp_index(10, 4)
Output
3
The valid indices for length 4 are 0..3. 10 is clamped to 3.
Full lesson preview
Write a small utility that clamps an index to the valid range for a sequence length. Handle edge cases like zero or negative lengths.
Problem statement
Task
Examples
Input
clamp_index(10, 4)
Output
3
The valid indices for length 4 are 0..3. 10 is clamped to 3.
Input format
Output format
Constraints
Samples
Input
clamp_index(-2, 3)
Output
0
Negative indices are clamped to 0.