mirror of
				https://github.com/theoludwig/programming-challenges.git
				synced 2025-09-11 23:11:21 +02:00 
			
		
		
		
	feat(challenges): add find-closest-number
				
					
				
			This commit is contained in:
		
							
								
								
									
										73
									
								
								challenges/find-closest-number/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								challenges/find-closest-number/README.md
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,73 @@
 | 
			
		||||
# find-closest-number
 | 
			
		||||
 | 
			
		||||
Created by [@Divlo](https://github.com/Divlo) on 1 May 2022.
 | 
			
		||||
 | 
			
		||||
## 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
 | 
			
		||||
```
 | 
			
		||||
							
								
								
									
										0
									
								
								challenges/find-closest-number/solutions/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								challenges/find-closest-number/solutions/.gitkeep
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										8
									
								
								challenges/find-closest-number/test/1/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								challenges/find-closest-number/test/1/input.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
3
 | 
			
		||||
6
 | 
			
		||||
1
 | 
			
		||||
2
 | 
			
		||||
3
 | 
			
		||||
4
 | 
			
		||||
5
 | 
			
		||||
6
 | 
			
		||||
							
								
								
									
										1
									
								
								challenges/find-closest-number/test/1/output.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								challenges/find-closest-number/test/1/output.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
3
 | 
			
		||||
							
								
								
									
										16
									
								
								challenges/find-closest-number/test/2/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								challenges/find-closest-number/test/2/input.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
0
 | 
			
		||||
14
 | 
			
		||||
7
 | 
			
		||||
-10
 | 
			
		||||
13
 | 
			
		||||
8
 | 
			
		||||
4
 | 
			
		||||
-7
 | 
			
		||||
-12
 | 
			
		||||
-3
 | 
			
		||||
3
 | 
			
		||||
-9
 | 
			
		||||
6
 | 
			
		||||
-1
 | 
			
		||||
-6
 | 
			
		||||
7
 | 
			
		||||
							
								
								
									
										1
									
								
								challenges/find-closest-number/test/2/output.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								challenges/find-closest-number/test/2/output.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
-1
 | 
			
		||||
							
								
								
									
										12
									
								
								challenges/find-closest-number/test/3/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								challenges/find-closest-number/test/3/input.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
80
 | 
			
		||||
10
 | 
			
		||||
2
 | 
			
		||||
42
 | 
			
		||||
82
 | 
			
		||||
122
 | 
			
		||||
162
 | 
			
		||||
202
 | 
			
		||||
242
 | 
			
		||||
282
 | 
			
		||||
322
 | 
			
		||||
362
 | 
			
		||||
							
								
								
									
										1
									
								
								challenges/find-closest-number/test/3/output.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								challenges/find-closest-number/test/3/output.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
82
 | 
			
		||||
							
								
								
									
										10
									
								
								challenges/find-closest-number/test/4/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								challenges/find-closest-number/test/4/input.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,10 @@
 | 
			
		||||
11
 | 
			
		||||
8
 | 
			
		||||
1
 | 
			
		||||
2
 | 
			
		||||
4
 | 
			
		||||
5
 | 
			
		||||
6
 | 
			
		||||
6
 | 
			
		||||
8
 | 
			
		||||
9
 | 
			
		||||
							
								
								
									
										1
									
								
								challenges/find-closest-number/test/4/output.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								challenges/find-closest-number/test/4/output.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
9
 | 
			
		||||
							
								
								
									
										8
									
								
								challenges/find-closest-number/test/5/input.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								challenges/find-closest-number/test/5/input.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
4
 | 
			
		||||
6
 | 
			
		||||
2
 | 
			
		||||
5
 | 
			
		||||
6
 | 
			
		||||
7
 | 
			
		||||
8
 | 
			
		||||
9
 | 
			
		||||
							
								
								
									
										1
									
								
								challenges/find-closest-number/test/5/output.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								challenges/find-closest-number/test/5/output.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
			
		||||
5
 | 
			
		||||
		Reference in New Issue
	
	Block a user