m, n, numbers = 4, 6, [10, 12, 10, 7, 4, 22] numbers.sort()i = min(range(n-m+1), key=lambda i: numbers[i+m-1] - numbers[i])print numbers[i:i+m]
Standard input is empty
[7, 10, 10, 12]
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!