The Problem & First Impressions

  • The main topics of this question revolve around creating a class named DiverseArray with three static methods for handling one-dimensional and two-dimensional arrays of integers. The methods are focused on calculating sums: the first method finds the sum of values in a one-dimensional array, the second method calculates the sums of rows in a two-dimensional array, and the third method analyzes the uniqueness of row sums in the two-dimensional array. The question requires understanding and implementation of these methods in Java.

Prompt Summary

  1. Method 1 - Sum of One-Dimensional Array:
    • Method Signature: public static int arraySum(int[] arr)
    • Description: Returns the sum of the values in a one-dimensional array.
    • Parameters:
      • arr - One-dimensional array of integers.
  2. Method 2 - Sum of Rows in Two-Dimensional Array:
    • Method Signature: public static int[] rowSums(int[][] arr2D)
    • Description: Returns an array representing the sums of the rows of a two-dimensional array.
    • Parameters:
      • arr2D - Two-dimensional array of integers.
  3. Method 3 - Analyzing Row Sums:
    • Method Signature: public static boolean analyzeRows(int[][] arr2D)
    • Description: Analyzes the row sums of a two-dimensional array and returns a boolean indicating whether all row sums are different.
    • Parameters:
      • arr2D - Two-dimensional array of integers.
    • Return:
      • true if all row sums are different.
      • false if there are any repeated row sums.

Part A:

  • Task: Create a static method named arraySum.
  • Purpose: Calculate and return the sum of entries in a specified one-dimensional array.
  • Example: Given an array arr1, demonstrate the result obtained by calling the arraySum method.
  • Outcome: The method should compute the sum of elements in the array and return the result.

My Solution:

  • I iterate through the list and add each element in a local variable that is initialized outside the scope of each loop run.
class DiverseArray {
    public static int arraySum(int[] arr) {
        int totalSum = 0;
        for (int index = 0; index < arr.length; index++) {
            totalSum += arr[index];
        }
        return totalSum;
    }
}

int[] myArray = {1, 1, 1, 1, 1, 1};

int resultSum = DiverseArray.arraySum(myArray);

System.out.println(resultSum);
6

Part B

  • Task: Implement a static method named rowSums.
  • Purpose: Calculate and return the sums of each row in a given two-dimensional array.
  • Example: Given a two-dimensional array arr2D in row-major order, demonstrate the result obtained by calling the rowSums method.
  • Outcome: The method should produce a one-dimensional array, where each entry is the sum of the corresponding row in the input array arr2D. The provided example involves an array mat1 with row sums {16, 32, 28, 20}.

My Solution:

  • This uses the same methodologies as my previous code, calling arraySum within each iteration of rows, and storing the respective sums in an index of an array that is beyond the scope of the loop
class DiverseArray {
  public static int arraySum(int[] arr) {
      int sum = 0;
      for (int i = 0; i<arr.length; i++){
          sum+=arr[i];
      }
      return sum;
  }

  public static int[] rowSums(int[][] twoD) {
      int[] sums = new int[twoD.length];
      for (int i = 0; i<twoD.length; i++) {
          sums[i] = arraySum(twoD[i]);
      }
      return sums;
  }
}

int[][] array = {{1,2,3,4,5}, {6,7,8,9,10}, {1,1,1,1,0}};
int[] result = DiverseArray.rowSums(array);
for (int i = 0; i<result.length; i++) {
  System.out.print(result[i] + " ");
}
// 

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

  public static int[] rowSums(int[][] twoD) {
      int[] sums = new int[twoD.length];
      for (int i = 0; i<twoD.length; i++) {
          sums[i] = arraySum(twoD[i]);
      }
      return sums;
  }
}

int[][] array = {{1,2,3,4,5}, {6,7,8,9,10}, {1,1,1,1,0}};
int[] result = DiverseArray.rowSums(array);
for (int i = 0; i<result.length; i++) {
  System.out.print(result[i] + " ");
}

// 
15 40 4 

Part C

  • Task: Implement a static method named isDiverse.
  • Purpose: Determine whether a given two-dimensional array is diverse.
  • Example: Given a two-dimensional array arr2D, demonstrate the result obtained by calling the isDiverse method.
  • Outcome: The method should return true if all row sums in the array are unique; otherwise, it should return false. In the provided examples, isDiverse(mat1) returns true as each row sum is unique, while isDiverse(mat2) returns false due to repeated row sums.

My Solution

  • Use a nested for loop that checks if sums are equal
class DiverseArray {
    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int i = 0; i<arr.length; i++){
            sum+=arr[i];
        }
        return sum;
    }
  
    public static int[] rowSums(int[][] twoD) {
        int[] sums = new int[twoD.length];
        for (int i = 0; i<twoD.length; i++) {
            sums[i] = arraySum(twoD[i]);
        }
        return sums;
    }
    
    public static boolean isDiverse(int[][] twoD) {
        int[] rowSums = rowSums(twoD);
    
        for (int i = 0; i < rowSums.length; i++) {
            int equal = 0;
    
            for (int j = 0; j < rowSums.length; j++) {
                if (rowSums[i] == rowSums[j]) {
                    equal++;
                }
            }
    
            if (equal > 1) {
                return false;
            }
        }
    
        return true;
    }
    
}

int[][] array1 = {{1,2,3,4,5}, {6,7,8,9,0}};
int[][] array2 = {{1,1,1,1,1}, {1,1,1,1,1}};
System.out.print(DiverseArray.isDiverse(array1) + "\n");
System.out.print(DiverseArray.isDiverse(array2));
// 

class DiverseArray {
    public static int arraySum(int[] arr) {
        int sum = 0;
        for (int i = 0; i<arr.length; i++){
            sum+=arr[i];
        }
        return sum;
    }
  
    public static int[] rowSums(int[][] twoD) {
        int[] sums = new int[twoD.length];
        for (int i = 0; i<twoD.length; i++) {
            sums[i] = arraySum(twoD[i]);
        }
        return sums;
    }
    
    public static boolean isDiverse(int[][] twoD) {
        int[] rowSums = rowSums(twoD);
    
        for (int i = 0; i < rowSums.length; i++) {
            int equal = 0;
    
            for (int j = 0; j < rowSums.length; j++) {
                if (rowSums[i] == rowSums[j]) {
                    equal++;
                }
            }
    
            if (equal > 1) {
                return false;
            }
        }
    
        return true;
    }
    
}

int[][] array1 = {{1,2,3,4,5}, {6,7,8,9,0}};
int[][] array2 = {{1,1,1,1,1}, {1,1,1,1,1}};
System.out.print(DiverseArray.isDiverse(array1) + "\n");
System.out.print(DiverseArray.isDiverse(array2));

// 
true
false