Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added AP1403 - Algorithms/src/main/java/Exercises.class
Binary file not shown.
130 changes: 83 additions & 47 deletions AP1403 - Algorithms/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
1 change: 1 addition & 0 deletions AP1403 - Algorithms/src/main/java/tempCodeRunnerFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
resultIndex