-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftRotateArray.java
More file actions
50 lines (35 loc) · 1.23 KB
/
LeftRotateArray.java
File metadata and controls
50 lines (35 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.Scanner;
/* Given an integer array nums, rotate the array to the left by one.
Note: There is no need to return anything, just modify the given array.
Examples:
Input: nums = [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]
Explanation: Initially, nums = [1, 2, 3, 4, 5]
Rotating once to left -> nums = [2, 3, 4, 5, 1]
Input: nums = [-1, 0, 3, 6]
Output: [0, 3, 6, -1]
Explanation: Initially, nums = [-1, 0, 3, 6]
Rotating once to left -> nums = [0, 3, 6, -1]*/
public class LeftRotateArray{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.println("Enter elements:");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
if (n > 1) {
int first = nums[0];
for (int i = 0; i < n - 1; i++) {
nums[i] = nums[i + 1];
}
nums[n - 1] = first;
}
System.out.println("\nRotated Array (Left by 1):");
for (int i = 0; i < n; i++) {
System.out.print(nums[i] + " ");
}
}
}