2022-05-01 19:05:41 +02:00
|
|
|
# find-closest-number
|
|
|
|
|
2023-07-02 17:28:54 +02:00
|
|
|
Created by [@theoludwig](https://github.com/theoludwig) on 1 May 2022.
|
2022-05-01 19:05:41 +02:00
|
|
|
|
|
|
|
## Instructions
|
|
|
|
|
|
|
|
Given an array of `n` integers, find the closest value to the given number (`given_number`).
|
|
|
|
|
|
|
|
## Input
|
|
|
|
|
|
|
|
- **Line 1:** An integer `given_number` for the number to find the closest value to
|
|
|
|
- **Line 2:** An integer `n` for the length of the list of integers
|
|
|
|
- **`n` next lines:** the integers
|
|
|
|
|
|
|
|
## Output
|
|
|
|
|
|
|
|
The closest value in the array to the given number.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
See the `test` folder for examples of input/output.
|
|
|
|
|
|
|
|
### Example 1
|
|
|
|
|
|
|
|
#### Input
|
|
|
|
|
|
|
|
```txt
|
|
|
|
3
|
|
|
|
6
|
|
|
|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Output
|
|
|
|
|
|
|
|
```txt
|
|
|
|
3
|
|
|
|
```
|
|
|
|
|
|
|
|
**Explanation:** The given number is `3` and `3` is in the array, so the closest value is `3`.
|
|
|
|
|
|
|
|
### Example 2
|
|
|
|
|
|
|
|
#### Input
|
|
|
|
|
|
|
|
```txt
|
|
|
|
0
|
|
|
|
14
|
|
|
|
7
|
|
|
|
-10
|
|
|
|
13
|
|
|
|
8
|
|
|
|
4
|
|
|
|
-7
|
|
|
|
-12
|
|
|
|
-3
|
|
|
|
3
|
|
|
|
-9
|
|
|
|
6
|
|
|
|
-1
|
|
|
|
-6
|
|
|
|
7
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Output
|
|
|
|
|
|
|
|
```txt
|
|
|
|
-1
|
|
|
|
```
|