Dsa
Math
Medium
Minimal Distance to Nearest Multiple
Given an integer N and another integer M, calculate the minimal distance from N to the nearest multiple of M.
Input:
Two integers N and M (-10^9 <= N, M <= 10^9 and M != 0).
Output:
An integer representing the minimal distance to the nearest multiple of M.
Example:
Input:
10 4
Output:
2
Explanation: The multiples of 4 around 10 are 8 and 12. The minimal distance to the nearest multiple is 2 (either 8 or 12).
Key concepts
mathmodulusdistance
Practise this out loud — free
Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.
🎙 Practise this question now