Skip to content

Commit 3e857c3

Browse files
committed
feat: Rotate array left by one position and print before and after
🧠 Logic: - Define an array of integers. - Print the original array using a helper method. - Rotate the array left by one: - Store the first element temporarily. - Shift all other elements one position to the left. - Place the first element at the end of the array. - Print the rotated array to observe the change. Signed-off-by: Somesh diwan <[email protected]>
1 parent 5ab6a38 commit 3e857c3

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
public class ArrayRotator {
2+
private static final String DELIMITER = ", ";
3+
4+
public static void main(String[] args) {
5+
int[] numbers = {3, 9, 7, 8, 12, 6, 15, 5, 4, 10};
6+
7+
printArray(numbers);
8+
rotateArrayLeft(numbers);
9+
printArray(numbers);
10+
}
11+
12+
public static void rotateArrayLeft(int[] array) {
13+
if (array == null || array.length <= 1) {
14+
return;
15+
}
16+
17+
int temp = array[0];
18+
for (int i = 1; i < array.length; i++) {
19+
array[i - 1] = array[i];
20+
}
21+
array[array.length - 1] = temp;
22+
}
23+
24+
private static void printArray(int[] array) {
25+
if (array == null || array.length == 0) {
26+
System.out.println("[]");
27+
return;
28+
}
29+
30+
for (int i = 0; i < array.length; i++) {
31+
System.out.print(array[i]);
32+
if (i < array.length - 1) {
33+
System.out.print(DELIMITER);
34+
}
35+
}
36+
System.out.println();
37+
}
38+
}

0 commit comments

Comments
 (0)