Skip to content

Commit 380a8c0

Browse files
committed
Add rotate array in java
1 parent 661bced commit 380a8c0

File tree

5 files changed

+126
-8
lines changed

5 files changed

+126
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ List of Programs related to data structures and algorithms
6363
10. Maximum Circular subarray: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/10.maxSumCircularSubArray/maxSumCircularSubArray.js)
6464
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/10.maxSumCircularSubArray/maxSumCircularSubArray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/10.maxSumCircularSubArray/maxSumCircularSubArray.md)
6565

66-
11. Rotate array: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/rotate.js)
66+
11. Rotate array: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.rotateArray/rotateArray.js)
67+
[Playground](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.rotateArray/rotateArray.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/11.rotateArray/rotateArray.md)
6768

6869
12. Search in rotated sorted array: [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/searchRotatedSortedArray.js)
6970

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package rotateArray;
2+
3+
import java.util.Arrays;
4+
5+
public class RotateArray {
6+
7+
private static int[] rotate(int[] nums, int n) {
8+
int length = nums.length;
9+
n %= length;
10+
11+
reversal(nums, 0, length-1);
12+
reversal(nums, 0, n-1);
13+
reversal(nums, n, length-1);
14+
15+
return nums;
16+
}
17+
18+
private static void reversal(int[] nums, int start, int end) {
19+
while(start < end) {
20+
int temp = nums[start];
21+
nums[start] = nums[end];
22+
nums[end] = temp;
23+
24+
start++;
25+
end--;
26+
}
27+
}
28+
29+
30+
public static void main(String[] args) {
31+
int[] rotate1 = {1, 2, 3, 4, 5, 6, 7};
32+
System.out.println("Before rotate: "+ Arrays.toString(rotate1));
33+
rotate(rotate1, 4);
34+
System.out.println("After rotate: "+ Arrays.toString(rotate1));
35+
36+
int[] rotate2 = {-10, 4, 5, -1};
37+
System.out.println("Before rotate: "+ Arrays.toString(rotate2));
38+
rotate(rotate2, 2);
39+
System.out.println("After rotate: "+ Arrays.toString(rotate2));
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
**Description**
2+
Given an integer array `nums`, rotate the array to the right by `n` steps, where `n` is non-negative number.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [1,2,3,4,5,6,7], n = 4
8+
Output: [5,6,7,1,2,3,4]
9+
10+
Example 2:
11+
12+
Input: nums = [-10, 4, 5, -1], n = 2
13+
Output: [5, -1, -10, 4]
14+
15+
**Algorithmic Steps:**
16+
This problem is solved with the help of two pointers, which is used to reverse subarrays. The algorithmic approach can be summarized as follows:
17+
18+
1. Initialize `length` variable to store length of an input array.
19+
20+
2. Update the number of times rotation(i.e, `n`) to `n % length` incase of number of rotations greater than length of an array.
21+
22+
3. Create a helper function to reverse elements of an array using swap operation with the help left and right pointers(i.e, `left` and `right`). The left pointer needs to be incremented and right pointer needs to decremented for each iteration.
23+
24+
4. Invoke reversal operation for entire array of elements(i.e, from the beggining to end of an array).
25+
26+
5. Invoke reversal operation for first `n` number of elements(i.e, from the beggining to n-1).
27+
28+
6. Invoke reversal operation for remaining number of elements(i.e, from `n-1` to end of an array).
29+
30+
7. Return in-place rotated array as output.
31+
32+
**Time and Space complexity:**
33+
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes O(n). So the overall time complexity is O(n).
35+
36+
Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be O(1).
37+

src/javascript/algorithms/array/rotate.js renamed to src/javascript/algorithms/array/11.rotateArray/rotateArray.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
// 1. Reversal approach
1+
// 1. Reversal approach using two pointers
22
function rotate(nums, n) {
33
const length = nums.length;
44
n %= length;
55

66
reversal(nums, 0, length-1);
77
reversal(nums, 0, n-1);
88
reversal(nums, n, length-1);
9+
10+
return nums;
911
}
1012

1113
function reversal(nums, start, end) {
@@ -25,11 +27,11 @@ function rotateBrutforce(nums, n) {
2527
}
2628

2729
let rotate1 = [1, 2, 3, 4, 5, 6, 7];
28-
console.log("Before rotate: ", rotate1.join(", "));
30+
console.log("Before rotate:", rotate1);
2931
rotate(rotate1, 4);
30-
console.log("After rotate: ", rotate1.join(", "));
32+
console.log("After rotate:", rotate1);
3133

32-
let rotate2 = [1, 2, 3, 4, 5, 6, 7];
33-
console.log("Before rotate: ", rotate2.join(", "));
34-
rotateBrutforce(rotate2, 4);
35-
console.log("After rotate: ", rotate2.join(", "));
34+
let rotate2 = [-10, 4, 5, -1];
35+
console.log("Before rotate:", rotate2);
36+
rotateBrutforce(rotate2, 2);
37+
console.log("After rotate:", rotate2.join);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
**Description**
2+
Given an integer array `nums`, rotate the array to the right by `n` steps, where `n` is non-negative number.
3+
4+
### Examples
5+
Example 1:
6+
7+
Input: nums = [1,2,3,4,5,6,7], n = 4
8+
Output: [5,6,7,1,2,3,4]
9+
10+
Example 2:
11+
12+
Input: nums = [-10, 4, 5, -1], n = 2
13+
Output: [5, -1, -10, 4]
14+
15+
**Algorithmic Steps:**
16+
This problem is solved with the help of two pointers, which is used to reverse subarrays. The algorithmic approach can be summarized as follows:
17+
18+
1. Initialize `length` variable to store length of an input array.
19+
20+
2. Update the number of times rotation(i.e, `n`) to `n % length` incase of number of rotations greater than length of an array.
21+
22+
3. Create a helper function to reverse elements of an array using swap operation with the help left and right pointers(i.e, `left` and `right`). The left pointer needs to be incremented and right pointer needs to decremented for each iteration.
23+
24+
4. Invoke reversal operation for entire array of elements(i.e, from the beggining to end of an array).
25+
26+
5. Invoke reversal operation for first `n` number of elements(i.e, from the beggining to n-1).
27+
28+
6. Invoke reversal operation for remaining number of elements(i.e, from `n-1` to end of an array).
29+
30+
7. Return in-place rotated array as output.
31+
32+
**Time and Space complexity:**
33+
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements. This is because we are performing three reversal operations on subarrays and each reversal operation takes O(n). So the overall time complexity is O(n).
35+
36+
Here, we don't use any additional datastructure due to in-place rotation. Hence, the space complexity will be O(1).
37+

0 commit comments

Comments
 (0)