Skip to content

Instantly share code, notes, and snippets.

@CompSciRocks
Created December 2, 2018 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CompSciRocks/8fbc93840bb4672c89289de994ba3452 to your computer and use it in GitHub Desktop.
Save CompSciRocks/8fbc93840bb4672c89289de994ba3452 to your computer and use it in GitHub Desktop.
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