-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArrayByKSteps.java
More file actions
53 lines (48 loc) · 1.19 KB
/
Copy pathRotateArrayByKSteps.java
File metadata and controls
53 lines (48 loc) · 1.19 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
51
52
53
package Array1D;
import java.util.Scanner;
class Solution{
public void rotate(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);
}
public void reverse(int arr[], int start, int end)
{
while(start<end)
{
int temp=arr[start];
arr[start]=arr[end];
arr[end]=temp;
start++;
end--;
}
}
}
public class RotateArrayByKSteps {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of Array: ");
int n= sc.nextInt();
int arr[]=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
System.out.println("Enter the number of steps you want to rotate:");
int k= sc.nextInt();
// k = k%n;
Solution sl=new Solution();
sl.rotate(arr,k);
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
}
}