title: Time and Space Complexity of top k frequent elements in an array
body: <p>There is a small confusion regarding the time and space complexity for the given problem:</p>
  
  <p>Given an array of size <strong>N</strong> , return a list of top <strong>K</strong> frequent elements. </p>
  
  <p>Based on the most popular solution: </p>
  
  <ol>
  <li>Use a HashMap of size <strong>K</strong> with the count of each entry as value. </li>
  <li>Build a MaxHeap of size <strong>K</strong> by traversing the HashMap generated above.</li>
  <li>Pop the elements in the MaxHeap into a list and return the list. </li>
  </ol>
  
  <p>K being the number of unique elements in the input. </p>
  
  <p>The space and time complexity is: O(K) and O(K*log(K)</p>
  
  <p>Now the confusion starts here. We know we are dealing with worst case complexity in the above analysis. So the worst value K can take is N, when all the elements in array are unique.  </p>
  
  <p><strong>Hence K &lt;= N. Thereby O(K) be represented as O(N) ??</strong> </p>
  
  <p>Thereby, shouldn't the space and time complexity be O(N) and O(N*log(N)) for the above problem?</p>
  
  <p>I know this is a technicality, but its been bothering me for a while. Please advise. </p>