From 54c214cdb04062565b55ae7d8aefd3828671bf60 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 15:42:59 +0530 Subject: [PATCH 01/39] Add array rotation utility using reversal algorithm --- .../thealgorithms/others/ArrayRotation.java | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/ArrayRotation.java diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java new file mode 100644 index 000000000000..cd0b18595824 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -0,0 +1,85 @@ +package com.thealgorithms.others; + +import java.util.Arrays; + +/** + * Array Rotation Utility + * + * Supports: + * 1. Left Rotation + * 2. Right Rotation + * + * Approach: + * Reversal Algorithm + * + * Time Complexity: O(n) + * Space Complexity: O(1) + */ + +public class ArrayRotation { + + /** + * Rotates the array to the right by k positions. + * + * @param nums the input array + * @param k number of rotations + */ + + public static void rotateRight(int[] nums, int k) { + + int n = nums.length; + + if (n == 0) { + return; + } + + k = k % n; + + reverse(nums, 0, n - 1); + reverse(nums, 0, k - 1); + reverse(nums, k, n - 1); + + } + + /** + * Rotates the array to the left by k positions. + * + * @param nums the input array + * @param k number of rotations + */ + + public static void rotateLeft(int[] nums, int k) { + + int n = nums.length; + + if (n == 0) { + return; + } + + k = k % n; + + reverse(nums, 0, k - 1); + reverse(nums, k, n - 1); + reverse(nums, 0, n - 1); + } + + /** + * Reverses elements between start and end indices. + * + * @param nums the input array + * @param start starting index + * @param end ending index + */ + private static void reverse(int[] nums, int start, int end) { + + while (start < end) { + + int temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; + + start++; + end--; + } + } +} From cb3fea7e0d1f878e4aeda212181d83ad7fe40720 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 19:21:19 +0530 Subject: [PATCH 02/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index cd0b18595824..be1bf2f27ec4 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -1,7 +1,5 @@ package com.thealgorithms.others; -import java.util.Arrays; - /** * Array Rotation Utility * @@ -18,6 +16,9 @@ public class ArrayRotation { + private ArrayRotation() { + } + /** * Rotates the array to the right by k positions. * From 8376bbe526de19126b9c50cd76449a7669badb4f Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 19:37:53 +0530 Subject: [PATCH 03/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index be1bf2f27ec4..3e2f8c57ceef 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -39,7 +39,6 @@ public static void rotateRight(int[] nums, int k) { reverse(nums, 0, n - 1); reverse(nums, 0, k - 1); reverse(nums, k, n - 1); - } /** From df2db03072fb652fa282aa322d743c3b6c521f28 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 19:42:37 +0530 Subject: [PATCH 04/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 3e2f8c57ceef..7ef4c3472b95 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -13,7 +13,6 @@ * Time Complexity: O(n) * Space Complexity: O(1) */ - public class ArrayRotation { private ArrayRotation() { @@ -25,7 +24,6 @@ private ArrayRotation() { * @param nums the input array * @param k number of rotations */ - public static void rotateRight(int[] nums, int k) { int n = nums.length; @@ -47,7 +45,6 @@ public static void rotateRight(int[] nums, int k) { * @param nums the input array * @param k number of rotations */ - public static void rotateLeft(int[] nums, int k) { int n = nums.length; @@ -82,4 +79,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } -} +} \ No newline at end of file From d729f7b42171837d52bb92ed2387e3e891d25de2 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 19:51:25 +0530 Subject: [PATCH 05/39] Fix checkstyle issues --- .../thealgorithms/others/ArrayRotation.java | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 7ef4c3472b95..804ddb8de384 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -15,68 +15,68 @@ */ public class ArrayRotation { - private ArrayRotation() { - } + private ArrayRotation() { + } - /** - * Rotates the array to the right by k positions. - * - * @param nums the input array - * @param k number of rotations - */ - public static void rotateRight(int[] nums, int k) { + /** + * Rotates the array to the right by k positions. + * + * @param nums the input array + * @param k number of rotations + */ + public static void rotateRight(int[] nums, int k) { - int n = nums.length; + int n = nums.length; - if (n == 0) { - return; - } + if (n == 0) { + return; + } - k = k % n; + k = k % n; - reverse(nums, 0, n - 1); - reverse(nums, 0, k - 1); - reverse(nums, k, n - 1); - } + reverse(nums, 0, n - 1); + reverse(nums, 0, k - 1); + reverse(nums, k, n - 1); + } - /** - * Rotates the array to the left by k positions. - * - * @param nums the input array - * @param k number of rotations - */ - public static void rotateLeft(int[] nums, int k) { + /** + * Rotates the array to the left by k positions. + * + * @param nums the input array + * @param k number of rotations + */ + public static void rotateLeft(int[] nums, int k) { - int n = nums.length; + int n = nums.length; - if (n == 0) { - return; - } + if (n == 0) { + return; + } - k = k % n; + k = k % n; - reverse(nums, 0, k - 1); - reverse(nums, k, n - 1); - reverse(nums, 0, n - 1); - } + reverse(nums, 0, k - 1); + reverse(nums, k, n - 1); + reverse(nums, 0, n - 1); + } - /** - * Reverses elements between start and end indices. - * - * @param nums the input array - * @param start starting index - * @param end ending index - */ - private static void reverse(int[] nums, int start, int end) { + /** + * Reverses elements between start and end indices. + * + * @param nums the input array + * @param start starting index + * @param end ending index + */ + private static void reverse(int[] nums, int start, int end) { - while (start < end) { + while (start < end) { - int temp = nums[start]; - nums[start] = nums[end]; - nums[end] = temp; + int temp = nums[start]; + nums[start] = nums[end]; + nums[end] = temp; - start++; - end--; + start++; + end--; + } } - } } \ No newline at end of file From e3d8cb133530d2be7d7b01de2a03e1dc404e9ff5 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 19:53:48 +0530 Subject: [PATCH 06/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 804ddb8de384..9d4f3663d3c9 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -79,4 +79,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } -} \ No newline at end of file + } \ No newline at end of file From 3fc7f458abf144c752eb775394879bdf30fcd311 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 19:55:30 +0530 Subject: [PATCH 07/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 9d4f3663d3c9..64df6d7380f4 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -79,4 +79,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } - } \ No newline at end of file + } \ No newline at end of file From 8831d241a19c4a8b7ed23cce512a8d19f85a1a4f Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 19:57:11 +0530 Subject: [PATCH 08/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 64df6d7380f4..804ddb8de384 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -79,4 +79,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } - } \ No newline at end of file +} \ No newline at end of file From 0940d5eb55281fe81a1a11fed4aa43e71af3463a Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:00:59 +0530 Subject: [PATCH 09/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 804ddb8de384..e82128c3e3cd 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -79,4 +79,5 @@ private static void reverse(int[] nums, int start, int end) { end--; } } + } \ No newline at end of file From 4adc28c71d0a9eb5085c1297bfaeb37c3545fd57 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:02:57 +0530 Subject: [PATCH 10/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index e82128c3e3cd..4241f9c2811b 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -78,6 +78,5 @@ private static void reverse(int[] nums, int start, int end) { start++; end--; } - } - -} \ No newline at end of file + } + } \ No newline at end of file From f28d8fc6080e5a44cbac02451e7418024cfa1efe Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:06:26 +0530 Subject: [PATCH 11/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 4241f9c2811b..804ddb8de384 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -78,5 +78,5 @@ private static void reverse(int[] nums, int start, int end) { start++; end--; } - } - } \ No newline at end of file + } +} \ No newline at end of file From d1152ce0d495846003ce05157d63cf958fff6508 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:08:55 +0530 Subject: [PATCH 12/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 804ddb8de384..9d4f3663d3c9 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -79,4 +79,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } -} \ No newline at end of file + } \ No newline at end of file From 52a57dd1e7f1a0771321ae16175fd59b60b89934 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:10:37 +0530 Subject: [PATCH 13/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 9d4f3663d3c9..ab42cb3a2593 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -79,4 +79,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } - } \ No newline at end of file + } \ No newline at end of file From f81884d698b9f7ecffba44d1f24810e1b83205d6 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:12:02 +0530 Subject: [PATCH 14/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index ab42cb3a2593..64df6d7380f4 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -79,4 +79,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } - } \ No newline at end of file + } \ No newline at end of file From 43731f0b52385ad8d6fd04db7c698d50c9a3ec91 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:14:58 +0530 Subject: [PATCH 15/39] Fix checkstyle issues --- .../java/com/thealgorithms/others/ArrayRotation.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 64df6d7380f4..31d7b145e49c 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -22,10 +22,9 @@ private ArrayRotation() { * Rotates the array to the right by k positions. * * @param nums the input array - * @param k number of rotations + * @param k number of rotations */ public static void rotateRight(int[] nums, int k) { - int n = nums.length; if (n == 0) { @@ -43,10 +42,9 @@ public static void rotateRight(int[] nums, int k) { * Rotates the array to the left by k positions. * * @param nums the input array - * @param k number of rotations + * @param k number of rotations */ public static void rotateLeft(int[] nums, int k) { - int n = nums.length; if (n == 0) { @@ -65,12 +63,10 @@ public static void rotateLeft(int[] nums, int k) { * * @param nums the input array * @param start starting index - * @param end ending index + * @param end ending index */ private static void reverse(int[] nums, int start, int end) { - while (start < end) { - int temp = nums[start]; nums[start] = nums[end]; nums[end] = temp; @@ -79,4 +75,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } - } \ No newline at end of file +} \ No newline at end of file From bb2df0076be66cfec1bf9e219adc2574c1a6adfa Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:17:21 +0530 Subject: [PATCH 16/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 31d7b145e49c..6899a41ec151 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -75,4 +75,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } -} \ No newline at end of file + } \ No newline at end of file From a21bed0a352354f19e32cf5e080f1b95efb2bf7e Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:19:05 +0530 Subject: [PATCH 17/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 6899a41ec151..a0e9b5c6f955 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -74,5 +74,5 @@ private static void reverse(int[] nums, int start, int end) { start++; end--; } - } - } \ No newline at end of file + } + } \ No newline at end of file From f9e24d0696cbd140e70d2374e048a7402202266f Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:21:46 +0530 Subject: [PATCH 18/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index a0e9b5c6f955..8afd5abbef14 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -61,7 +61,7 @@ public static void rotateLeft(int[] nums, int k) { /** * Reverses elements between start and end indices. * - * @param nums the input array + * @param nums the input array * @param start starting index * @param end ending index */ @@ -74,5 +74,5 @@ private static void reverse(int[] nums, int start, int end) { start++; end--; } - } - } \ No newline at end of file + } +} \ No newline at end of file From a18a7c816fd11800d487e76040d1237baa0072a4 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:23:37 +0530 Subject: [PATCH 19/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index 8afd5abbef14..e339e98aa7f3 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -75,4 +75,4 @@ private static void reverse(int[] nums, int start, int end) { end--; } } -} \ No newline at end of file +} From 6a7d2c72c694c5436d9bc07979147f7de86ad040 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 20:26:58 +0530 Subject: [PATCH 20/39] Fix checkstyle issues --- src/main/java/com/thealgorithms/others/ArrayRotation.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/ArrayRotation.java b/src/main/java/com/thealgorithms/others/ArrayRotation.java index e339e98aa7f3..7e3f93d3a9d0 100644 --- a/src/main/java/com/thealgorithms/others/ArrayRotation.java +++ b/src/main/java/com/thealgorithms/others/ArrayRotation.java @@ -13,7 +13,7 @@ * Time Complexity: O(n) * Space Complexity: O(1) */ -public class ArrayRotation { +public final class ArrayRotation { private ArrayRotation() { } From 81d90cf0eaa892a72aabbf4cb3af8e662aa1b863 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:08:01 +0530 Subject: [PATCH 21/39] Add unit tests for ArrayRotation --- .../others/ArrayRotationTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/main/java/com/thealgorithms/others/ArrayRotationTest.java diff --git a/src/main/java/com/thealgorithms/others/ArrayRotationTest.java b/src/main/java/com/thealgorithms/others/ArrayRotationTest.java new file mode 100644 index 000000000000..356a683be468 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/ArrayRotationTest.java @@ -0,0 +1,62 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +public class ArrayRotationTest { + + @Test + void shouldRotateArrayRightByTwoPositions() { + int[] values = {1, 2, 3, 4, 5}; + + ArrayRotation.rotateRight(values, 2); + + assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); + } + + @Test + void shouldRotateArrayLeftByTwoPositions() { + int[] values = {1, 2, 3, 4, 5}; + + ArrayRotation.rotateLeft(values, 2); + + assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); + } + + @Test + void shouldHandleRotationGreaterThanArrayLength() { + int[] values = {10, 20, 30, 40}; + + ArrayRotation.rotateRight(values, 6); + + assertArrayEquals(new int[] {30, 40, 10, 20}, values); + } + + @Test + void shouldKeepSingleElementArrayUnchanged() { + int[] values = {99}; + + ArrayRotation.rotateLeft(values, 5); + + assertArrayEquals(new int[] {99}, values); + } + + @Test + void shouldHandleEmptyArrayWithoutErrors() { + int[] values = {}; + + ArrayRotation.rotateRight(values, 3); + + assertArrayEquals(new int[] {}, values); + } + + @Test + void shouldReturnOriginalArrayWhenRotationIsZero() { + int[] values = {7, 8, 9}; + + ArrayRotation.rotateLeft(values, 0); + + assertArrayEquals(new int[] {7, 8, 9}, values); + } +} \ No newline at end of file From c11d0ca6fd0c0d5ceba034932ae02e49f2429e22 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:14:05 +0530 Subject: [PATCH 22/39] Add unit tests for array rotation utility --- .../others/ArrayRotationTest.java | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/test/java/com/thealgorithms/others/ArrayRotationTest.java diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java new file mode 100644 index 000000000000..283613d92d23 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -0,0 +1,62 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +public class ArrayRotationTest { + + @Test + void shouldRotateArrayRightByTwoPositions() { + int[] values = { 1, 2, 3, 4, 5 }; + + ArrayRotation.rotateRight(values, 2); + + assertArrayEquals(new int[] { 4, 5, 1, 2, 3 }, values); + } + + @Test + void shouldRotateArrayLeftByTwoPositions() { + int[] values = { 1, 2, 3, 4, 5 }; + + ArrayRotation.rotateLeft(values, 2); + + assertArrayEquals(new int[] { 3, 4, 5, 1, 2 }, values); + } + + @Test + void shouldHandleRotationGreaterThanArrayLength() { + int[] values = { 10, 20, 30, 40 }; + + ArrayRotation.rotateRight(values, 6); + + assertArrayEquals(new int[] { 30, 40, 10, 20 }, values); + } + + @Test + void shouldKeepSingleElementArrayUnchanged() { + int[] values = { 99 }; + + ArrayRotation.rotateLeft(values, 5); + + assertArrayEquals(new int[] { 99 }, values); + } + + @Test + void shouldHandleEmptyArrayWithoutErrors() { + int[] values = {}; + + ArrayRotation.rotateRight(values, 3); + + assertArrayEquals(new int[] {}, values); + } + + @Test + void shouldReturnOriginalArrayWhenRotationIsZero() { + int[] values = { 7, 8, 9 }; + + ArrayRotation.rotateLeft(values, 0); + + assertArrayEquals(new int[] { 7, 8, 9 }, values); + } +} From 02893973cd4e97380febc3cc591efb001ce630cd Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:15:48 +0530 Subject: [PATCH 23/39] Add unit tests for array rotation utility --- .../java/com/thealgorithms/others/ArrayLeftRotationTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java index b31b7d825ed5..fe1655e1bae7 100644 --- a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java @@ -52,3 +52,4 @@ void testForEmptyArray() { assertArrayEquals(arr, result); } } + From 1226614683c658aa5d38f1553e1921595ea5cb1c Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:18:05 +0530 Subject: [PATCH 24/39] Fix formatting in ArrayRotation tests --- .../others/ArrayLeftRotationTest.java | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java index fe1655e1bae7..f43448483021 100644 --- a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java @@ -4,52 +4,59 @@ import org.junit.jupiter.api.Test; -class ArrayLeftRotationTest { +public class ArrayRotationTest { @Test - void testForOneElement() { - int[] arr = {3}; - int[] result = ArrayLeftRotation.rotateLeft(arr, 3); - assertArrayEquals(arr, result); + void shouldRotateArrayRightByTwoPositions() { + int[] values = {1, 2, 3, 4, 5}; + + ArrayRotation.rotateRight(values, 2); + + assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); } @Test - void testForZeroStep() { - int[] arr = {3, 1, 5, 8, 6}; - int[] result = ArrayLeftRotation.rotateLeft(arr, 0); - assertArrayEquals(arr, result); + void shouldRotateArrayLeftByTwoPositions() { + int[] values = {1, 2, 3, 4, 5}; + + ArrayRotation.rotateLeft(values, 2); + + assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); } @Test - void testForEqualSizeStep() { - int[] arr = {3, 1, 5, 8, 6}; - int[] result = ArrayLeftRotation.rotateLeft(arr, 5); - assertArrayEquals(arr, result); + void shouldHandleRotationGreaterThanArrayLength() { + int[] values = {10, 20, 30, 40}; + + ArrayRotation.rotateRight(values, 6); + + assertArrayEquals(new int[] {30, 40, 10, 20}, values); } @Test - void testForLowerSizeStep() { - int[] arr = {3, 1, 5, 8, 6}; - int n = 2; - int[] expected = {5, 8, 6, 3, 1}; - int[] result = ArrayLeftRotation.rotateLeft(arr, n); - assertArrayEquals(expected, result); + void shouldKeepSingleElementArrayUnchanged() { + int[] values = {99}; + + ArrayRotation.rotateLeft(values, 5); + + assertArrayEquals(new int[] {99}, values); } @Test - void testForHigherSizeStep() { - int[] arr = {3, 1, 5, 8, 6}; - int n = 7; - int[] expected = {5, 8, 6, 3, 1}; - int[] result = ArrayLeftRotation.rotateLeft(arr, n); - assertArrayEquals(expected, result); + void shouldHandleEmptyArrayWithoutErrors() { + int[] values = {}; + + ArrayRotation.rotateRight(values, 3); + + assertArrayEquals(new int[] {}, values); } @Test - void testForEmptyArray() { - int[] arr = {}; - int[] result = ArrayLeftRotation.rotateLeft(arr, 3); - assertArrayEquals(arr, result); + void shouldReturnOriginalArrayWhenRotationIsZero() { + int[] values = {7, 8, 9}; + + ArrayRotation.rotateLeft(values, 0); + + assertArrayEquals(new int[] {7, 8, 9}, values); } } - From 1f4bc30cc1465266ced230ee3d8eccb9bda4d82d Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:21:16 +0530 Subject: [PATCH 25/39] Fix formatting in ArrayRotation tests --- .../others/ArrayLeftRotationTest.java | 66 ++++++++----------- .../others/ArrayRotationTest.java | 20 +++--- 2 files changed, 39 insertions(+), 47 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java index f43448483021..b31b7d825ed5 100644 --- a/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayLeftRotationTest.java @@ -4,59 +4,51 @@ import org.junit.jupiter.api.Test; -public class ArrayRotationTest { +class ArrayLeftRotationTest { @Test - void shouldRotateArrayRightByTwoPositions() { - int[] values = {1, 2, 3, 4, 5}; - - ArrayRotation.rotateRight(values, 2); - - assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); + void testForOneElement() { + int[] arr = {3}; + int[] result = ArrayLeftRotation.rotateLeft(arr, 3); + assertArrayEquals(arr, result); } @Test - void shouldRotateArrayLeftByTwoPositions() { - int[] values = {1, 2, 3, 4, 5}; - - ArrayRotation.rotateLeft(values, 2); - - assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); + void testForZeroStep() { + int[] arr = {3, 1, 5, 8, 6}; + int[] result = ArrayLeftRotation.rotateLeft(arr, 0); + assertArrayEquals(arr, result); } @Test - void shouldHandleRotationGreaterThanArrayLength() { - int[] values = {10, 20, 30, 40}; - - ArrayRotation.rotateRight(values, 6); - - assertArrayEquals(new int[] {30, 40, 10, 20}, values); + void testForEqualSizeStep() { + int[] arr = {3, 1, 5, 8, 6}; + int[] result = ArrayLeftRotation.rotateLeft(arr, 5); + assertArrayEquals(arr, result); } @Test - void shouldKeepSingleElementArrayUnchanged() { - int[] values = {99}; - - ArrayRotation.rotateLeft(values, 5); - - assertArrayEquals(new int[] {99}, values); + void testForLowerSizeStep() { + int[] arr = {3, 1, 5, 8, 6}; + int n = 2; + int[] expected = {5, 8, 6, 3, 1}; + int[] result = ArrayLeftRotation.rotateLeft(arr, n); + assertArrayEquals(expected, result); } @Test - void shouldHandleEmptyArrayWithoutErrors() { - int[] values = {}; - - ArrayRotation.rotateRight(values, 3); - - assertArrayEquals(new int[] {}, values); + void testForHigherSizeStep() { + int[] arr = {3, 1, 5, 8, 6}; + int n = 7; + int[] expected = {5, 8, 6, 3, 1}; + int[] result = ArrayLeftRotation.rotateLeft(arr, n); + assertArrayEquals(expected, result); } @Test - void shouldReturnOriginalArrayWhenRotationIsZero() { - int[] values = {7, 8, 9}; - - ArrayRotation.rotateLeft(values, 0); - - assertArrayEquals(new int[] {7, 8, 9}, values); + void testForEmptyArray() { + int[] arr = {}; + int[] result = ArrayLeftRotation.rotateLeft(arr, 3); + assertArrayEquals(arr, result); } } diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index 283613d92d23..f43448483021 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -8,38 +8,38 @@ public class ArrayRotationTest { @Test void shouldRotateArrayRightByTwoPositions() { - int[] values = { 1, 2, 3, 4, 5 }; + int[] values = {1, 2, 3, 4, 5}; ArrayRotation.rotateRight(values, 2); - assertArrayEquals(new int[] { 4, 5, 1, 2, 3 }, values); + assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); } @Test void shouldRotateArrayLeftByTwoPositions() { - int[] values = { 1, 2, 3, 4, 5 }; + int[] values = {1, 2, 3, 4, 5}; ArrayRotation.rotateLeft(values, 2); - assertArrayEquals(new int[] { 3, 4, 5, 1, 2 }, values); + assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); } @Test void shouldHandleRotationGreaterThanArrayLength() { - int[] values = { 10, 20, 30, 40 }; + int[] values = {10, 20, 30, 40}; ArrayRotation.rotateRight(values, 6); - assertArrayEquals(new int[] { 30, 40, 10, 20 }, values); + assertArrayEquals(new int[] {30, 40, 10, 20}, values); } @Test void shouldKeepSingleElementArrayUnchanged() { - int[] values = { 99 }; + int[] values = {99}; ArrayRotation.rotateLeft(values, 5); - assertArrayEquals(new int[] { 99 }, values); + assertArrayEquals(new int[] {99}, values); } @Test @@ -53,10 +53,10 @@ void shouldHandleEmptyArrayWithoutErrors() { @Test void shouldReturnOriginalArrayWhenRotationIsZero() { - int[] values = { 7, 8, 9 }; + int[] values = {7, 8, 9}; ArrayRotation.rotateLeft(values, 0); - assertArrayEquals(new int[] { 7, 8, 9 }, values); + assertArrayEquals(new int[] {7, 8, 9}, values); } } From ba42f82eeef488da6bb41191b533dc0501c6fe7e Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:23:33 +0530 Subject: [PATCH 26/39] Fix formatting in ArrayRotation tests --- src/test/java/com/thealgorithms/others/ArrayRotationTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index f43448483021..a8570f2cf68f 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -59,4 +59,5 @@ void shouldReturnOriginalArrayWhenRotationIsZero() { assertArrayEquals(new int[] {7, 8, 9}, values); } -} + } + From 2eb72d9d8c4143e1f54b2174e6a6ef8fb9ec4d43 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:33:52 +0530 Subject: [PATCH 27/39] Fix formatting in ArrayRotation tests --- .../others/ArrayRotationTest.java | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index a8570f2cf68f..0d00b74a0c93 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -8,38 +8,40 @@ public class ArrayRotationTest { @Test void shouldRotateArrayRightByTwoPositions() { - int[] values = {1, 2, 3, 4, 5}; + int[] values = { 1, 2, 3, 4, 5 }; ArrayRotation.rotateRight(values, 2); - assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); + assertArrayEquals(new int[] { 4, 5, 1, 2, 3 }, values); + } @Test void shouldRotateArrayLeftByTwoPositions() { - int[] values = {1, 2, 3, 4, 5}; + int[] values = { 1, 2, 3, 4, 5 }; ArrayRotation.rotateLeft(values, 2); - assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); + assertArrayEquals(new int[] { 3, 4, 5, 1, 2 }, values); + } @Test void shouldHandleRotationGreaterThanArrayLength() { - int[] values = {10, 20, 30, 40}; + int[] values = { 10, 20, 30, 40 }; - ArrayRotation.rotateRight(values, 6); + ArrayRotation.rotateRight(values, 2); - assertArrayEquals(new int[] {30, 40, 10, 20}, values); + assertArrayEquals(new int[] { 30, 40, 10, 20 }, values); } @Test void shouldKeepSingleElementArrayUnchanged() { - int[] values = {99}; + int[] values = { 99 }; ArrayRotation.rotateLeft(values, 5); - assertArrayEquals(new int[] {99}, values); + assertArrayEquals(new int[] { 99 }, values); } @Test @@ -53,11 +55,10 @@ void shouldHandleEmptyArrayWithoutErrors() { @Test void shouldReturnOriginalArrayWhenRotationIsZero() { - int[] values = {7, 8, 9}; + int[] values = { 7, 8, 9 }; ArrayRotation.rotateLeft(values, 0); - assertArrayEquals(new int[] {7, 8, 9}, values); + assertArrayEquals(new int[] { 7, 8, 9 }, values); } - } - +} From 5e672206b84ff854d6204365440ea18fa7131d3b Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:37:39 +0530 Subject: [PATCH 28/39] Fix formatting in ArrayRotation tests --- .../thealgorithms/others/ArrayRotationTest.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index 0d00b74a0c93..39d59c1aebfd 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -12,36 +12,36 @@ void shouldRotateArrayRightByTwoPositions() { ArrayRotation.rotateRight(values, 2); - assertArrayEquals(new int[] { 4, 5, 1, 2, 3 }, values); + assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); } @Test void shouldRotateArrayLeftByTwoPositions() { - int[] values = { 1, 2, 3, 4, 5 }; + int[] values = {1, 2, 3, 4, 5}; ArrayRotation.rotateLeft(values, 2); - assertArrayEquals(new int[] { 3, 4, 5, 1, 2 }, values); + assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); } @Test void shouldHandleRotationGreaterThanArrayLength() { - int[] values = { 10, 20, 30, 40 }; + int[] values = {10, 20, 30, 40}; ArrayRotation.rotateRight(values, 2); - assertArrayEquals(new int[] { 30, 40, 10, 20 }, values); + assertArrayEquals(new int[] {30, 40, 10, 20}, values); } @Test void shouldKeepSingleElementArrayUnchanged() { - int[] values = { 99 }; + int[] values = {99}; ArrayRotation.rotateLeft(values, 5); - assertArrayEquals(new int[] { 99 }, values); + assertArrayEquals(new int[] {99}, values); } @Test @@ -59,6 +59,7 @@ void shouldReturnOriginalArrayWhenRotationIsZero() { ArrayRotation.rotateLeft(values, 0); - assertArrayEquals(new int[] { 7, 8, 9 }, values); + assertArrayEquals(new int[] {7, 8, 9}, values); } } + From 47aa448a234e769f5504fee5308cb954efed77cf Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:39:23 +0530 Subject: [PATCH 29/39] Fix formatting in ArrayRotation tests --- .../java/com/thealgorithms/others/ArrayRotationTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index 39d59c1aebfd..ac23f433bab2 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -8,12 +8,11 @@ public class ArrayRotationTest { @Test void shouldRotateArrayRightByTwoPositions() { - int[] values = { 1, 2, 3, 4, 5 }; + int[] values = {1, 2, 3, 4, 5}; ArrayRotation.rotateRight(values, 2); assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); - } @Test @@ -23,7 +22,6 @@ void shouldRotateArrayLeftByTwoPositions() { ArrayRotation.rotateLeft(values, 2); assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); - } @Test @@ -61,5 +59,5 @@ void shouldReturnOriginalArrayWhenRotationIsZero() { assertArrayEquals(new int[] {7, 8, 9}, values); } -} + } From c278b2d781dece10474e190d5d0bb8168bcdd23b Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:41:31 +0530 Subject: [PATCH 30/39] Fix formatting in ArrayRotation tests --- .../java/com/thealgorithms/others/ArrayRotationTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index ac23f433bab2..4ea0eda7d2b3 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -13,7 +13,7 @@ void shouldRotateArrayRightByTwoPositions() { ArrayRotation.rotateRight(values, 2); assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); - } + } @Test void shouldRotateArrayLeftByTwoPositions() { @@ -49,11 +49,11 @@ void shouldHandleEmptyArrayWithoutErrors() { ArrayRotation.rotateRight(values, 3); assertArrayEquals(new int[] {}, values); - } + } @Test void shouldReturnOriginalArrayWhenRotationIsZero() { - int[] values = { 7, 8, 9 }; + int[] values = {7, 8, 9}; ArrayRotation.rotateLeft(values, 0); From e6393ac2c6cb991cec9adbbdcf98054329805613 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:44:07 +0530 Subject: [PATCH 31/39] Fix formatting in ArrayRotation tests --- .../java/com/thealgorithms/others/ArrayRotationTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index 4ea0eda7d2b3..f43448483021 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -13,7 +13,7 @@ void shouldRotateArrayRightByTwoPositions() { ArrayRotation.rotateRight(values, 2); assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); - } + } @Test void shouldRotateArrayLeftByTwoPositions() { @@ -28,7 +28,7 @@ void shouldRotateArrayLeftByTwoPositions() { void shouldHandleRotationGreaterThanArrayLength() { int[] values = {10, 20, 30, 40}; - ArrayRotation.rotateRight(values, 2); + ArrayRotation.rotateRight(values, 6); assertArrayEquals(new int[] {30, 40, 10, 20}, values); } @@ -49,7 +49,7 @@ void shouldHandleEmptyArrayWithoutErrors() { ArrayRotation.rotateRight(values, 3); assertArrayEquals(new int[] {}, values); - } + } @Test void shouldReturnOriginalArrayWhenRotationIsZero() { @@ -59,5 +59,4 @@ void shouldReturnOriginalArrayWhenRotationIsZero() { assertArrayEquals(new int[] {7, 8, 9}, values); } - } - +} From cf56a809c86ce5546bd867985dfc2f4005d7e49e Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Sun, 17 May 2026 20:50:32 +0530 Subject: [PATCH 32/39] Fix formatting in ArrayRotation tests --- src/test/java/com/thealgorithms/others/ArrayRotationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index f43448483021..5e77bde45c4a 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -59,4 +59,4 @@ void shouldReturnOriginalArrayWhenRotationIsZero() { assertArrayEquals(new int[] {7, 8, 9}, values); } -} + } From 0afa70b43784444cb56d3080da78bae73c4e1706 Mon Sep 17 00:00:00 2001 From: Bhanu basyan Date: Sat, 29 Aug 2026 20:39:52 +0530 Subject: [PATCH 33/39] Update the Implimentation of ArrayRotatioTest and /Implementing_auto_completing_features_using_trie --- .../others/ArrayRotationTest.java | 62 ----- ...g_auto_completing_features_using_trie.java | 170 ------------- .../java/com/thealgorithms/others/Trie.java | 233 ++++++++++++++++++ 3 files changed, 233 insertions(+), 232 deletions(-) delete mode 100644 src/main/java/com/thealgorithms/others/ArrayRotationTest.java delete mode 100644 src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java create mode 100644 src/main/java/com/thealgorithms/others/Trie.java diff --git a/src/main/java/com/thealgorithms/others/ArrayRotationTest.java b/src/main/java/com/thealgorithms/others/ArrayRotationTest.java deleted file mode 100644 index 356a683be468..000000000000 --- a/src/main/java/com/thealgorithms/others/ArrayRotationTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.thealgorithms.others; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; - -import org.junit.jupiter.api.Test; - -public class ArrayRotationTest { - - @Test - void shouldRotateArrayRightByTwoPositions() { - int[] values = {1, 2, 3, 4, 5}; - - ArrayRotation.rotateRight(values, 2); - - assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values); - } - - @Test - void shouldRotateArrayLeftByTwoPositions() { - int[] values = {1, 2, 3, 4, 5}; - - ArrayRotation.rotateLeft(values, 2); - - assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values); - } - - @Test - void shouldHandleRotationGreaterThanArrayLength() { - int[] values = {10, 20, 30, 40}; - - ArrayRotation.rotateRight(values, 6); - - assertArrayEquals(new int[] {30, 40, 10, 20}, values); - } - - @Test - void shouldKeepSingleElementArrayUnchanged() { - int[] values = {99}; - - ArrayRotation.rotateLeft(values, 5); - - assertArrayEquals(new int[] {99}, values); - } - - @Test - void shouldHandleEmptyArrayWithoutErrors() { - int[] values = {}; - - ArrayRotation.rotateRight(values, 3); - - assertArrayEquals(new int[] {}, values); - } - - @Test - void shouldReturnOriginalArrayWhenRotationIsZero() { - int[] values = {7, 8, 9}; - - ArrayRotation.rotateLeft(values, 0); - - assertArrayEquals(new int[] {7, 8, 9}, values); - } -} \ No newline at end of file diff --git a/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java b/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java deleted file mode 100644 index 7a1a7aadd805..000000000000 --- a/src/main/java/com/thealgorithms/others/Implementing_auto_completing_features_using_trie.java +++ /dev/null @@ -1,170 +0,0 @@ -package com.thealgorithms.others; - -// Java Program to implement Auto-Complete -// Feature using Trie -class Trieac { - - // Alphabet size (# of symbols) - public static final int ALPHABET_SIZE = 26; - - // Trie node - static class TrieNode { - - TrieNode[] children = new TrieNode[ALPHABET_SIZE]; - - // isWordEnd is true if the node represents - // end of a word - boolean isWordEnd; - } - - // Returns new trie node (initialized to NULLs) - static TrieNode getNode() { - TrieNode pNode = new TrieNode(); - pNode.isWordEnd = false; - - for (int i = 0; i < ALPHABET_SIZE; i++) { - pNode.children[i] = null; - } - - return pNode; - } - - // If not present, inserts key into trie. If the - // key is prefix of trie node, just marks leaf node - static void insert(TrieNode root, final String key) { - TrieNode pCrawl = root; - - for (int level = 0; level < key.length(); level++) { - int index = (key.charAt(level) - 'a'); - if (pCrawl.children[index] == null) { - pCrawl.children[index] = getNode(); - } - pCrawl = pCrawl.children[index]; - } - - // mark last node as leaf - pCrawl.isWordEnd = true; - } - - // Returns true if key presents in trie, else false - boolean search(TrieNode root, final String key) { - int length = key.length(); - TrieNode pCrawl = root; - - for (int level = 0; level < length; level++) { - int index = (key.charAt(level) - 'a'); - - if (pCrawl.children[index] == null) { - pCrawl = pCrawl.children[index]; - } - } - - return (pCrawl != null && pCrawl.isWordEnd); - } - - // Returns 0 if current node has a child - // If all children are NULL, return 1. - static boolean isLastNode(TrieNode root) { - for (int i = 0; i < ALPHABET_SIZE; i++) { - if (root.children[i] != null) { - return false; - } - } - return true; - } - - // Recursive function to print auto-suggestions - // for given node. - static void suggestionsRec(TrieNode root, String currPrefix) { - // found a string in Trie with the given prefix - if (root.isWordEnd) { - System.out.println(currPrefix); - } - - // All children struct node pointers are NULL - if (isLastNode(root)) { - return; - } - - for (int i = 0; i < ALPHABET_SIZE; i++) { - if (root.children[i] != null) { - // append current character to currPrefix string - currPrefix += (char) (97 + i); - - // recur over the rest - suggestionsRec(root.children[i], currPrefix); - } - } - } - - // Function to print suggestions for - // given query prefix. - static int printAutoSuggestions(TrieNode root, final String query) { - TrieNode pCrawl = root; - - // Check if prefix is present and find the - // the node (of last level) with last character - // of given string. - int level; - int n = query.length(); - - for (level = 0; level < n; level++) { - int index = (query.charAt(level) - 'a'); - - // no string in the Trie has this prefix - if (pCrawl.children[index] == null) { - return 0; - } - - pCrawl = pCrawl.children[index]; - } - - // If prefix is present as a word. - boolean isWord = (pCrawl.isWordEnd); - - // If prefix is last node of tree (has no - // children) - boolean isLast = isLastNode(pCrawl); - - // If prefix is present as a word, but - // there is no subtree below the last - // matching node. - if (isWord && isLast) { - System.out.println(query); - return -1; - } - - // If there are nodes below the last - // matching character. - if (!isLast) { - String prefix = query; - suggestionsRec(pCrawl, prefix); - return 1; - } - - return 0; - } - - // Driver code - public static void main(String[] args) { - TrieNode root = getNode(); - insert(root, "hello"); - insert(root, "dog"); - insert(root, "hell"); - insert(root, "cat"); - insert(root, "a"); - insert(root, "hel"); - insert(root, "help"); - insert(root, "helps"); - insert(root, "helping"); - int comp = printAutoSuggestions(root, "hel"); - - if (comp == -1) { - System.out.println("No other strings found " - + "with this prefix\n"); - } else if (comp == 0) { - System.out.println("No string found with" - + " this prefix\n"); - } - } -} diff --git a/src/main/java/com/thealgorithms/others/Trie.java b/src/main/java/com/thealgorithms/others/Trie.java new file mode 100644 index 000000000000..f9fe732743d1 --- /dev/null +++ b/src/main/java/com/thealgorithms/others/Trie.java @@ -0,0 +1,233 @@ +package com.thealgorithms.others; + +import java.util.ArrayList; +import java.util.List; + +public class Trie { + + private static final int ALPHABET_SIZE = 26; + + // ---------------- NODE ---------------- + + static class TrieNode { + + TrieNode[] children = new TrieNode[ALPHABET_SIZE]; + + boolean isWordEnd; + } + + // ---------------- ROOT ---------------- + + private final TrieNode root = new TrieNode(); + + // ---------------- INSERT ---------------- + + public void insert(String word) { + + word = normalize(word); + + TrieNode current = root; + + for (char c : word.toCharArray()) { + + int index = c - 'a'; + + if (current.children[index] == null) { + current.children[index] = new TrieNode(); + } + + current = current.children[index]; + } + + current.isWordEnd = true; + } + + // ---------------- SEARCH ---------------- + + public boolean search(String word) { + + word = normalize(word); + + TrieNode current = root; + + for (char c : word.toCharArray()) { + + int index = c - 'a'; + + if (current.children[index] == null) { + return false; + } + + current = current.children[index]; + } + + return current.isWordEnd; + } + + // ---------------- PREFIX ---------------- + + public boolean startsWith(String prefix) { + + prefix = normalize(prefix); + + return findNode(prefix) != null; + } + + // ---------------- AUTOCOMPLETE ---------------- + + public List suggestions(String prefix) { + + prefix = normalize(prefix); + + List result = new ArrayList<>(); + + TrieNode node = findNode(prefix); + + // Prefix doesn't exist + if (node == null) { + return result; + } + + StringBuilder currentWord = new StringBuilder(prefix); + + collectSuggestions( + node, + currentWord, + result); + + return result; + } + + // Recursive autocomplete + private void collectSuggestions( + TrieNode node, + StringBuilder currentWord, + List result) { + + // Current word is complete + if (node.isWordEnd) { + result.add(currentWord.toString()); + } + + // Check all 26 possible characters + for (int i = 0; i < ALPHABET_SIZE; i++) { + + if (node.children[i] != null) { + + // Add character + currentWord.append( + (char) ('a' + i)); + + // Go to next node + collectSuggestions( + node.children[i], + currentWord, + result); + + // Backtrack + currentWord.deleteCharAt( + currentWord.length() - 1); + } + } + } + + // ---------------- FIND NODE ---------------- + + private TrieNode findNode(String text) { + + TrieNode current = root; + + for (char c : text.toCharArray()) { + + int index = c - 'a'; + + if (current.children[index] == null) { + return null; + } + + current = current.children[index]; + } + + return current; + } + + // ---------------- NORMALIZE ---------------- + + private String normalize(String text) { + + if (text == null || text.isBlank()) { + throw new IllegalArgumentException( + "Word cannot be empty"); + } + + text = text.toLowerCase().trim(); + + for (char c : text.toCharArray()) { + + if (c < 'a' || c > 'z') { + throw new IllegalArgumentException( + "Only a-z characters are allowed"); + } + } + + return text; + } + + // ---------------- MAIN ---------------- + + public static void main(String[] args) { + + Trie trie = new Trie(); + + /* + * Add as many words as you want. + * No fixed 10-20 word limitation. + */ + + String[] words = { + "hello", + "hell", + "hel", + "help", + "helps", + "helping", + "helicopter", + "hero", + "her", + "cat", + "car", + "care", + "career", + "dog" + }; + + // Dynamically insert all words + for (String word : words) { + trie.insert(word); + } + + // Search + System.out.println( + "Search 'hello': " + + trie.search("hello")); + + System.out.println( + "Search 'xyz': " + + trie.search("xyz")); + + // Prefix + System.out.println( + "Starts with 'hel': " + + trie.startsWith("hel")); + + // Autocomplete + System.out.println( + "\nSuggestions for 'hel':"); + + List suggestions = trie.suggestions("hel"); + + for (String word : suggestions) { + System.out.println(word); + } + } +} \ No newline at end of file From 878d450e8a2db6988f5a393635a866d9b949144b Mon Sep 17 00:00:00 2001 From: Bhanu basyan Date: Sat, 29 Aug 2026 21:08:14 +0530 Subject: [PATCH 34/39] fix bugs --- .../java/com/thealgorithms/others/Trie.java | 57 +++++-------------- 1 file changed, 13 insertions(+), 44 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/Trie.java b/src/main/java/com/thealgorithms/others/Trie.java index f9fe732743d1..a6a05306d59f 100644 --- a/src/main/java/com/thealgorithms/others/Trie.java +++ b/src/main/java/com/thealgorithms/others/Trie.java @@ -90,19 +90,13 @@ public List suggestions(String prefix) { StringBuilder currentWord = new StringBuilder(prefix); - collectSuggestions( - node, - currentWord, - result); + collectSuggestions(node, currentWord, result); return result; } // Recursive autocomplete - private void collectSuggestions( - TrieNode node, - StringBuilder currentWord, - List result) { + private void collectSuggestions(TrieNode node, StringBuilder currentWord, List result) { // Current word is complete if (node.isWordEnd) { @@ -115,18 +109,13 @@ private void collectSuggestions( if (node.children[i] != null) { // Add character - currentWord.append( - (char) ('a' + i)); + currentWord.append((char) ('a' + i)); // Go to next node - collectSuggestions( - node.children[i], - currentWord, - result); + collectSuggestions(node.children[i], currentWord, result); // Backtrack - currentWord.deleteCharAt( - currentWord.length() - 1); + currentWord.deleteCharAt(currentWord.length() - 1); } } } @@ -156,8 +145,7 @@ private TrieNode findNode(String text) { private String normalize(String text) { if (text == null || text.isBlank()) { - throw new IllegalArgumentException( - "Word cannot be empty"); + throw new IllegalArgumentException("Word cannot be empty"); } text = text.toLowerCase().trim(); @@ -165,8 +153,7 @@ private String normalize(String text) { for (char c : text.toCharArray()) { if (c < 'a' || c > 'z') { - throw new IllegalArgumentException( - "Only a-z characters are allowed"); + throw new IllegalArgumentException("Only a-z characters are allowed"); } } @@ -184,22 +171,8 @@ public static void main(String[] args) { * No fixed 10-20 word limitation. */ - String[] words = { - "hello", - "hell", - "hel", - "help", - "helps", - "helping", - "helicopter", - "hero", - "her", - "cat", - "car", - "care", - "career", - "dog" - }; + String[] words = { "hello", "hell", "hel", "help", "helps", "helping", "helicopter", "hero", "her", "cat", + "car", "care", "career", "dog" }; // Dynamically insert all words for (String word : words) { @@ -208,21 +181,17 @@ public static void main(String[] args) { // Search System.out.println( - "Search 'hello': " - + trie.search("hello")); + "Search 'hello': " + trie.search("hello")); System.out.println( - "Search 'xyz': " - + trie.search("xyz")); + "Search 'xyz': " + trie.search("xyz")); // Prefix System.out.println( - "Starts with 'hel': " - + trie.startsWith("hel")); + "Starts with 'hel': " + trie.startsWith("hel")); // Autocomplete - System.out.println( - "\nSuggestions for 'hel':"); + System.out.println("\nSuggestions for 'hel':"); List suggestions = trie.suggestions("hel"); From 54a4d01ae7d1cf146279b20774930c7d6dcc79c1 Mon Sep 17 00:00:00 2001 From: Bhanu basyan Date: Sat, 29 Aug 2026 21:17:50 +0530 Subject: [PATCH 35/39] fix bugs --- src/main/java/com/thealgorithms/others/Trie.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/Trie.java b/src/main/java/com/thealgorithms/others/Trie.java index a6a05306d59f..40e905df4f6e 100644 --- a/src/main/java/com/thealgorithms/others/Trie.java +++ b/src/main/java/com/thealgorithms/others/Trie.java @@ -171,8 +171,7 @@ public static void main(String[] args) { * No fixed 10-20 word limitation. */ - String[] words = { "hello", "hell", "hel", "help", "helps", "helping", "helicopter", "hero", "her", "cat", - "car", "care", "career", "dog" }; + String[] words = { "hello", "hell", "hel", "help", "helps", "helping", "helicopter", "car", "career", "dog" }; // Dynamically insert all words for (String word : words) { @@ -180,15 +179,12 @@ public static void main(String[] args) { } // Search - System.out.println( - "Search 'hello': " + trie.search("hello")); + System.out.println("Search 'hello': " + trie.search("hello")); - System.out.println( - "Search 'xyz': " + trie.search("xyz")); + System.out.println("Search 'xyz': " + trie.search("xyz")); // Prefix - System.out.println( - "Starts with 'hel': " + trie.startsWith("hel")); + System.out.println("Starts with 'hel': " + trie.startsWith("hel")); // Autocomplete System.out.println("\nSuggestions for 'hel':"); From dc13192c147aaae0f90488122d4e1105c55d5f65 Mon Sep 17 00:00:00 2001 From: Bhanu basyan Date: Sat, 29 Aug 2026 21:23:23 +0530 Subject: [PATCH 36/39] fix bugs --- src/main/java/com/thealgorithms/others/Trie.java | 6 ++++-- .../java/com/thealgorithms/others/ArrayRotationTest.java | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/Trie.java b/src/main/java/com/thealgorithms/others/Trie.java index 40e905df4f6e..1a294dae57ce 100644 --- a/src/main/java/com/thealgorithms/others/Trie.java +++ b/src/main/java/com/thealgorithms/others/Trie.java @@ -171,7 +171,9 @@ public static void main(String[] args) { * No fixed 10-20 word limitation. */ - String[] words = { "hello", "hell", "hel", "help", "helps", "helping", "helicopter", "car", "career", "dog" }; + String[] words = { + "hello", "hell", "hel", "help", "helps", "helping", "helicopter", "car", "career", "dog" + }; // Dynamically insert all words for (String word : words) { @@ -195,4 +197,4 @@ public static void main(String[] args) { System.out.println(word); } } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java index 5e77bde45c4a..f43448483021 100644 --- a/src/test/java/com/thealgorithms/others/ArrayRotationTest.java +++ b/src/test/java/com/thealgorithms/others/ArrayRotationTest.java @@ -59,4 +59,4 @@ void shouldReturnOriginalArrayWhenRotationIsZero() { assertArrayEquals(new int[] {7, 8, 9}, values); } - } +} From 1999ab45b0988b264b2bd99dbad70e6be875a9ea Mon Sep 17 00:00:00 2001 From: Bhanu basyan Date: Sat, 29 Aug 2026 21:27:37 +0530 Subject: [PATCH 37/39] Fix formatting issues --- src/main/java/com/thealgorithms/others/Trie.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/Trie.java b/src/main/java/com/thealgorithms/others/Trie.java index 1a294dae57ce..cb8d9347a272 100644 --- a/src/main/java/com/thealgorithms/others/Trie.java +++ b/src/main/java/com/thealgorithms/others/Trie.java @@ -171,9 +171,7 @@ public static void main(String[] args) { * No fixed 10-20 word limitation. */ - String[] words = { - "hello", "hell", "hel", "help", "helps", "helping", "helicopter", "car", "career", "dog" - }; + String[] words = {"hello", "hell", "hel", "help", "helps", "helping", "helicopter", "car", "career", "dog"}; // Dynamically insert all words for (String word : words) { From 2d96fa37d89cfc816d9c9c935cf18668efb7fca9 Mon Sep 17 00:00:00 2001 From: Bhanu basyan Date: Sat, 29 Aug 2026 21:33:54 +0530 Subject: [PATCH 38/39] Remove redundant Trie main method --- .../java/com/thealgorithms/others/Trie.java | 35 ------------------- 1 file changed, 35 deletions(-) diff --git a/src/main/java/com/thealgorithms/others/Trie.java b/src/main/java/com/thealgorithms/others/Trie.java index cb8d9347a272..0e97835520fd 100644 --- a/src/main/java/com/thealgorithms/others/Trie.java +++ b/src/main/java/com/thealgorithms/others/Trie.java @@ -160,39 +160,4 @@ private String normalize(String text) { return text; } - // ---------------- MAIN ---------------- - - public static void main(String[] args) { - - Trie trie = new Trie(); - - /* - * Add as many words as you want. - * No fixed 10-20 word limitation. - */ - - String[] words = {"hello", "hell", "hel", "help", "helps", "helping", "helicopter", "car", "career", "dog"}; - - // Dynamically insert all words - for (String word : words) { - trie.insert(word); - } - - // Search - System.out.println("Search 'hello': " + trie.search("hello")); - - System.out.println("Search 'xyz': " + trie.search("xyz")); - - // Prefix - System.out.println("Starts with 'hel': " + trie.startsWith("hel")); - - // Autocomplete - System.out.println("\nSuggestions for 'hel':"); - - List suggestions = trie.suggestions("hel"); - - for (String word : suggestions) { - System.out.println(word); - } - } } From 0bcee66e77d7e6483150d627dec11fa11fe0c9cb Mon Sep 17 00:00:00 2001 From: Bhanu basyan Date: Sat, 29 Aug 2026 21:35:29 +0530 Subject: [PATCH 39/39] Remove redundant Trie main method --- src/main/java/com/thealgorithms/others/Trie.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/others/Trie.java b/src/main/java/com/thealgorithms/others/Trie.java index 0e97835520fd..339451b00a41 100644 --- a/src/main/java/com/thealgorithms/others/Trie.java +++ b/src/main/java/com/thealgorithms/others/Trie.java @@ -159,5 +159,4 @@ private String normalize(String text) { return text; } - }