From 71dd8f72ee378ba58d3cf1113232aaa911a81129 Mon Sep 17 00:00:00 2001 From: DomTr <61636396+DomTr@users.noreply.github.com> Date: Tue, 17 Jun 2025 23:32:41 +0300 Subject: [PATCH 1/6] Shortest coprime segment using sliding window technique --- .../slidingwindow/ShortestCoprimeSegment.java | 117 ++++++++++++++++++ .../ShortestCoprimeSegmentTest.java | 39 ++++++ 2 files changed, 156 insertions(+) create mode 100644 src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java create mode 100644 src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java diff --git a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java new file mode 100644 index 000000000000..c6932ee19bd1 --- /dev/null +++ b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java @@ -0,0 +1,117 @@ +package com.thealgorithms.slidingwindow; + +import java.util.LinkedList; + +/** + * The Sliding Window technique together with 2-stack technique is used to find the minimal size of coprime segment in an array. + * Segment a[i],...,a[i+l] is coprime if gcd(a[i], a[i+1], ..., a[i+l]) = 1 + *

+ * Run-time complexity: O(n log n) + * What is special about this 2-stack technique is that it enables us to remove element a[i] and find gcd(a[i+1],...,a[i+l]) in amortized O(1) time. + * For 'remove' worst-case would be O(n) operation, but this happens rarely. + * Main observation is that each element gets processed a constant amount of times, hence complexity will be: + * O(n log n), where log n comes from complexity of gcd. + *

+ * The 2-stack technique enables us to 'remove' an element fast if it is known how to 'add' an element fast to the set. + * In our case 'adding' is calculating d' = gcd(a[i],...,a[i+l+1]), when d = gcd(a[i],...a[i]) with d' = gcd(d, a[i+l+1]). + * 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). + *

+ * One can change methods 'legalSegment' and function 'f' in DoubleStack to adapt this code to other Silding-window type problems. + * 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 + *

+ * 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) + * + * @author DomTr (https://github.com/DomTr) + */ +public class ShortestCoprimeSegment { + // Prevent instantiation + private ShortestCoprimeSegment() { + } + + /** + * @param arr is the input array + * @param n is the array size + * @return the length of the smallest segment in the array which has gcd equal to 1. If no such segment exists, returns -1 + */ + public static int shortestCoprimeSegment(int n, long[] arr) { + DoubleStack front = new DoubleStack(); + DoubleStack back = new DoubleStack(); + int l = 0, best = n + 1; + for (int i = 0; i < n; i++) { + back.push(arr[i]); + while (legalSegment(front, back)) { + remove(front, back); + best = Math.min(best, i - l + 1); + l++; + } + } + if (best > n) best = -1; + return best; + } + + private static boolean legalSegment(DoubleStack front, DoubleStack back) { + return gcd(front.top(), back.top()) == 1; + } + + private static long gcd(long a, long b) { + if (a < b) return gcd(b, a); + else if (b == 0) return a; + else return gcd(a % b, b); + } + + /** + * This solves the problem of removing elements quickly. + * Even though the worst case of 'remove' method is O(n), it is a very pessimistic view. + * We will need to empty out 'back', only when 'from' is empty. + * Consider element x when it is added to stack 'back'. + * After some time 'front' becomes empty and x goes to 'front'. Notice that in the for-loop we proceed further and x will never come back to any stacks 'back' or 'front'. + * In other words, every element gets processed by a constant number of operations. + * So 'remove' amortized runtime is actually O(n). + */ + private static void remove(DoubleStack front, DoubleStack back) { + if (front.isEmpty()) { + while (!back.isEmpty()) { + front.push(back.pop()); + } + } + front.pop(); + } + + /** + * 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. + */ + private static class DoubleStack { + LinkedList stack, values; + + public DoubleStack() { + values = new LinkedList<>(); + stack = new LinkedList<>(); + values.add((long) 0); // Initialise with 0 which is neutral element in terms of gcd, i.e. gcd(a,0) = a + } + + long f(long a, long b) { // Can be replaced with other function + return gcd(a, b); + } + + public void push(long x) { + stack.addLast(x); + values.addLast(f(values.getLast(), x)); + } + + public long top() { + return values.getLast(); + } + + public long pop() { + long res = stack.getLast(); + stack.removeLast(); + values.removeLast(); + return res; + } + + public boolean isEmpty() { + return stack.isEmpty(); + } + } + +} diff --git a/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java new file mode 100644 index 000000000000..e1ceff6d2921 --- /dev/null +++ b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java @@ -0,0 +1,39 @@ +package com.thealgorithms.slidingwindow; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit tests for ShortestCoprimeSegment algorithm + * + * @author DomTr (https://github.com/DomTr) + */ +public class ShortestCoprimeSegmentTest { + @Test + public void testShortestCoprimeSegment() { + assertEquals(3, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 9, 3, 6})); + assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 5, 9, 3, 6})); + assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(2, new long[]{3, 2})); + assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{3, 9, 9, 9, 10})); + assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[]{3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5})); + assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7})); + assertEquals(5, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7})); + 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})); + 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})); + 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})); + // Segment can consist of one element + assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 1, 3, 6})); + assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[]{1})); + } + + @Test + public void testNoCoprimeSegment() { + // There may not be a coprime segment + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 8, 12, 8})); + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[]{4, 4, 4, 4, 10, 4, 6, 8, 12, 8})); + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[]{100})); + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(3, new long[]{2, 2, 2})); + + } +} From 64cd545311ae0308601c762aeaed5a4b3e96bb42 Mon Sep 17 00:00:00 2001 From: DomTr <61636396+DomTr@users.noreply.github.com> Date: Wed, 18 Jun 2025 08:13:28 +0300 Subject: [PATCH 2/6] mvn checkstyle passes --- .../slidingwindow/ShortestCoprimeSegment.java | 31 +++++++++------ .../ShortestCoprimeSegmentTest.java | 39 +++++++++---------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java index c6932ee19bd1..22008caa742c 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java +++ b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java @@ -17,13 +17,13 @@ * 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). *

* One can change methods 'legalSegment' and function 'f' in DoubleStack to adapt this code to other Silding-window type problems. - * 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 + * I recommend this article for more explanations: Article 1 or https://usaco.guide/gold/sliding-window?lang=cpp#method-2---two-stacks *

* 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) * - * @author DomTr (https://github.com/DomTr) + * @author DomTr (Github) */ -public class ShortestCoprimeSegment { +public final class ShortestCoprimeSegment { // Prevent instantiation private ShortestCoprimeSegment() { } @@ -36,7 +36,8 @@ private ShortestCoprimeSegment() { public static int shortestCoprimeSegment(int n, long[] arr) { DoubleStack front = new DoubleStack(); DoubleStack back = new DoubleStack(); - int l = 0, best = n + 1; + int l = 0; + int best = n + 1; for (int i = 0; i < n; i++) { back.push(arr[i]); while (legalSegment(front, back)) { @@ -45,7 +46,9 @@ public static int shortestCoprimeSegment(int n, long[] arr) { l++; } } - if (best > n) best = -1; + if (best > n) { + best = -1; + } return best; } @@ -54,9 +57,15 @@ private static boolean legalSegment(DoubleStack front, DoubleStack back) { } private static long gcd(long a, long b) { - if (a < b) return gcd(b, a); - else if (b == 0) return a; - else return gcd(a % b, b); + if (a < b) { + return gcd(b, a); + } + else if (b == 0) { + return a; + } + else { + return gcd(a % b, b); + } } /** @@ -81,9 +90,10 @@ private static void remove(DoubleStack front, DoubleStack back) { * 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. */ private static class DoubleStack { - LinkedList stack, values; + LinkedList stack; + LinkedList values; - public DoubleStack() { + DoubleStack() { values = new LinkedList<>(); stack = new LinkedList<>(); 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() { return stack.isEmpty(); } } - } diff --git a/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java index e1ceff6d2921..a8729e9cb2aa 100644 --- a/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java +++ b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java @@ -1,39 +1,38 @@ package com.thealgorithms.slidingwindow; -import org.junit.jupiter.api.Test; - import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; + /** * Unit tests for ShortestCoprimeSegment algorithm * - * @author DomTr (https://github.com/DomTr) + * @author DomTr (...) */ public class ShortestCoprimeSegmentTest { @Test public void testShortestCoprimeSegment() { - assertEquals(3, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 9, 3, 6})); - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 5, 9, 3, 6})); - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(2, new long[]{3, 2})); - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{3, 9, 9, 9, 10})); - assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[]{3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5})); - assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7})); - assertEquals(5, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7})); - 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})); - 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})); - 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})); + assertEquals(3, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 9, 3, 6})); + assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 5, 9, 3, 6})); + assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(2, new long[] {3, 2})); + assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3, 9, 9, 9, 10})); + assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5})); + assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7})); + assertEquals(5, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7})); + 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})); + 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})); + 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})); // Segment can consist of one element - assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 1, 3, 6})); - assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[]{1})); + assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 1, 3, 6})); + assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {1})); } @Test public void testNoCoprimeSegment() { // There may not be a coprime segment - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[]{4, 6, 8, 12, 8})); - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[]{4, 4, 4, 4, 10, 4, 6, 8, 12, 8})); - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[]{100})); - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(3, new long[]{2, 2, 2})); - + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 8, 12, 8})); + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[] {4, 4, 4, 4, 10, 4, 6, 8, 12, 8})); + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {100})); + assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(3, new long[] {2, 2, 2})); } } From c78dd46c757e2eb28ca9168c605107fc3a4879d7 Mon Sep 17 00:00:00 2001 From: DomTr <61636396+DomTr@users.noreply.github.com> Date: Wed, 18 Jun 2025 08:59:47 +0300 Subject: [PATCH 3/6] gcd function reformatted --- .../thealgorithms/slidingwindow/ShortestCoprimeSegment.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java index 22008caa742c..d616af17458f 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java +++ b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java @@ -59,11 +59,9 @@ private static boolean legalSegment(DoubleStack front, DoubleStack back) { private static long gcd(long a, long b) { if (a < b) { return gcd(b, a); - } - else if (b == 0) { + } else if (b == 0) { return a; - } - else { + } else { return gcd(a % b, b); } } From 151fb27c23ea57d563e1fdd166281cb33f644134 Mon Sep 17 00:00:00 2001 From: DomTr <61636396+DomTr@users.noreply.github.com> Date: Wed, 18 Jun 2025 09:03:32 +0300 Subject: [PATCH 4/6] fixed typo in ShortestCoprimeSegment --- .../com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java index d616af17458f..3d46e61329f9 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java +++ b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java @@ -16,7 +16,7 @@ * In our case 'adding' is calculating d' = gcd(a[i],...,a[i+l+1]), when d = gcd(a[i],...a[i]) with d' = gcd(d, a[i+l+1]). * 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). *

- * One can change methods 'legalSegment' and function 'f' in DoubleStack to adapt this code to other Silding-window type problems. + * One can change methods 'legalSegment' and function 'f' in DoubleStack to adapt this code to other sliding-window type problems. * I recommend this article for more explanations: Article 1 or https://usaco.guide/gold/sliding-window?lang=cpp#method-2---two-stacks *

* 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) From 911061d3ffd1e0c14b5064a99d05312a7a5f67c8 Mon Sep 17 00:00:00 2001 From: DomTr <61636396+DomTr@users.noreply.github.com> Date: Wed, 18 Jun 2025 21:49:55 +0300 Subject: [PATCH 5/6] 1. shortestCoprimeSegment now returns not the length, but the shortest segment itself. 2. Testcases have been adapted, a few new ones added. --- .../slidingwindow/ShortestCoprimeSegment.java | 35 ++++++---- .../ShortestCoprimeSegmentTest.java | 64 +++++++++++++------ 2 files changed, 68 insertions(+), 31 deletions(-) diff --git a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java index 3d46e61329f9..b99f7ca7d62f 100644 --- a/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java +++ b/src/main/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegment.java @@ -1,9 +1,10 @@ package com.thealgorithms.slidingwindow; +import java.util.Arrays; import java.util.LinkedList; /** - * The Sliding Window technique together with 2-stack technique is used to find the minimal size of coprime segment in an array. + * The Sliding Window technique together with 2-stack technique is used to find coprime segment of minimal size in an array. * Segment a[i],...,a[i+l] is coprime if gcd(a[i], a[i+1], ..., a[i+l]) = 1 *

* Run-time complexity: O(n log n) @@ -12,12 +13,12 @@ * Main observation is that each element gets processed a constant amount of times, hence complexity will be: * O(n log n), where log n comes from complexity of gcd. *

- * The 2-stack technique enables us to 'remove' an element fast if it is known how to 'add' an element fast to the set. + * More generally, the 2-stack technique enables us to 'remove' an element fast if it is known how to 'add' an element fast to the set. * In our case 'adding' is calculating d' = gcd(a[i],...,a[i+l+1]), when d = gcd(a[i],...a[i]) with d' = gcd(d, a[i+l+1]). * 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). *

* One can change methods 'legalSegment' and function 'f' in DoubleStack to adapt this code to other sliding-window type problems. - * I recommend this article for more explanations: Article 1 or https://usaco.guide/gold/sliding-window?lang=cpp#method-2---two-stacks + * I recommend this article for more explanations: "CF Article">Article 1 or USACO Article *

* 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) * @@ -30,26 +31,36 @@ private ShortestCoprimeSegment() { /** * @param arr is the input array - * @param n is the array size - * @return the length of the smallest segment in the array which has gcd equal to 1. If no such segment exists, returns -1 + * @return shortest segment in the array which has gcd equal to 1. If no such segment exists or array is empty, returns empty array */ - public static int shortestCoprimeSegment(int n, long[] arr) { + public static long[] shortestCoprimeSegment(long[] arr) { + if (arr == null || arr.length == 0) { + return new long[] {}; + } DoubleStack front = new DoubleStack(); DoubleStack back = new DoubleStack(); + int n = arr.length; int l = 0; - int best = n + 1; + int shortestLength = n + 1; + int beginsAt = -1; // beginning index of the shortest coprime segment for (int i = 0; i < n; i++) { back.push(arr[i]); while (legalSegment(front, back)) { remove(front, back); - best = Math.min(best, i - l + 1); + if (shortestLength > i - l + 1) { + beginsAt = l; + shortestLength = i - l + 1; + } l++; } } - if (best > n) { - best = -1; + if (shortestLength > n) { + shortestLength = -1; + } + if (shortestLength == -1) { + return new long[] {}; } - return best; + return Arrays.copyOfRange(arr, beginsAt, beginsAt + shortestLength); } private static boolean legalSegment(DoubleStack front, DoubleStack back) { @@ -94,7 +105,7 @@ private static class DoubleStack { DoubleStack() { values = new LinkedList<>(); stack = new LinkedList<>(); - values.add((long) 0); // Initialise with 0 which is neutral element in terms of gcd, i.e. gcd(a,0) = a + values.add(0L); // Initialise with 0 which is neutral element in terms of gcd, i.e. gcd(a,0) = a } long f(long a, long b) { // Can be replaced with other function diff --git a/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java index a8729e9cb2aa..37b1e0c08682 100644 --- a/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java +++ b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java @@ -1,7 +1,7 @@ package com.thealgorithms.slidingwindow; -import static org.junit.jupiter.api.Assertions.assertEquals; - +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import java.util.Arrays; import org.junit.jupiter.api.Test; /** @@ -12,27 +12,53 @@ public class ShortestCoprimeSegmentTest { @Test public void testShortestCoprimeSegment() { - assertEquals(3, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 9, 3, 6})); - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 5, 9, 3, 6})); - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(2, new long[] {3, 2})); - assertEquals(2, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3, 9, 9, 9, 10})); - assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5})); - assertEquals(4, ShortestCoprimeSegment.shortestCoprimeSegment(4, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7})); - assertEquals(5, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7})); - 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})); - 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})); - 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})); + assertArrayEquals(new long[] {4, 6, 9}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 6, 9, 3, 6})); + assertArrayEquals(new long[] {4, 5}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 5, 9, 3, 6})); + assertArrayEquals(new long[] {3, 2}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {3, 2})); + assertArrayEquals(new long[] {9, 10}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {3, 9, 9, 9, 10})); + + long[] test5 = 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}; + long[] answer5 = Arrays.copyOfRange(test5, 0, test5.length - 1); + assertArrayEquals(answer5, ShortestCoprimeSegment.shortestCoprimeSegment(test5)); + + // Test suite, when the entire array needs to be taken + long[] test6 = new long[] {3 * 7, 7 * 5, 5 * 7 * 3, 3 * 5}; + assertArrayEquals(test6, ShortestCoprimeSegment.shortestCoprimeSegment(test6)); + + long[] test7 = new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 3 * 7}; + assertArrayEquals(test7, ShortestCoprimeSegment.shortestCoprimeSegment(test7)); + + long[] test8 = new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 5 * 7}; + assertArrayEquals(test8, ShortestCoprimeSegment.shortestCoprimeSegment(test8)); + + long[] test9 = new long[] {3 * 11, 11 * 7, 11 * 7 * 3, 11 * 7 * 3 * 5, 11 * 7 * 3 * 5 * 13, 7 * 13}; + assertArrayEquals(test9, ShortestCoprimeSegment.shortestCoprimeSegment(test9)); + + long[] test10 = 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}; + assertArrayEquals(test10, ShortestCoprimeSegment.shortestCoprimeSegment(test10)); + // Segment can consist of one element - assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 1, 3, 6})); - assertEquals(1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {1})); + long[] test11 = new long[] {1}; + assertArrayEquals(test11, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 6, 1, 3, 6})); + long[] test12 = new long[] {1}; + assertArrayEquals(test12, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {1})); + } + @Test + public void testShortestCoprimeSegment2() { + assertArrayEquals(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7, 2 * 3 * 5 * 7})); + assertArrayEquals(new long[] {5 * 7, 2}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7, 2})); + assertArrayEquals(new long[] {5 * 7, 2 * 5 * 7, 2 * 11}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2 * 3, 2 * 3 * 5, 2 * 3 * 5 * 7, 5 * 7, 2 * 5 * 7, 2 * 11})); + assertArrayEquals(new long[] {3 * 5 * 7, 2 * 3, 2}, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2, 2 * 3, 2 * 3 * 5, 3 * 5 * 7, 2 * 3, 2})); } - @Test public void testNoCoprimeSegment() { // There may not be a coprime segment - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(5, new long[] {4, 6, 8, 12, 8})); - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(10, new long[] {4, 4, 4, 4, 10, 4, 6, 8, 12, 8})); - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(1, new long[] {100})); - assertEquals(-1, ShortestCoprimeSegment.shortestCoprimeSegment(3, new long[] {2, 2, 2})); + long[] empty = new long[] {}; + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(null)); + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(empty)); + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 6, 8, 12, 8})); + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {4, 4, 4, 4, 10, 4, 6, 8, 12, 8})); + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {100})); + assertArrayEquals(empty, ShortestCoprimeSegment.shortestCoprimeSegment(new long[] {2, 2, 2})); } } From 8bcd7dbf0a6812c1227e120c8cd4b949cf126e56 Mon Sep 17 00:00:00 2001 From: DomTr <61636396+DomTr@users.noreply.github.com> Date: Wed, 18 Jun 2025 21:53:55 +0300 Subject: [PATCH 6/6] clang formatted ShortestCoprimeSegmentTest.java code --- .../thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java index 37b1e0c08682..acb9e1e30ac7 100644 --- a/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java +++ b/src/test/java/com/thealgorithms/slidingwindow/ShortestCoprimeSegmentTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.slidingwindow; import static org.junit.jupiter.api.Assertions.assertArrayEquals; + import java.util.Arrays; import org.junit.jupiter.api.Test;