Skip to content

ORIGINAL CODING QUESTION

Closest pair of reviewer ratings

Several reviewers each rated the same resume. Return the smallest absolute difference between the ratings of two different reviewers (two different positions). Equal ratings give a difference of 0.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

First integer n, then n integers. Print one integer.

Constraints

2 ≤ n ≤ 200; -1000 ≤ rating ≤ 1000.

Example 1

Input

5
40 12 29 33 18

Output

4

Sorted, the ratings are 12 18 29 33 40; the closest neighbours are 29 and 33.

Example 2

Input

3
8 15 8

Output

0

Two reviewers gave the same rating.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Checking every pair works, but sorting reveals structure.

Hint 2

After sorting, the closest value to any rating sits right next to it.

Hint 3

Sort the ratings and take the minimum difference between adjacent elements.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →