title: Determine whether any permutation of a given array such that the sum of all subarrays of length K are equal
body: <p>I was asked this question in an interview.  </p>
  
  <blockquote>
    <p>You are given an array consisting of N integers. You need to determine whether any permutation of a given array exists such that the sum of all subarrays of length K are equal. N is divisible by K i.e. N mod K = 0.<br>
    If the array is [1,2,1,3,2,3] and K = 2, it is False.<br>
    If the array is [1,2,1,3,2,3] and K = 3, it is True.  </p>
  </blockquote>
  
  <p>My logic was this.  </p>
  
  <blockquote>
    <p>1) If N == K, return True.<br>
    2) Else store each of the distinct element in the array in a dictionary. If the number of distinct elements in the dictionary is greater than K, return False.<br>
    3) Else store the count of each distinct element in the dictionary.<br>
    4) If the count of any distinct element is less than (N/K), return False.<br>
    5) Finally return True.</p>
  </blockquote>
  
  <p>Here is my code in Python:  </p>
  
  <pre><code>def check(nums, n, k):
  if n == k:
      return True
  else:
      my_dict = {}
      for i in nums:
          if i not in my_dict:
              my_dict[i] = 1
          else:
              my_dict[i] += 1
      if len(my_dict) &gt; k:
          return False
      count = int(n/k)
      for i,j in my_dict.items():
          if j &lt; count:
              return False
      return True  
  </code></pre>
  
  <p>Am I doing the right approach ? Is there a better way to do it ?</p>