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.

https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap15_frq_computer_science_a.pdf

## 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

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

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

Shouldn’t the first for loop be,

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

?

• Brandon Horn

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

Can we use a for each loop in the B part

• Brandon Horn

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);
sIndex++;
}

return sums;
}
```
4. anoncow

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

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

5. napstablook

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

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.

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

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