diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 15a2133..b20cc7b 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -1,64 +1,101 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class Exercises { - /* - there is an array of positive integers as input of function and another integer for the target value - all the algorithm should do is to find those two integers in array which their multiplication is the target - then it should return an array of their indices - e.g. {1, 2, 3, 4} with target of 8 -> {1, 3} - note: you should return the indices in ascending order and every array's solution is unique - */ public int[] productIndices(int[] values, int target) { - // todo + for (int i = 0; i < values.length; i++) { + for (int j = i + 1; j < values.length; j++) { + if (values[i] * values[j] == target) { + return new int[]{i, j}; + } + } + } return null; } - /* - given a matrix of random integers, you should do spiral traversal in it - e.g. if the matrix is as shown below: - 1 2 3 - 4 5 6 - 7 8 9 - then the spiral traversal of that is: - {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 - */ + public int[] spiralTraversal(int[][] values, int rows, int cols) { - // todo - return null; - } + int[] result = new int[rows * cols]; + int index = 0; + int top = 0, bottom = rows - 1, left = 0, right = cols - 1; - /* - integer partitioning is a combinatorics problem in discreet maths - the problem is to generate sum numbers which their summation is the input number + while (top <= bottom && left <= right) { + for (int i = left; i <= right; i++) { + result[index++] = values[top][i]; + } + top++; - e.g. 1 -> all partitions of integer 3 are: - 3 - 2, 1 - 1, 1, 1 + for (int i = top; i <= bottom; i++) { + result[index++] = values[i][right]; + } + right--; - e.g. 2 -> for number 4 goes as: - 4 - 3, 1 - 2, 2 - 2, 1, 1 - 1, 1, 1, 1 + if (top <= bottom) { + for (int i = right; i >= left; i--) { + result[index++] = values[bottom][i]; + } + bottom--; + } - note: as you can see in examples, we want to generate distinct summations, which means 1, 2 and 2, 1 are no different - you should generate all partitions of the input number and + if (left <= right) { + for (int i = bottom; i >= top; i--) { + result[index++] = values[i][left]; + } + left++; + } + } + + return result; + } - hint: you can measure the size and order of arrays by finding the pattern of partitions and their number - trust me, that one's fun and easy :) - if you're familiar with lists and arraylists, you can also edit method's body to use them instead of array - */ public int[][] intPartitions(int n) { - // todo - return null; + List> result = new ArrayList<>(); + partitionHelper(n, n, new ArrayList<>(), result); + return result.stream() + .map(list -> list.stream().mapToInt(Integer::intValue).toArray()) + .toArray(int[][]::new); } + private void partitionHelper(int n, int max, List current, List> result) { + if (n == 0) { + result.add(new ArrayList<>(current)); + return; + } + + for (int i = Math.min(n, max); i >= 1; i--) { + current.add(i); + partitionHelper(n - i, i, current, result); + current.remove(current.size() - 1); + } + } + + public static void main(String[] args) { - // you can test your code here + Exercises ex = new Exercises(); + + int[] values = {1, 2, 3, 4}; + int target = 8; + int[] indices = ex.productIndices(values, target); + System.out.println("Indices: " + Arrays.toString(indices)); // Expected: [1, 3] + + int[][] matrix = { + {1, 2, 3}, + {4, 5, 6}, + {7, 8, 9} + }; + int[] spiral = ex.spiralTraversal(matrix, 3, 3); + System.out.println("Spiral: " + Arrays.toString(spiral)); // Expected: [1, 2, 3, 6, 9, 8, 7, 4, 5] + + // Test intPartitions + int n = 4; + int[][] partitions = ex.intPartitions(n); + System.out.println("Partitions for " + n + ":"); + for (int[] partition : partitions) { + System.out.println(Arrays.toString(partition)); // Expected: [4], [3, 1], [2, 2], [2, 1, 1], [1, 1, 1, 1] + } } }