From 54c214cdb04062565b55ae7d8aefd3828671bf60 Mon Sep 17 00:00:00 2001 From: bhanu1012 Date: Wed, 13 May 2026 15:42:59 +0530 Subject: [PATCH 01/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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/20] 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() { }