Skip to content

Commit 64cd545

Browse files
committed
mvn checkstyle passes
1 parent 71dd8f7 commit 64cd545

File tree

2 files changed

+39
-31
lines changed

2 files changed

+39
-31
lines changed

src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
* and removing is find gcd(a[i+1],...,a[i+l]). We don't calculate it explicitly, but it is pushed in the stack which we can pop in O(1).
1818
* <p>
1919
* One can change methods 'legalSegment' and function 'f' in DoubleStack to adapt this code to other Silding-window type problems.
20-
* I recommend this article for more explanations: https://codeforces.com/edu/course/2/lesson/9/2 or https://usaco.guide/gold/sliding-window?lang=cpp#method-2---two-stacks
20+
* I recommend this article for more explanations: <a href="https://codeforces.com/edu/course/2/lesson/9/2">Article 1</a> or https://usaco.guide/gold/sliding-window?lang=cpp#method-2---two-stacks
2121
* <p>
2222
* Another method to solve this problem is through segment trees. Then query operation would have O(log n), not O(1) time, but runtime complexity would still be O(n log n)
2323
*
24-
* @author DomTr (https://github.com/DomTr)
24+
* @author DomTr (<a href="https://github.com/DomTr">Github</a>)
2525
*/
26-
public class ShortestCoprimeSegment {
26+
public final class ShortestCoprimeSegment {
2727
// Prevent instantiation
2828
private ShortestCoprimeSegment() {
2929
}
@@ -36,7 +36,8 @@ private ShortestCoprimeSegment() {
3636
public static int shortestCoprimeSegment(int n, long[] arr) {
3737
DoubleStack front = new DoubleStack();
3838
DoubleStack back = new DoubleStack();
39-
int l = 0, best = n + 1;
39+
int l = 0;
40+
int best = n + 1;
4041
for (int i = 0; i < n; i++) {
4142
back.push(arr[i]);
4243
while (legalSegment(front, back)) {
@@ -45,7 +46,9 @@ public static int shortestCoprimeSegment(int n, long[] arr) {
4546
l++;
4647
}
4748
}
48-
if (best > n) best = -1;
49+
if (best > n) {
50+
best = -1;
51+
}
4952
return best;
5053
}
5154

@@ -54,9 +57,15 @@ private static boolean legalSegment(DoubleStack front, DoubleStack back) {
5457
}
5558

5659
private static long gcd(long a, long b) {
57-
if (a < b) return gcd(b, a);
58-
else if (b == 0) return a;
59-
else return gcd(a % b, b);
60+
if (a < b) {
61+
return gcd(b, a);
62+
}
63+
else if (b == 0) {
64+
return a;
65+
}
66+
else {
67+
return gcd(a % b, b);
68+
}
6069
}
6170

6271
/**
@@ -81,9 +90,10 @@ private static void remove(DoubleStack front, DoubleStack back) {
8190
* DoubleStack serves as a collection of two stacks. One is a normal stack called 'stack', the other 'values' stores gcd-s up until some index.
8291
*/
8392
private static class DoubleStack {
84-
LinkedList<Long> stack, values;
93+
LinkedList<Long> stack;
94+
LinkedList<Long> values;
8595

86-
public DoubleStack() {
96+
DoubleStack() {
8797
values = new LinkedList<>();
8898
stack = new LinkedList<>();
8999
values.add((long) 0); // Initialise with 0 which is neutral element in terms of gcd, i.e. gcd(a,0) = a
@@ -113,5 +123,4 @@ public boolean isEmpty() {
113123
return stack.isEmpty();
114124
}
115125
}
116-
117126
}
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11
package com.thealgorithms.slidingwindow;
22

3-
import org.junit.jupiter.api.Test;
4-
53
import static org.junit.jupiter.api.Assertions.assertEquals;
64

5+
import org.junit.jupiter.api.Test;
6+
77
/**
88
* Unit tests for ShortestCoprimeSegment algorithm
99
*
10-
* @author DomTr (https://github.com/DomTr)
10+
* @author DomTr (<a href="https://github.com/DomTr">...</a>)
1111
*/
1212
public class ShortestCoprimeSegmentTest {
1313
@Test
1414
public void testShortestCoprimeSegment() {
15-
assertEquals(3, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 9, 3, 6}));
16-
assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 5, 9, 3, 6}));
17-
assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(2, new long[]{3, 2}));
18-
assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{3, 9, 9, 9, 10}));
19-
assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[]{3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5}));
20-
assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7}));
21-
assertEquals(5, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7}));
22-
assertEquals(6, ShortestCoprimeSegment.shortestCoprimeSegment(6, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13}));
23-
assertEquals(6, ShortestCoprimeSegment.shortestCoprimeSegment(7, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13, 11 * 7 * 3 * 5 * 13}));
24-
assertEquals(10, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[]{3 * 11, 7 * 11, 3 * 7 * 11, 3 * 5 * 7 * 11, 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13 * 17, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23, 7 * 13}));
15+
assertEquals(3, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 9, 3, 6}));
16+
assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 5, 9, 3, 6}));
17+
assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(2, new long[] {3, 2}));
18+
assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3, 9, 9, 9, 10}));
19+
assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5}));
20+
assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7}));
21+
assertEquals(5, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7}));
22+
assertEquals(6, ShortestCoprimeSegment.shortestCoprimeSegment(6, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13}));
23+
assertEquals(6, ShortestCoprimeSegment.shortestCoprimeSegment(7, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13, 11 * 7 * 3 * 5 * 13}));
24+
assertEquals(10, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[] {3 * 11, 7 * 11, 3 * 7 * 11, 3 * 5 * 7 * 11, 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13, 2 * 3 * 5 * 7 * 11 * 13 * 17, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23, 7 * 13}));
2525
// Segment can consist of one element
26-
assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 1, 3, 6}));
27-
assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[]{1}));
26+
assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 1, 3, 6}));
27+
assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {1}));
2828
}
2929

3030
@Test
3131
public void testNoCoprimeSegment() {
3232
// There may not be a coprime segment
33-
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 8, 12, 8}));
34-
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[]{4, 4, 4, 4, 10, 4, 6, 8, 12, 8}));
35-
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[]{100}));
36-
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(3, new long[]{2, 2, 2}));
37-
33+
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 8, 12, 8}));
34+
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[] {4, 4, 4, 4, 10, 4, 6, 8, 12, 8}));
35+
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {100}));
36+
assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(3, new long[] {2, 2, 2}));
3837
}
3938
}

0 commit comments

Comments
 (0)