DiverseArray free response answer 12

DiverseArray free response problem from the 2015 AP Computer Science A Exam.

DiverseArray is #1 from the from the 2015 AP Computer Science A Free Response problems.


Part (a) – arraySum method

public static int arraySum(int[] arr)
  int sum = 0;
  for(int value : arr)
    sum += value;

  return sum;

Part (b) – rowSums method

public static int[] rowSums(int[][] arr2D)
  int[] sums = new int[arr2D.length];
  for(int i = 0; i < sums.length; i++)
    sums[i] = arraySum(arr2D[i]);

  return sums;

This method requires a call to arraySum so the 2D array arr2D must be treated as an array of 1D arrays. A regular for loop is appropriate here since the index is needed for both sums and arr2D.

Part (c) – isDiverse method

public static boolean isDiverse(int[][] arr2D)
  int[] sums = rowSums(arr2D);
  for(int i = 0; i < sums.length; i++)
    for(int j = i + 1; j < sums.length; j++)
      if(sums[i] == sums[j])
        return false;
  return true;

I’m not usually a proponent of using nested loops to traverse a 1D array; however, they are a simple solution here. An alternative solution is to sort sums (not arr2D) then check for adjacent duplicate values. A Map could also be used; however, that is outside the scope of AP CS A.

12 thoughts on “DiverseArray free response answer

  1. Ria G May 9,2015 5:28 pm

    Part B, line 5, has a typo, no? array2D is a two-D array so it’s needs a double bracket to access an element.

    • Brandon Horn May 10,2015 8:17 am

      In part (b) the intent is to access an entire row of arr2D so that the row can be passed to arraySum. This requires a single index (that of the row).

  2. Alan Tang May 10,2015 1:00 am

    Shouldn’t the first for loop be,

    for(int i = 0; i < sums.length -1; i++)


    • Brandon Horn May 10,2015 8:21 am

      I assume you’re referring to isDiverse. Both your loop and mine would work. In my case the outer loop would visit the last element but the inner loop would never run (The condition would be false before the first execution.). I considered adding a -1 to the outer loop but I felt it was clearer without it.

  3. anonymously May 10,2015 4:14 am

    Can we use a for each loop in the B part

    • Brandon Horn May 10,2015 8:24 am

      You could use a for each loop to traverse the rows of arr2D (the 2D array); however, you would need to maintain an separate index for sums (the 1D array). This would be more effort than a regular for loop. The code to do so is below.

      public static int[] rowSums(int[][] arr2D)
        int[] sums = new int[arr2D.length];
        int sIndex = 0;
        for(int[] row : arr2D)
          sums[sIndex] = arraySum(row);
        return sums;
  4. anoncow May 11,2015 11:46 am

    Hey Brandon,

    If I called any of the previous methods using DiverseArray.calledMethod, in any of the parts, would it be okay? Or would points be deducted?

    • Brandon Horn May 12,2015 10:23 am

      Since the methods are static that syntax (ClassName.methodName()) is acceptable.

  5. napstablook Jan 13,2016 10:11 pm

    Teacher said for part B, that is could be

    int [] sums = new int [arr2D.length];
    int rowCount = 0;
    for(int [] row : arr2D {
      sums[rowCount] = arraySum(row);
      rowCount ++

    return sums;

    // I am not so sure he is right or what this is

    • Brandon Horn Feb 17,2016 12:28 pm

      Yes. You could use a for-each loop as your teacher stated. I used a regular for loop since I needed the index anyway.

  6. Nick K. May 4,2016 9:34 pm

    Lol for part c could I have created a HashSet, stuffed all the values from a call to rowSums() in there, and returned whether the length of the HashSet equaled the length of the row sums array?

    • Brandon Horn May 5,2016 1:16 pm

      Sure. (HashSets have size, not length though. Size can change. Length is fixed.)

Comments are closed.