From b754df02edbb68b26ade98c5d79d61abc38f944f Mon Sep 17 00:00:00 2001 From: ShayanEdalatjoo Date: Sat, 25 Apr 2026 13:44:15 +0330 Subject: [PATCH] Adding intPartitions object. --- .idea/.name | 1 + src/main/java/MainExercises.java | 42 ++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .idea/.name diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 0000000..30a3ff1 --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +MainExercises.java \ No newline at end of file diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 60a5b89..9367256 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,5 @@ +import java.util.ArrayList; + public class MainExercises { /* @@ -29,6 +31,7 @@ public class MainExercises triangle[i][j] = ' '; } } + return triangle; } @@ -124,15 +127,44 @@ public class MainExercises If you're familiar with Lists and ArrayLists, you can also edit the method's body to use them instead of arrays. */ - + private ArrayList> intPartitionsHelper(int n, int biggest) { + ArrayList> result = new ArrayList<>(); + if (n == 0) { + result.add(new ArrayList<>()); + return result; + } + for (int k = 1; k <= Math.min(biggest, n); k++) { + ArrayList> subPartitions = intPartitionsHelper(n - k, k); + for (ArrayList subPartition : subPartitions) { + ArrayList newPartition = new ArrayList<>(); + newPartition.add(k); + newPartition.addAll(subPartition); + result.add(newPartition); + } + } + return result; + } public int[][] intPartitions(int n) { - // todo - return null; + ArrayList> answerList = intPartitionsHelper(n, n); + int[][] answerArray = new int[answerList.size()][]; + for(int i = 0; i < answerList.size(); i++){ + answerArray[(answerList.size() - i) - 1] = new int[answerList.get(i).size()]; + for(int j = 0; j < answerList.get(i).size(); j++){ + answerArray[(answerList.size() - i) - 1][j] = answerList.get(i).get(j); + } + } + return answerArray; } - public static void main() + public void main() { - + int n = 4; + int[][] partitions = intPartitions(n); + for (int i = 0; i < partitions.length; i++) { + for(int j = 0; j < partitions[i].length; j++) + System.out.print(partitions[i][j]); + System.out.println(); + } } } \ No newline at end of file