Advertisement
apad464

DiverseArrayRunner FRQ

Mar 23rd, 2023
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. class DiverseArray {
  4.  
  5.     public static int arraySum (int[] arr) {
  6.         int total = 0;
  7.         for (int i = 0; i < arr.length; i++) {
  8.             total += arr[i];
  9.         }
  10.         return total;
  11.     }
  12.  
  13.     public static int[] rowSums(int[][] arr2D) {
  14.         int[] arrTotal = new int[arr2D.length];
  15.         for (int i = 0; i < arr2D.length; i++) {
  16.             int[] temp = new int[arr2D[0].length];
  17.             for (int j = 0; j < arr2D[0].length; j++) {
  18.                 temp[j] = arr2D[i][j];
  19.             }
  20.             arrTotal[i] = arraySum(temp);
  21.         }
  22.         return arrTotal;
  23.     }
  24.  
  25.     public static boolean isDiverse(int[][] arr2D) {
  26.         int[] sums = rowSums(arr2D);
  27.         for (int i = 0; i < sums.length; i++) {
  28.             int target = i;
  29.             for (int j = i + 1; j < sums.length; j++) {
  30.                 if (sums[j] == target) {
  31.                     return false;
  32.                 }
  33.             }
  34.         }
  35.         return true;
  36.     }
  37. }
  38.  
  39. public class DiverseArrayRunner {
  40.     public static void main(String[] args) {
  41.         System.out.println("arraySum()\n" + DiverseArray.arraySum(new int[]{1, 3, 2, 7, 3}));
  42.         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}})));
  43.         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}}));
  44.     }
  45. }
Tags: Java
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement