diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/src/main/java/MainExercises.java b/src/main/java/MainExercises.java index 21581ea..0798ac2 100644 --- a/src/main/java/MainExercises.java +++ b/src/main/java/MainExercises.java @@ -1,3 +1,6 @@ +import java.util.ArrayList; +import java.util.List; + public class MainExercises { /* @@ -20,8 +23,21 @@ public class MainExercises */ 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+1 ; j++){ + if(i == 0 || j == 0 || i== j || i == n-1 ){ + triangle[i][j] = '*'; + } + else{ + triangle[i][j] = ' '; + } + } + } + + return triangle; } @@ -57,11 +73,56 @@ public class MainExercises - Number of rows: matrix.length - Number of columns: matrix[0].length (if rectangular) */ + + public int[] spiralTraversal(int[][] matrix) { - // todo - return null; + int rows = matrix.length; + int columns = matrix[0].length; + int[] result = new int[rows*columns]; + + int top = 0; + int down = rows-1; + int left = 0; + int right = columns-1; + int counter = 0; + + while(top <= down && left <= right){ + for (int i = left ; i <= right ; i++){ + result[counter] = matrix[top][i]; + counter++; + } + top++; + + for(int i = top ; i <= down ; i++){ + result[counter] = matrix[i][right]; + counter++; + } + right--; + + if (counter < rows*columns){ + for(int i = right ; i >= left ; i--){ + result[counter] = matrix[down][i]; + counter++; + } + down--; + + for(int i = down ; i >= top ; i--){ + result[counter]= matrix[i][left]; + counter++; + } + left++; + + + } + + } + + + + return result; } + /* integer partitioning is a combinatorics problem in discreet maths the problem is to generate sum numbers which their summation is the input number @@ -90,11 +151,67 @@ public class MainExercises body to use them instead of arrays. */ + private static List> result; + private static List help; + + public int[][] intPartitions(int n) { - // todo - return null; + result = new ArrayList<>(); + help = new ArrayList<>(); + + findRecursive(n, n); + + int[][] answer = casting(result); + + return answer; } + private static void findRecursive(int target , int max){ + + if(target == 0){ + + result.add(new ArrayList<>(help)); + return; + } + + for(int i = Math.min(target , max) ; i >= 1 ; i--){ + help.add(i); + + + findRecursive(target -i , i); + + help.remove(help.size() -1); + + } + + } + + private int[][] casting(List> input){ + int n = input.size(); + + int[][] answer = new int[n][]; + + for(int i = 0 ; i < n ; i++){ + int k = input.get(i).size(); + answer[i] = new int[k]; + + for(int j = 0 ; j < k ; j++){ + answer[i][j] = input.get(i).get(j); + } + + + } + + return answer; + + + + } + + + + + public static void main() {