Skip to content

Commit 6edb343

Browse files
Merge branch 'master' into testing/AverageTest
2 parents 4008ef0 + 434ab50 commit 6edb343

File tree

2 files changed

+31
-31
lines changed

2 files changed

+31
-31
lines changed

src/main/java/com/thealgorithms/maths/Convolution.java

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,21 @@ public static double[] convolution(double[] a, double[] b) {
2323
double[] convolved = new double[a.length + b.length - 1];
2424

2525
/*
26-
The discrete convolution of two signals A and B is defined as:
27-
28-
A.length
29-
C[i] = Σ (A[k]*B[i-k])
30-
k=0
31-
32-
It's obvious that: 0 <= k <= A.length , 0 <= i <= A.length + B.length - 2 and 0 <= i-k <=
33-
B.length - 1 From the last inequality we get that: i - B.length + 1 <= k <= i and thus we get
34-
the conditions below.
26+
* Discrete convolution formula:
27+
* C[i] = Σ A[k] * B[i - k]
28+
* where k ranges over valid indices so that both A[k] and B[i-k] are in bounds.
3529
*/
30+
3631
for (int i = 0; i < convolved.length; i++) {
37-
convolved[i] = 0;
38-
int k = Math.max(i - b.length + 1, 0);
32+
double sum = 0;
33+
int kStart = Math.max(0, i - b.length + 1);
34+
int kEnd = Math.min(i, a.length - 1);
3935

40-
while (k < i + 1 && k < a.length) {
41-
convolved[i] += a[k] * b[i - k];
42-
k++;
36+
for (int k = kStart; k <= kEnd; k++) {
37+
sum += a[k] * b[i - k];
4338
}
39+
40+
convolved[i] = sum;
4441
}
4542

4643
return convolved;

src/main/java/com/thealgorithms/maths/Mode.java

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,49 @@
33
import java.util.ArrayList;
44
import java.util.Collections;
55
import java.util.HashMap;
6+
import java.util.List;
7+
import java.util.Map;
68

7-
/*
8-
* Find the mode of an array of numbers
9-
*
10-
* The mode of an array of numbers is the most frequently occurring number in the array,
11-
* or the most frequently occurring numbers if there are multiple numbers with the same frequency
9+
/**
10+
* Utility class to calculate the mode(s) of an array of integers.
11+
* <p>
12+
* The mode of an array is the integer value(s) that occur most frequently.
13+
* If multiple values have the same highest frequency, all such values are returned.
14+
* </p>
1215
*/
1316
public final class Mode {
1417
private Mode() {
1518
}
1619

17-
/*
18-
* Find the mode of an array of integers
20+
/**
21+
* Computes the mode(s) of the specified array of integers.
22+
* <p>
23+
* If the input array is empty, this method returns {@code null}.
24+
* If multiple numbers share the highest frequency, all are returned in the result array.
25+
* </p>
1926
*
20-
* @param numbers array of integers
21-
* @return mode of the array
27+
* @param numbers an array of integers to analyze
28+
* @return an array containing the mode(s) of the input array, or {@code null} if the input is empty
2229
*/
2330
public static int[] mode(final int[] numbers) {
2431
if (numbers.length == 0) {
2532
return null;
2633
}
2734

28-
HashMap<Integer, Integer> count = new HashMap<>();
35+
Map<Integer, Integer> count = new HashMap<>();
2936

3037
for (int num : numbers) {
31-
if (count.containsKey(num)) {
32-
count.put(num, count.get(num) + 1);
33-
} else {
34-
count.put(num, 1);
35-
}
38+
count.put(num, count.getOrDefault(num, 0) + 1);
3639
}
3740

3841
int max = Collections.max(count.values());
39-
ArrayList<Integer> modes = new ArrayList<>();
42+
List<Integer> modes = new ArrayList<>();
4043

4144
for (final var entry : count.entrySet()) {
4245
if (entry.getValue() == max) {
4346
modes.add(entry.getKey());
4447
}
4548
}
46-
return modes.stream().mapToInt(n -> n).toArray();
49+
return modes.stream().mapToInt(Integer::intValue).toArray();
4750
}
4851
}

0 commit comments

Comments
 (0)