Lv.1arraysorting
Closest Pair Gap
- Memory limit
- 256 MB
- Correctness time limit
- 10000 ms
- Solvers
- 0
Given an integer array nums, return the minimum of nums[i] nums[j] over all pairs of different positions.
Values range from to , so the difference may exceed the 32-bit range.
Examples
| nums | result |
|---|---|
[4, 9, 1, 32, 13] | 3 |
[10, -5] | 15 |
[7, 3, 7] | 0 |
Efficiency
This problem has performance tests. Judge the required time complexity from the input sizes in the constraints.
Function signature
solution(nums: int[]) → long
Constraints
-
nums.length -
nums[i]
Expected complexity
- Time
- O(N log N)
- Space
- O(1)
Examples
| nums | Expected |
|---|---|
[4,9,1,32,13] | 3 |
[10,-5] | 15 |
[7,3,7] | 0 |
Hint
Hint
After sorting, the closest pair is always adjacent (if a < b < c then c - a > b - a), so check neighbouring pairs
only: . Compute differences in 64 bits; overflows an int. Comparing all pairs is and
exceeds the time limit.
Time limits per language
| Language | Efficiency time limit | |
|---|---|---|
| C (GCC, gnu17) | 200 ms | estimated |
| C++ (GCC, gnu++23) | 200 ms | derived |
| Python 3.13 | 1000 ms | derived |
| Java 25 | 1316 ms | derived |
| Kotlin 2.4 (JVM) | 1400 ms | estimated |
| C# 14 (.NET 10) | 1400 ms | estimated |
Test groups
| Group | Weight | Tests | Visibility |
|---|---|---|---|
| sample | 0 | 3 | inputs and outputs |
| correctness | 40 | 10 | verdict only |
| performance | 60 | 3 | verdict only |