Created
August 27, 2019 20:42
-
-
Save wenweixu/77ec9a5d9512023de14ab9f4978546fa to your computer and use it in GitHub Desktop.
Hackerrank Max Min python solution
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
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
# Complete the maxMin function below. | |
def maxMin(k, arr): | |
arr.sort() | |
result = arr[k-1] - arr[0] | |
for i in range(n-k+1): | |
if arr[i+k-1] - arr[i] < result: | |
result = arr[i+k-1] - arr[i] | |
return result | |
if __name__ == '__main__': | |
fptr = open(os.environ['OUTPUT_PATH'], 'w') | |
n = int(input()) | |
k = int(input()) | |
arr = [] | |
for _ in range(n): | |
arr_item = int(input()) | |
arr.append(arr_item) | |
result = maxMin(k, arr) | |
fptr.write(str(result) + '\n') | |
fptr.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank so much