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.

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.

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).Shouldn’t the first for loop be,

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

?

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.Can we use a for each loop in the B part

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.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?

Since the methods are static that syntax (

`ClassName.methodName()`

) is acceptable.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

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

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?Sure. (HashSets have size, not length though. Size can change. Length is fixed.)