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
2 changes: 1 addition & 1 deletion AP1403 - Algorithms/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

135 changes: 115 additions & 20 deletions AP1403 - Algorithms/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,83 @@
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
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
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++) {
if (values[i] * values[j] == target) {
return new int[]{i, j};
}
}
}
return new int[]{}; // Return empty array if no pair is found
}

/*
given a matrix of random integers, you should do spiral traversal in it
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
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;
int bottom = rows - 1;
int left = 0;
int right = cols - 1;

while (top <= bottom && left <= right) {
// Traverse from left to right
for (int i = left; i <= right; i++) {
result[index++] = values[top][i];
}
top++;

// Traverse from top to bottom
for (int i = top; i <= bottom; i++) {
result[index++] = values[i][right];
}
right--;

// Traverse from right to left
if (top <= bottom) {
for (int i = right; i >= left; i--) {
result[index++] = values[bottom][i];
}
bottom--;
}

// Traverse from bottom to top
if (left <= right) {
for (int i = bottom; i >= top; i--) {
result[index++] = values[i][left];
}
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
Integer partitioning is a combinatorics problem in discrete maths.
The problem is to generate sum numbers which their summation is the input number.

e.g. 1 -> all partitions of integer 3 are:
3
Expand All @@ -45,20 +91,69 @@ public int[] spiralTraversal(int[][] values, int rows, int cols) {
2, 1, 1
1, 1, 1, 1

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
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.

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 :)
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
If you're familiar with lists and ArrayLists, you can also edit the method's body to use them instead of array.
*/
public int[][] intPartitions(int n) {
// todo
return null;
List<int[]> partitions = new ArrayList<>();
generatePartitions(n, n, new ArrayList<>(), partitions);

// Convert List<int[]> to int[][]
int[][] result = new int[partitions.size()][];
for (int i = 0; i < partitions.size(); i++) {
result[i] = partitions.get(i);
}

return result;
}

private void generatePartitions(int n, int max, List<Integer> current, List<int[]> result) {
if (n == 0) {
int[] partition = new int[current.size()];
for (int i = 0; i < current.size(); i++) {
partition[i] = current.get(i);
}
result.add(partition);
} else {
for (int i = Math.min(n, max); i >= 1; i--) {
current.add(i);
generatePartitions(n - i, i, current, result);
current.remove(current.size() - 1);
}
}
}

public static void main(String[] args) {
// you can test your code here
Exercises exercises = new Exercises();

// Test productIndices
int[] values = {1, 2, 3, 4};
int target = 8;
int[] indices = exercises.productIndices(values, target);
System.out.println("Indices for product " + target + ": " + Arrays.toString(indices));

// Test spiralTraversal
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
int rows = matrix.length;
int cols = matrix[0].length;
int[] spiral = exercises.spiralTraversal(matrix, rows, cols);
System.out.println("Spiral traversal: " + Arrays.toString(spiral));

// Test intPartitions
int n = 4;
int[][] partitions = exercises.intPartitions(n);
System.out.println("Partitions of " + n + ":");
for (int[] partition : partitions) {
System.out.println(Arrays.toString(partition));
}
}
}
8 changes: 8 additions & 0 deletions alghorithms/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions alghorithms/.idea/alghorithms.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions alghorithms/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions alghorithms/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions alghorithms/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.