PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#BS0000154. Tìm k phần tử gần nhất (Find K Closest Elements)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Tìm k phần tử gần nhất (Find K Closest Elements)
Find K Closest Elements
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given a nondecreasing sequence of integers, an integer , and a target , select exactly array elements closest to . Elements are compared by absolute distance to ; if distances tie, the smaller value wins. Equal values at different indices count separately. Print the chosen values in nondecreasing order.
Input
- The first line contains .
- The second line contains values in nondecreasing order.
Output
Print exactly selected integer values, sorted in nondecreasing order.
Subtasks
- Subtask 1 (20 points): ; ; for all ; .
- Subtask 2 (30 points): ; ; for all ; .
- Subtask 3 (50 points): ; ; for all ; .
Examples
Example 1
Input
5 4 3
1 2 3 4 5
Output
1 2 3 4
Explanation
For , the distances of are . The three closest are . For the last place, both and have distance , but wins the smaller-value tie-break. Sorted output is 1 2 3 4.