Skip to content

Commit a14ec6b

Browse files
committed
Added multiplication matrix problem and solution
1 parent 3a42294 commit a14ec6b

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Description
2+
3+
Good morning! Here's your coding interview problem for today.
4+
5+
This problem was asked by **Apple**.
6+
7+
Suppose you have a multiplication table that is N by N. That is, a 2D array where the value at the i-th row and j-th column is `(i + 1) * (j + 1)` (if 0-indexed) or `i * j` (if 1-indexed).
8+
9+
Given integers N and X, write a function that returns the number of times X appears as a value in an N by N multiplication table.
10+
11+
For example, given N = 6 and X = 12, you should return 4, since the multiplication table looks like this:
12+
13+
```
14+
| 1 | 2 | 3 | 4 | 5 | 6 |
15+
16+
| 2 | 4 | 6 | 8 | 10 | 12 |
17+
18+
| 3 | 6 | 9 | 12 | 15 | 18 |
19+
20+
| 4 | 8 | 12 | 16 | 20 | 24 |
21+
22+
| 5 | 10 | 15 | 20 | 25 | 30 |
23+
24+
| 6 | 12 | 18 | 24 | 30 | 36 |
25+
```
26+
27+
And there are 4 12's in the table.
28+
29+
# Source
30+
31+
Received by email from the [Daily Coding Problem](https://www.dailycodingproblem.com)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Idea behind the solution
2+
3+
We can simply check the divisors of the given X and count how many of those divisors multiplied by each other gets X.
4+
5+
For the `i-th` divisor we check all the previous `j-th` divisor, for `0 <= j <= i`. When `divisor_i * divisor_j == X` is `false` for every `j` in the on going iteration, we can return the current count since `divisor_i` will be smaller in the next iteration and every upcoming check of `divisor_i * divisor_j == X` will be `false`.
6+
7+
The running time in the worst case is `O(n^2)` for n being the length of the matrix.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
5+
private static List<Integer> divisors(int n, int maxDivisor) {
6+
7+
final int min = Math.min(n / 2, maxDivisor);
8+
List<Integer> divisors = new ArrayList<>(min);
9+
10+
for (int i = 2; i <= min; i++)
11+
if (n % i == 0)
12+
divisors.add((i));
13+
14+
return divisors;
15+
}
16+
17+
private static int solve(int n, int x) {
18+
19+
int count = 0;
20+
List<Integer> divisors = divisors(x, n);
21+
System.out.println(divisors);
22+
boolean foundAny = true;
23+
for (int i = divisors.size() - 1; foundAny && i >= 0; i--) {
24+
foundAny = false;
25+
for (int j = i; j >= 0; j--) {
26+
if (divisors.get(i) * divisors.get(j) != x) {
27+
continue;
28+
}
29+
30+
foundAny = true;
31+
32+
if (i == j)
33+
count++;
34+
else
35+
count += 2;
36+
}
37+
}
38+
39+
return count;
40+
}
41+
42+
public static void main(String[] args) {
43+
System.out.println(solve(6, 12)); // 4
44+
System.out.println(solve(6, 9)); // 1
45+
System.out.println(solve(6, 36)); // 1
46+
System.out.println(solve(6, 20)); // 2
47+
System.out.println(solve(6, 30)); // 2
48+
}
49+
}

0 commit comments

Comments
 (0)