title: optimizing subarray max/min analysis in python
body: <p>I'm currently working on a hackerrank problem (found <a href="https://www.hackerrank.com/challenges/angry-children" rel="nofollow">here</a>). in this problem you have to take a given array and find the subarray of length N with the smallest max-min value. the page explains it a little better than I do. </p>
  
  <p>that being said, I've finished the problem and have a solution that I believe works in every case... but I keep on 'timing out' on a select few of the test cases they provide. I've included my code below: </p>
  
  <pre><code>def maxmin(array, original, new): 
      minimum = -1
      for i in range(0, original-new-1): 
          subarray = array[i:i+(new)]
          if minimum == -1: 
              minimum = max(subarray)-min(subarray)
          else: 
              if (max(subarray)-min(subarray)) &lt; minimum: 
                  minimum = max(subarray)-min(subarray)
      return minimum 
  
  
  n = input()
  k = input()
  candies = [input() for _ in range(0,n)]
  candies.sort()
  print maxmin(candies, n, k)
  </code></pre>
  
  <p>as I said above, the code works fine -- but apparently it's not 'fast' enough. I'm still <em>very</em> new to Python (as a quick review of my previous questions would suggest), so I'm not sure how to make this code more 'pythonic' or just faster, in general. any help in this regard would be extremely appreciated. </p>
  
  <p>thanks!</p>