Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- class DiverseArray {
- public static int arraySum (int[] arr) {
- int total = 0;
- for (int i = 0; i < arr.length; i++) {
- total += arr[i];
- }
- return total;
- }
- public static int[] rowSums(int[][] arr2D) {
- int[] arrTotal = new int[arr2D.length];
- for (int i = 0; i < arr2D.length; i++) {
- int[] temp = new int[arr2D[0].length];
- for (int j = 0; j < arr2D[0].length; j++) {
- temp[j] = arr2D[i][j];
- }
- arrTotal[i] = arraySum(temp);
- }
- return arrTotal;
- }
- public static boolean isDiverse(int[][] arr2D) {
- int[] sums = rowSums(arr2D);
- for (int i = 0; i < sums.length; i++) {
- int target = i;
- for (int j = i + 1; j < sums.length; j++) {
- if (sums[j] == target) {
- return false;
- }
- }
- }
- return true;
- }
- }
- public class DiverseArrayRunner {
- public static void main(String[] args) {
- System.out.println("arraySum()\n" + DiverseArray.arraySum(new int[]{1, 3, 2, 7, 3}));
- System.out.println("\nrowSums()\n" + Arrays.toString(DiverseArray.rowSums(new int[][]{{1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1}})));
- System.out.println("\nisDiverse()\n" + DiverseArray.isDiverse(new int[][]{{1, 3, 2, 7, 3}, {10, 10, 4, 6, 2}, {5, 3, 5, 9, 6}, {7, 6, 4, 2, 1}}));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement