Implement 3 methods; Triangle, Traversal and Partitions #1

Merged
Meraj merged 1 commits from develop into main 2026-04-27 00:29:21 +00:00
+87 -9
View File
@@ -1,3 +1,6 @@
import java.util.ArrayList;
import java.util.List;
public class MainExercises
{
/*
@@ -19,9 +22,18 @@ public class MainExercises
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;
char[][] triangle = new char[n][];
for (int i = 0; i < n; i++) {
triangle[i] = new char[i + 1];
for(int j = 0; j <= i; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == i) {
triangle[i][j] = '*';
} else {
triangle[i][j] = ' ';
}
}
}
return triangle;
}
@@ -58,8 +70,47 @@ public class MainExercises
- Number of columns: matrix[0].length (if rectangular)
*/
public int[] spiralTraversal(int[][] matrix) {
// todo
return null;
int[] result = new int[matrix.length * (matrix[0].length)];
int topI = 0;
int leftJ = 0;
int botI = matrix.length - 1;
int rightJ = matrix[0].length - 1;
int count = 0;
int turn = 1; // 1 for topI, 2 for rightJ, 3 for botI, 4 for leftJ
while (count < matrix.length * matrix[0].length) {
if (turn == 1) {
for(int j = leftJ; j <= rightJ; j++) {
result[count] = matrix[topI][j];
count++;
}
topI++;
turn = 2;
} else if (turn == 2) {
for (int i = topI; i <= botI; i++) {
result[count] = matrix[i][rightJ];
count++;
}
rightJ--;
turn = 3;
} else if (turn == 3) {
for (int j = rightJ; j >= leftJ; j--) {
result[count] = matrix[botI][j];
count++;
}
botI--;
turn = 4;
} else if (turn == 4) {
for (int i = botI; i >= topI; i--) {
result[count] = matrix[i][leftJ];
count++;
}
leftJ++;
turn = 1;
}
}
return result;
}
/*
@@ -84,16 +135,43 @@ public class MainExercises
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 :)
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 int[][] intPartitions(int n) {
List<List<Integer>> partitions = new ArrayList<>();
List<Integer> current = new ArrayList<>();
addPartition(n, n,current, partitions);
int[][] result = new int[partitions.size()][];
for (int i = 0; i < partitions.size(); i++) {
List<Integer> p = partitions.get(i);
int size = p.size();
result[i] = new int[size];
for (int j = 0; j < size;j++) {
result[i][j] = p.get(j);
}
}
return result;
}
private void addPartition(int remaining, int maxAllowed,
List<Integer> current, List<List<Integer>> partitions){
if (remaining == 0) {
partitions.add(new ArrayList<>(current));
return;
}
for (int j = Math.min(remaining, maxAllowed); j >=1; j--) {
current.add(j);
addPartition(remaining - j, j, current, partitions);
current.remove(current.size() - 1);
}
}
public static void main()