Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
54c214c
Add array rotation utility using reversal algorithm
Bhanubasyan May 13, 2026
cb3fea7
Fix checkstyle issues
Bhanubasyan May 13, 2026
8376bbe
Fix checkstyle issues
Bhanubasyan May 13, 2026
df2db03
Fix checkstyle issues
Bhanubasyan May 13, 2026
d729f7b
Fix checkstyle issues
Bhanubasyan May 13, 2026
e3d8cb1
Fix checkstyle issues
Bhanubasyan May 13, 2026
3fc7f45
Fix checkstyle issues
Bhanubasyan May 13, 2026
8831d24
Fix checkstyle issues
Bhanubasyan May 13, 2026
0940d5e
Fix checkstyle issues
Bhanubasyan May 13, 2026
4adc28c
Fix checkstyle issues
Bhanubasyan May 13, 2026
f28d8fc
Fix checkstyle issues
Bhanubasyan May 13, 2026
d1152ce
Fix checkstyle issues
Bhanubasyan May 13, 2026
52a57dd
Fix checkstyle issues
Bhanubasyan May 13, 2026
f81884d
Fix checkstyle issues
Bhanubasyan May 13, 2026
43731f0
Fix checkstyle issues
Bhanubasyan May 13, 2026
bb2df00
Fix checkstyle issues
Bhanubasyan May 13, 2026
a21bed0
Fix checkstyle issues
Bhanubasyan May 13, 2026
f9e24d0
Fix checkstyle issues
Bhanubasyan May 13, 2026
a18a7c8
Fix checkstyle issues
Bhanubasyan May 13, 2026
6a7d2c7
Fix checkstyle issues
Bhanubasyan May 13, 2026
d44a3f4
Merge branch 'master' into add-array-rotation-utility
Bhanubasyan May 13, 2026
c0746a5
Merge branch 'master' into add-array-rotation-utility
Bhanubasyan May 14, 2026
d0bca4f
Merge branch 'master' into add-array-rotation-utility
Bhanubasyan May 17, 2026
81d90cf
Add unit tests for ArrayRotation
Bhanubasyan May 17, 2026
af33548
Add unit tests for ArrayRotation
Bhanubasyan May 17, 2026
c11d0ca
Add unit tests for array rotation utility
Bhanubasyan May 17, 2026
0289397
Add unit tests for array rotation utility
Bhanubasyan May 17, 2026
1226614
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
1f4bc30
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
ba42f82
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
2eb72d9
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
5e67220
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
47aa448
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
c278b2d
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
e6393ac
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
cf56a80
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
0afa70b
Update the Implimentation of ArrayRotatioTest and /Implementing_auto…
Bhanubasyan Aug 29, 2026
1409a6e
Merge branch 'master' into add-array-rotation-utility
Bhanubasyan Aug 29, 2026
878d450
fix bugs
Bhanubasyan Aug 29, 2026
375cfde
fix bugs
Bhanubasyan Aug 29, 2026
54a4d01
fix bugs
Bhanubasyan Aug 29, 2026
dc13192
fix bugs
Bhanubasyan Aug 29, 2026
1999ab4
Fix formatting issues
Bhanubasyan Aug 29, 2026
2d96fa3
Remove redundant Trie main method
Bhanubasyan Aug 29, 2026
0bcee66
Remove redundant Trie main method
Bhanubasyan Aug 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions src/main/java/com/thealgorithms/others/ArrayRotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.thealgorithms.others;

/**
* Array Rotation Utility
*
* Supports:
* 1. Left Rotation
* 2. Right Rotation
*
* Approach:
* Reversal Algorithm
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
public final class 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) {
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--;
}
}
}
162 changes: 162 additions & 0 deletions src/main/java/com/thealgorithms/others/Trie.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
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<String> suggestions(String prefix) {

prefix = normalize(prefix);

List<String> 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<String> 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;
}
}
62 changes: 62 additions & 0 deletions src/test/java/com/thealgorithms/others/ArrayRotationTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading