title: 2D Array, best average calculator using HashMap
body: <p><strong>Problem Statement:</strong> I have a 2D array of strings containing student names and respective marks as below</p>
  
  <pre><code>String[][] scores = {{"Bob","85"},{"Mark","100"},{"Charles","63"},{"Mark","34"}};
  </code></pre>
  
  <p>I want to calculate the best average among all the students available, i.e with the above input the best average should be 85.</p>
  
  <p><strong>My Attempt:</strong>
  I tried to solve this using HashMap as below.</p>
  
  <pre><code>public int  bestAverageCalculator(String[][] scores) {
  
      // This HashMap maps student name to their list of scores
      Map&lt;String,List&lt;Integer&gt;&gt; scoreMap = new HashMap&lt;String,List&lt;Integer&gt;&gt;();
      for(String[] score:scores) {
          String name = score[0];
          int currentScore =Integer.parseInt(score[1]);
  
          if(scoreMap.containsKey(name)) {
              List&lt;Integer&gt; scoreList = scoreMap.get(name);
              scoreList.add(currentScore);
              scoreMap.put(name, scoreList);
          }
          else {
              List&lt;Integer&gt; scoreList = new ArrayList&lt;Integer&gt;();
              scoreList.add(currentScore);
              scoreMap.put(name, scoreList);
          }
      }
      //scoreMap will be {Charles=[63], Bob=[85], Mark=[100, 34]}
      //After Map is formed i am iterating though all the values and finding the best average as below
      int bestAverage = 0;
      for(List&lt;Integer&gt; value:scoreMap.values()) {
          int sum = 0;
          int count = 0;
          for(int i:value) {
              sum+=i;
              count++;
          }
          int average = (int)Math.floor(sum/count);
          if(average&gt;bestAverage)
              bestAverage = average;
      }
  
      return bestAverage;// returns 85
  
  }
  </code></pre>
  
  <p>The implementation is correct and i am getting the answer as expected, but i was told the space complexity of the program is more and it can be achieved without using the <code>List&lt;Integer&gt;</code> for marks, i am not able to understand how average can be calculated on fly without storing list of marks.</p>
  
  <p>Please suggest if any other methods can solve this other than HashMap.</p>
  
  <p>Any help would be appreciated.</p>