Interview Questions/Coding/Minimum Range After Add Operations

Minimum Range After Add Operations

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Minimum Range After Add Operations

You are given an array of positive integers nums and a positive integer k. In one operation, you may choose any element and add k to it. You may perform this operation any number of times. Return the minimum possible value of max(nums) - min(nums) after the operations.

Examples

Example 1
Input:
1 5 9
4
Output:
0

Explanation: All numbers have the same remainder modulo 4, so they can be made equal.

Approach hint

Adding k does not change a number's remainder modulo k.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp