Question 1: 2D Arrays

Part A

Write a static method arraySum that calculates and returns the sum of the entries in a specified one-dimensional array. The following example shows an array arr1 and the value returned by a call to arraySum.

public static int arraySum(int[] arr){
    int arrSum = 0;
    for(int i = 0; i < arr.length; i++){
        arrSum += arr[i]; 
    }
    return arrSum;
}

int[] arr = {1,2,3,4};
arraySum(arr);
10

Reflection

Overall the problem was very simple with just a for loop needed to iterate through the array and add the values to a sum. With the problem only really testing knowledge of the indexing arrays, declaring variables, and other really basic concepts.

Part B

Write a static method rowSums that calculates the sums of each of the rows in a given two-dimensional array and returns these sums in a one-dimensional array. The method has one parameter, a two- dimensional array arr2D of int values. The array is in row-major order: arr2D[r][c] is the entry at row r and column c. The method returns a one-dimensional array with one entry for each row of arr2D such that each entry is the sum of the corresponding row in arr2D. As a reminder, each row of a two-dimensional array is a one-dimensional array. For example, if mat1 is the array represented by the following table, the call rowSums(mat1) returns the array {16, 32, 28, 20}.

public static int[] rowSums(int[][] arr2d){  
    int[] sumArr = new int[arr2d.length];
    for(int i = 0; i < arr2d.length; i++){
        int rowSum = arraySum(arr2d[i]);
        sumArr[i] = rowSum;
    };
    return sumArr;
}
int[][] mat1 = 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(Arrays.toString(rowSums(mat1)));

[16, 32, 28, 20]

Reflection

This problem was again very similar to part A, with the only difference being that we had to iterate through a 2D array instead of a 1D array, then call the sum method we defined in part A to get the sum of each row then add it to a variable. This was again a very simple question that only seemed to test some basic concepts.

Part C

A two-dimensional array is diverse if no two of its rows have entries that sum to the same value. In the following examples, the array mat1 is diverse because each row sum is different, but the array mat2 is not diverse because the first and last rows have the same sum.Write a static method isDiverse that determines whether or not a given two-dimensional array is diverse. The method has one parameter: a two-dimensional array arr2D of int values. The method should return true if all the row sums in the given array are unique; otherwise, it should return false. In the arrays shown above, the call isDiverse(mat1) returns true and the call isDiverse(mat2) returns false.

public static boolean isDiverse(int[][] mat1){
    boolean diverse = false;
    int[] rowSums = rowSums(mat1);

    for(int i = 0; i < rowSums.length; i++){
        for(int j = i+1; j < rowSums.length; j++){
            if (rowSums[i] != rowSums[j]){
                continue;
            } else{
                return diverse;
            }
        }
    }
    diverse = true;
    return diverse;
}

int[][] mat1 = 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("Call 1 Mat 1");
System.out.println(isDiverse(mat1));

int[][] mat2 = {
    {1,1,5,3,4},
    {12,7,6,1,9},
    {8,11,10,2,5},
    {3,2,3,0,6}
};


System.out.println("\nCall 2 Mat 2");
System.out.println(isDiverse(mat2));
Call 1 Mat 1
true

Call 2 Mat 2
false

Reflection

This question was more challenging than the prior 2 questions as it required you to compare the sums of each row and see if there were no duplicates within the array. With the first obvious step being to first call the rowSums method we defined in part B to get the sums of each row, then begin the main logic. This was different than the prior two solution as it required a nested for loop so you could compare each element to within the array of rowSums to every element after it through indexing rowSums from the current index and the following indexes and comparing them to each other with an if statement. This condition if met would return false, if not met would return true. Overall the problem was not very challenging but was more interesting than the prior two questions.