-
-
Notifications
You must be signed in to change notification settings - Fork 362
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7cbc6c
commit 4537203
Showing
1 changed file
with
19 additions
and
0 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
Divide and Conquer/Find Closest Number in Array/SolutionByDhaval.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
arr_in = input()[9:] | ||
arr_in = arr_in[:-1] | ||
|
||
arr_in = arr_in.replace(" ", "") | ||
arr = list(map(int, arr_in.split(","))) | ||
|
||
target_num = input().replace(" ", "") | ||
num = int(target_num.split("=")[1]) | ||
|
||
smallest_diff = abs(arr[0]-num) | ||
closest_num = 0 | ||
|
||
for index in arr: | ||
diff = abs(index-num) | ||
if diff < smallest_diff: | ||
smallest_diff = diff | ||
closest_num = index | ||
|
||
print(closest_num) |