-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathExercises.java
More file actions
107 lines (89 loc) · 3.35 KB
/
Exercises.java
File metadata and controls
107 lines (89 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
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) {
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
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) {
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;
}
/*
integer partitioning is a combinatorics problem in discreet 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
2, 1
1, 1, 1
e.g. 2 -> for number 4 goes as:
4
3, 1
2, 2
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
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;
}
public static void main(String[] args) {
// you can test your code here
}
}