Color Matrix

Beautiful Square

Common Questions

2D Array Initialization:

  • Declaring and instantiating two-dimensional arrays.
  • Assigning values to specific rows and columns.

2D Array Traversal:

  • Using nested loops to navigate through rows and columns.
  • Accessing and modifying individual elements using indices.

Conditional Checks in 2D Arrays:

  • Searching for specific values or patterns in the array.
  • Implementing conditions to check rows or columns with certain characteristics.

Methods Involving 2D Arrays:

  • Passing 2D arrays as method parameters.
  • Returning 2D arrays from methods.

Variable Scope in 2D Arrays:

  • Differentiating between local variables within methods and instance variables of the class.
  • Understanding the lifecycle of variables within loops and methods.

Resizing and Copying 2D Arrays:

  • Creating new arrays based on certain conditions from existing arrays.
  • Implementing deep copy and shallow copy for 2D arrays.

Common Patterns and Problems:

  • Finding the sum or average of elements in rows or columns.
  • Rotating or flipping the 2D array.
  • Checking for diagonal, horizontal, or vertical patterns.

Topics Covered

  • 2D Array Traversal:
    • Understanding how to navigate through the rows and columns of a 2D array.
    • Using nested loops to access individual elements.
  • Conditional Checks:
    • Evaluating conditions within the 2D array.
    • Making decisions based on the values of elements.
  • Array Resizing:
    • Dynamically creating new arrays based on conditions from existing arrays.
    • Preserving data integrity while resizing.

AP CSA Exam 2021: Problem 4

Q4

Question 4 Part A

Q4A

class arrayResizer {
    public static boolean isNonZeroRow(int[][] array2D, int r) {
        for (int j = 0; j < array2D[r].length; j++) {
            if (array2D[r][j] == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main (String[] args) {
        int[][] arr = {
            {2, 1, 0},
            {1, 3, 2},
            {1, 8, 0},
            {4, 5, 6}
        };
        
        System.out.println(arrayResizer.isNonZeroRow(arr, 1));
        System.out.println(arrayResizer.isNonZeroRow(arr, 2));
    }
}

arrayResizer.main(null);
true
false

Method: isNonZeroRow

Description

This method checks if all elements in a specified row of a 2D array are non-zero.

Parameters

  • array2D: The two-dimensional array to check.
  • r: The index of the row to check.

Return Value

  • Returns true if all elements in the row are non-zero.
  • Returns false otherwise.

Implementation Details

  1. A loop traverses each column of the specified row.
  2. Within the loop, there’s a conditional check for each element.
  3. If any element in the row is zero, the method immediately returns false.
  4. If the loop completes without finding a zero, the method returns true.

Question 4 Part B

Q4B

class arrayResizer {

    // Checks if a given row 'r' in the 2D array 'array2D' has all non-zero elements.
    public static boolean isNonZeroRow(int[][] array2D, int r) {
        for (int j = 0; j < array2D[r].length; j++) {
            // If any element in the row is zero, return false.
            if (array2D[r][j] == 0) {
                return false;
            }
        }
        // If all elements in the row are non-zero, return true.
        return true;
    }
    
    // Counts and returns the number of rows in the 2D array 'array2D' that have all non-zero elements.
    public static int numNonZeroRows(int[][] array2D) {
        int count = 0;
        for (int i = 0; i < array2D.length; i++) {
            // If the row has all non-zero elements, increment the count.
            if (isNonZeroRow(array2D, i)) {
                count++;
            }
        }
        return count;
    }
    
    // Returns a new 2D array that contains only the rows from 'array2D' that have all non-zero elements.
    public static int[][] resize(int[][] array2D) {
        // Get the number of rows that have all non-zero elements.
        int nonZeroRowCount = numNonZeroRows(array2D);
        
        // Create a new 2D array with the size of non-zero rows.
        int[][] resizedArray = new int[nonZeroRowCount][];
        
        int index = 0;
        for (int i = 0; i < array2D.length; i++) {
            // If the row has all non-zero elements, add it to the new 2D array.
            if (isNonZeroRow(array2D, i)) {
                resizedArray[index] = array2D[i];
                index++;
            }
        }
        return resizedArray;
    }

    // Checks if a given row 'r' in the 2D array 'array2D' has all elements in ascending order.
    public static boolean isAscendingRow(int[][] array2D, int r) {
        for (int j = 1; j < array2D[r].length; j++) {
            if (array2D[r][j-1] > array2D[r][j]) {
                return false;
            }
        }
        return true;
    }

    // Returns a new 2D array that contains only the rows from 'array2D' that have elements in ascending order.
    public static int[][] resizeBasedOnOrder(int[][] array2D) {
        int ascendingRowCount = 0;
        for (int i = 0; i < array2D.length; i++) {
            if (isAscendingRow(array2D, i)) {
                ascendingRowCount++;
            }
        }

        int[][] resizedArray = new int[ascendingRowCount][];
        int index = 0;
        for (int i = 0; i < array2D.length; i++) {
            if (isAscendingRow(array2D, i)) {
                resizedArray[index] = array2D[i];
                index++;
            }
        }
        return resizedArray;
    }

    public static void main (String[] args) {
        int[][] arr = {
            {2, 1, 0},
            {1, 3, 2},
            {1, 8, 0},
            {4, 5, 6}
        };
        
        int[][] resizedArr = arrayResizer.resize(arr);
        int[][] resizedOnOrderArr = arrayResizer.resizeBasedOnOrder(arr);
        
        //looping through the rows, then columns of the 2D array, and printing the values out
        for (int i = 0; i < resizedArr.length; i++) {
            for (int j = 0; j < resizedArr[i].length; j++) {
                System.out.print(resizedArr[i][j] + " ");
            }
            System.out.println();
        }
        
        System.out.println();

        //same thing, but this time for the one we resized based on ascending rows
        for (int i = 0; i < resizedOnOrderArr.length; i++) {
            for (int j = 0; j < resizedOnOrderArr[i].length; j++) {
                System.out.print(resizedOnOrderArr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

arrayResizer.main(null);
1 3 2 
4 5 6 

4 5 6 

Method: resize

Overview

The resize method is designed to filter out rows in a 2D array that contain any zero values. The method returns a new 2D array that only includes rows with non-zero values.

Parameters

  • array2D: A two-dimensional array that serves as the input.

Return Value

  • A new 2D array containing only rows from the original array2D that have all non-zero values.

Implementation Details

  1. Determine Non-Zero Rows: The method first calculates the number of rows in the original array that consist entirely of non-zero values.
  2. Initialize New Array: A new 2D array is created with a number of rows equivalent to the count of non-zero rows from the original array.
  3. Copy Non-Zero Rows: The method then iterates through each row of the original array. If a row is identified as having only non-zero values (using the isNonZeroRow helper method), it’s copied to the new array.
  4. Return Resized Array: The new 2D array, now containing only non-zero rows, is returned.

Feedback

1/1

  • Add a method that resizes the array on another condition, like the order of the integers

Resize Method Scoring Guide Resize Method Scoring Guide