From 067eef9e97c9085a435e71c1231e78d286cd169f Mon Sep 17 00:00:00 2001 From: Ali Ghaedrahmat Date: Sat, 15 Mar 2025 03:19:14 +0330 Subject: [PATCH] commit for this repo --- .../src/main/java/Exercises.class | Bin 0 -> 1748 bytes .../src/main/java/Exercises.java | 130 +++++++++++------- .../src/main/java/tempCodeRunnerFile.java | 1 + 3 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 AP1403 - Algorithms/src/main/java/Exercises.class create mode 100644 AP1403 - Algorithms/src/main/java/tempCodeRunnerFile.java diff --git a/AP1403 - Algorithms/src/main/java/Exercises.class b/AP1403 - Algorithms/src/main/java/Exercises.class new file mode 100644 index 0000000000000000000000000000000000000000..5949ba8e72003ac9b567623632aefcbdf0691f12 GIT binary patch literal 1748 zcmZuy-)|IE6#i!C&fM8yw}rO1W!)V>K-ji)3oEWtpr{1mlF~(r*xHDlZYS+vyIZ$g zR31!>`{+L)`T)g*2m62~R%0qpk{Eq5kvAVrO!yN>b^Y#iLu=xux##{k=iKl6&biZH z{`uhnfV0TEh{2Q)30SZV?7Z&Z@CWDp%Isii`g%~V8?aA>m9Rc+z|8hub>Sc`;U=&H z2?OzS9|W~>xD+fIXfIcnEA>l$tsd6HYK1#iwpi>fO0+R{a%n&a1pSMXMY={|T?S%0svA8LyAw$18{4)3Ui9m8%)Ag*;z%PS;U%yKd;ddX?8Bvc zut2ZsGO4JMC_1bTUaEza`dGad_zNdpJd6Dj2NF1_CFuMg9|LkxN6c3Yh_lt1fOJQ~ zN-(;-Fdfvc_|x;;XQYei&MGnp(JNRHM0^1B!P#g=RP(qdTi=daZK z8$oT!pQn;pO}pjIo3`s>XSuJ_hy_0+g6{0(CXuL4WD!Y>v-y<6IpUUK+Af_Wg!1?lm7ZOpZTAhcO<;N}~h! znKBk#+6R|&L8KPC2w@MeJIGZYalF92lRSF`NnAu5dg-|f)&^q623+It6IlEs(mwQW z;x#^@`wqJOp7{*Yy9H^kyZ1BR+`2m?%z|YW?2MQxNVDL0v~guCc^CJTqvCgQTR8<+ zxq7-gC6v{$jDBV3m76OyTvGU}VH?VhTo|G1OLl-LsO8hXgF0|`pj2}L1$a2&gFhA(E6_6lFeo5*0A zrGAh0eeA_e?8C=wiBGW~pVQu<%wIC{D^`1r^?peEE0z2Wxk%^}NIGVd30(B zoreAa8`xz$L3)FaCo)NGV!9@@x*$g|&}yJZtY@5H=las=HOSMM^g|?LqiJ0bZkZ`D zV6LHEx6qI19J0-VR8mk*W zv$OJ)2kaw>=h-{QDO7GBS;f7#Y zAA74$p&p7PdX;(3@(G;dlQ_@puMu{UmAt^}6j_y#2wnohw*J=Gp0Ls}{c&yK_?A>1 z=DbE6M^cuuy+}Fg>8+q!Z{(F^V@o!cR_n2{#nkF1Oga(f!IbcnED@`mVU(Ck2_2^E zV>cv0|Do(H)Vb)TT_Wn&iS{!4aE!fmnP}f&`Nt{o1YX8lT#aKAZ*M){b`4!@c8{i> i!(fZT0a^+T`3;^~!@jL=lm4d#_Op#L9garI9s3uRcs^qQ literal 0 HcmV?d00001 diff --git a/AP1403 - Algorithms/src/main/java/Exercises.java b/AP1403 - Algorithms/src/main/java/Exercises.java index 15a2133..913e560 100644 --- a/AP1403 - Algorithms/src/main/java/Exercises.java +++ b/AP1403 - Algorithms/src/main/java/Exercises.java @@ -1,64 +1,100 @@ 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 - return null; + for (int i = 0; i < values.length; i++) { + for (int j = i + 1; j < values.length; j++) { + int mult = values[i] * values[j]; + if (mult == target) { + int[] result = {i, j}; + return result; + } + } + } + int[] result = {-1, -1}; + return result; } + + public int[] spiralTraversal(int[][] matrix, int rowCount, int colCount) { + if (rowCount == 0 || colCount == 0) return new int[0]; - /* - 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} + int[] output = new int[rowCount * colCount]; + int pos = 0, upper = 0, lower = rowCount - 1, start = 0, end = colCount - 1; - 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; + while (upper <= lower && start <= end) { + for (int col = start; col <= end; col++){ + output[pos++] = matrix[upper][col]; + } + upper++; + for (int row = upper; row <= lower; row++){ + output[pos++] = matrix[row][end]; + } + end--; + if (upper <= lower) { + for (int col = end; col >= start; col--) { + output[pos++] = matrix[lower][col]; + } + lower--; + } + if (start <= end) { + for (int row = lower; row >= upper; row--){ + output[pos++] = matrix[row][start]; + } + start++; + } + } + + return output; } - /* - integer partitioning is a combinatorics problem in discreet maths - the problem is to generate sum numbers which their summation is the input number + public int[][] intPartitions(int n) { + int maxPartitionCount = countPartitions(n , n); + int[][] partitionResult = new int[maxPartitionCount][n]; + int[] currentPartition = new int[n]; - e.g. 1 -> all partitions of integer 3 are: - 3 - 2, 1 - 1, 1, 1 + int currentResultIndex = generatePartitions(n, n, 0, currentPartition, partitionResult, 0); - e.g. 2 -> for number 4 goes as: - 4 - 3, 1 - 2, 2 - 2, 1, 1 - 1, 1, 1, 1 + int[][] partition = new int[currentResultIndex][]; + for (int i = 0; i < currentResultIndex; i++) { + partition[i] = new int[partitionResult[i].length]; + for (int j = 0; j < partitionResult[i].length; j++) { + partition[i][j] = partitionResult[i][j]; + } + } + return partition; + } + private int countPartitions(int n, int max) { + if (n == 0) { + return 1; + } + if (n < 0) { + return 0; + } + int count = 0; + for (int i = Math.min(n, max); i >= 1; i--) { + count += countPartitions(n - i, i); + } + return count; + } - 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 + private int generatePartitions(int n, int max, int index, int[] currentPartition, int[][] partitionResult, int resultIndex) { + if (n == 0) { + partitionResult[resultIndex] = new int[index]; + for (int i = 0; i < index; i++) { + partitionResult[resultIndex][i] = currentPartition[i]; + } + return resultIndex + 1; + } - 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 :) + for (int i = Math.min(n, max); i >= 1; i--) { + currentPartition[index] = i; + resultIndex = generatePartitions(n - i, i, index + 1, currentPartition, partitionResult, resultIndex); + } - 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; + return resultIndex; } + public static void main(String[] args) { - // you can test your code here + System.out.println(); } } diff --git a/AP1403 - Algorithms/src/main/java/tempCodeRunnerFile.java b/AP1403 - Algorithms/src/main/java/tempCodeRunnerFile.java new file mode 100644 index 0000000..f3acc41 --- /dev/null +++ b/AP1403 - Algorithms/src/main/java/tempCodeRunnerFile.java @@ -0,0 +1 @@ +resultIndex \ No newline at end of file