0% found this document useful (0 votes)
6 views3 pages

Java Rotations Code Explanation

The document provides various methods for rotating arrays and strings in Java, including left and right rotations by a specified number of elements. It covers inefficient and efficient approaches, such as using reversal and extra arrays, as well as rotating 2D matrices by 90 degrees. Each method is accompanied by code snippets and explanations of the underlying logic.

Uploaded by

diyakrishna399
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Java Rotations Code Explanation

The document provides various methods for rotating arrays and strings in Java, including left and right rotations by a specified number of elements. It covers inefficient and efficient approaches, such as using reversal and extra arrays, as well as rotating 2D matrices by 90 degrees. Each method is accompanied by code snippets and explanations of the underlying logic.

Uploaded by

diyakrishna399
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Array & String Rotations - Code & Explanation

1. Left Rotation by 1 Element

int[] arr = {1, 2, 3, 4, 5};


int first = arr[0];
for (int i = 0; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = first;
Explanation:

- Stores first element.

- Shifts all others to the left.

- Places first element at the end.

2. Left Rotation by k (Inefficient)

int[] arr = {1, 2, 3, 4, 5};


int k = 2;
for (int i = 0; i < k; i++) {
int first = arr[0];
for (int j = 0; j < arr.length - 1; j++) {
arr[j] = arr[j + 1];
}
arr[arr.length - 1] = first;
}
Explanation:

- Performs left rotation k times.

- Each time stores first element, shifts left, puts first at the end.

3. Left Rotation by k (Efficient using Reversal)

static void rotateLeft(int[] arr, int k) {


k = k % arr.length;
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
reverse(arr, 0, arr.length - 1);
}

static void reverse(int[] arr, int start, int end) {


while (start < end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
Explanation:

- Reverses parts of array and then the whole array.

- Efficient O(n) rotation.

4. Right Rotation by k (Using Reversal)

static void rotateRight(int[] arr, int k) {


k = k % arr.length;
reverse(arr, 0, arr.length - 1);
reverse(arr, 0, k - 1);
reverse(arr, k, arr.length - 1);
}
Explanation:

- Reverse the full array.

- Then reverse first k and then remaining.

- Results in right circular rotation.

5. Right Circular Rotation (Using Extra Array)

int[] arr = {1, 2, 3, 4, 5};


int k = 2;
int n = arr.length;
int[] rotated = new int[n];

for (int i = 0; i < n; i++) {


rotated[(i + k) % n] = arr[i];
}
Explanation:

- Uses a new array to place each element in new rotated position.

- (i + k) % n gives new position.

6. Left Circular Rotation (Using Extra Array)

int[] arr = {1, 2, 3, 4, 5};


int k = 2;
int n = arr.length;
int[] rotated = new int[n];
for (int i = 0; i < n; i++) {
rotated[i] = arr[(i + k) % n];
}
Explanation:

- Places each element by picking from (i+k)%n index.

- Efficient using one loop and extra array.

7. Rotate String Left and Right

// Left
String str = "abcdef";
int k = 2;
String leftRotated = str.substring(k) + str.substring(0, k);

// Right
int n = str.length();
String rightRotated = str.substring(n - k) + str.substring(0, n - k);
Explanation:

- Left: cut and join substring from k to end + start to k.

- Right: cut and join from (n-k) to end + start to (n-k).

8. Rotate 2D Matrix by 90 Degrees

// Transpose
for (int i = 0; i < n; i++) {
for (int j = i; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
// Reverse each row
for (int i = 0; i < n; i++) {
for (int j = 0, k = n - 1; j < k; j++, k--) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][k];
matrix[i][k] = temp;
}
}
Explanation:

- First step: transpose the matrix.

- Second step: reverse each row.

- Results in 90-degree clockwise rotation.

You might also like