Created
December 2, 2018 14:53
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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