Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Solutions to DiverseArray problem on 2015 AP CompSci exam - https://compsci.rocks/diversearray-solution/
public static int arraySum( int[] arr ) {
int s = 0;
for (int n: arr)
s += n;
return s;
}
public static int arraySum( int[] arr ) {
int s = 0;
for (int i=; i<arr.length; i++) {
s += arr[i]
}
return s;
}
public static int arraySum( int[] arr ) {
int s = 0;
int pos = 0;
while (pos < arr.length) {
s += arr[pos];
pos++;
}
return s;
}
public static boolean isDiverse( int[][] arr2D ) {
/* To be implemented in Part C */
int[] check = rowSums(arr2D);
for (int i=0; i<check.length; i++) {
for (int j=i+1; j<check.length; j++) {
if (check[i] == check[j])
return false;
}
}
return true;
}
public static int[] rowSums( int[][] arr2D ) {
int[] out = new int[arr2D.length];
for (int i=0; i<arr2D.length; i++)
out[i] = arraySum(arr2D[i]);
return out;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.