From 2464df0725630f6206492c10698ff1f9110b16d6 Mon Sep 17 00:00:00 2001 From: Romina Date: Fri, 24 Apr 2026 17:33:12 +0330 Subject: [PATCH] implement triangle method --- src/main/java/MainExercises.java | 238 ++++++++++++++++++------------- 1 file changed, 141 insertions(+), 97 deletions(-) diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..75fbffe 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,103 +1,147 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Arrays; + public class MainExercises { - /* - you should create a triangle with "*" and return a two-dimensional array of characters based on that - the triangle's area is empty, which means some characters should be " " - example 1, input = 3: - * - ** - *** - - example 2, input = 5: - * - ** - * * - * * - ***** - - the output has to be a two-dimensional array of characters, so don't just print the triangle! - */ - public char[][] generateTriangle(int n) { - - // todo - return null; - - } - - - - /* - SPIRAL TRAVERSAL OF A RECTANGULAR MATRIX - - Given a rectangular matrix (2D array) of integers, this method traverses - it in a spiral order (clockwise from outside to inside) and returns - the elements as a 1D array. - - EXAMPLE: - Input matrix: - 1 2 3 - 4 5 6 - 7 8 9 - - Spiral order: start at top-left (1), go right →, then down ↓, - then left ←, then up ↑, then repeat inward. - - Result: {1, 2, 3, 6, 9, 8, 7, 4, 5} - - so you should walk in that matrix in a curl and then add the numbers in order you've seen them in a 1D array - - RECTANGULAR MATRIX ASSUMPTION: - This method assumes the input matrix is RECTANGULAR (all rows have - the same number of columns). In Java, we can verify this because - 2D arrays might be jagged (rows of different lengths). - - IMPORTANT: In Java, we do NOT need to pass rows and cols! - The 2D array 'matrix' knows its own dimensions: - - Number of rows: matrix.length - - Number of columns: matrix[0].length (if rectangular) - */ - public int[] spiralTraversal(int[][] matrix) { - // todo - return null; - } - - /* - integer partitioning is a combinatorics problem in discreet maths - the problem is to generate sum numbers which their summation is the input number - - e.g. 1 -> all partitions of integer 3 are: - 3 - 2, 1 - 1, 1, 1 - - e.g. 2 -> for number 4 goes as: - 4 - 3, 1 - 2, 2 - 2, 1, 1 - 1, 1, 1, 1 - - Note: As you can see in the examples, we want to generate distinct partitions, - which means 1,2 and 2,1 are not different — they count as the same combination. - - You should generate all partitions of the input number. - - Hint: You can determine the size and order of the arrays by finding the pattern - of partitions and their count. Trust me, this one's fun and easy :) - - If you're familiar with Lists and ArrayLists, you can also edit the method's - body to use them instead of arrays. - */ - - public int[][] intPartitions(int n) { - // todo - return null; - } - - - public static void main() + public static char[][] generateTriangle(int n) { + if (n <= 0) + { + return new char[0][0]; + } + char[][] triangle = new char[n][]; + for (int i = 0; i < n; i++) + { + triangle[i] = new char[i + 1]; + Arrays.fill(triangle[i], ' '); + + if (i == n - 1) + { + Arrays.fill(triangle[i], '*'); + } + else + { + if (triangle[i].length > 0) + { + triangle[i][0] = '*'; // First character + } + if (triangle[i].length > 1) + { + triangle[i][triangle[i].length - 1] = '*'; // Last character + } + } + } + return triangle; } -} \ No newline at end of file + + + public static int[] spiralTraversal(int[][] matrix) + { + if (matrix == null || matrix.length == 0 || matrix[0].length == 0) + { + return new int[0]; + } + + int rows = matrix.length; + int cols = matrix[0].length; + int[] result = new int[rows * cols]; + int index = 0; + + int top = 0, bottom = rows - 1, left = 0, right = cols - 1; + + while (top <= bottom && left <= right) + { + // Traverse Right + for (int j = left; j <= right; j++) + { + result[index++] = matrix[top][j]; + } + top++; + + // Traverse Down + for (int i = top; i <= bottom; i++) + { + result[index++] = matrix[i][right]; + } + right--; + + // Traverse Left (only if there's a row left to traverse) + if (top <= bottom) + { + for (int j = right; j >= left; j--) + { + result[index++] = matrix[bottom][j]; + } + bottom--; + } + + // Traverse Up (only if there's a column left to traverse) + if (left <= right) + { + for (int i = bottom; i >= top; i--) + { + result[index++] = matrix[i][left]; + } + left++; + } + } + return result; + } + + + public static int[][] intPartitions(int n) + { + if (n <= 0) + { + return new int[0][0]; + } + + List> partitionsList = new ArrayList<>(); + findPartitionsHelper(n, n, new ArrayList<>(), partitionsList); + + return convertListToArray(partitionsList); + } + + + private static void findPartitionsHelper(int target, int maxVal, List currentPartition, List> allPartitions) + { + if (target == 0) + { + allPartitions.add(new ArrayList<>(currentPartition)); + return; + } + + for (int i = Math.min(maxVal, target); i >= 1; i--) + { + currentPartition.add(i); + findPartitionsHelper(target - i, i, currentPartition, allPartitions); + currentPartition.remove(currentPartition.size() - 1); + } + } + + private static int[][] convertListToArray(List> partitionsList) + { + int numRows = partitionsList.size(); + if (numRows == 0) + { + return new int[0][0]; + } + + int[][] result = new int[numRows][]; + for (int i = 0; i < numRows; i++) + { + List rowList = partitionsList.get(i); + int[] rowArray = new int[rowList.size()]; + for (int j = 0; j < rowList.size(); j++) + { + rowArray[j] = rowList.get(j); + } + result[i] = rowArray; + } + return result; + } + +}