Skip to content

1 and 2 solve #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
51 changes: 47 additions & 4 deletions AP1403 - Algorithms/src/main/java/Exercises.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ public class Exercises {
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) {
int[] arr = { i , j };
return arr;
}
}
}
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
Expand All @@ -25,8 +33,43 @@ public int[] productIndices(int[] values, int target) {
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[] answer = new int[rows * cols]; // آرایه خروجی
int index = 0; // اندیس برای پر کردن آرایه خروجی

int srow = 0, erow = rows - 1;
int scol = 0, ecol = cols - 1;

while (srow <= erow && scol <= ecol) {
for (int i = scol; i <= ecol; i++) {
answer[index] = values[srow][i];
index ++;
}
srow ++;

for (int i = srow; i <= erow; i++) {
answer[index] = values[i][ecol];
index ++;
}
ecol --;

if (srow <= erow) {
for (int i = ecol; i >= scol; i--) {
answer[index] = values[erow][i];
index ++;
}
erow --;
}

if (scol <= ecol) {
for (int i = erow; i >= srow; i--) {
answer[index] = values[i][scol];
index ++;
}
scol ++;
}
}

return answer;
}

/*
Expand Down