-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRightRotateArray12(3).java
49 lines (44 loc) · 1.25 KB
/
RightRotateArray12(3).java
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
import java.util.*;
class RightRotateArray
{
void rotate(int a[], int n, int r)
{
int i;
System.out.println("Original Array: ");
for(i=0;i<n;i++)
{
System.out.print(a[i] + " ");
}
for (int j=1;j<=r;j++)
{
int x = a[n-1];
for(i=n-1;i>0;i--)
{
a[i] = a[i-1];
}
a[0] = x;
}
System.out.println(" ");
System.out.println("Right Rotated Array: ");
for(i=0;i<n;i++)
{
System.out.print(a[i] + " ");
}
}
public static void main(String[] args)
{
Scanner ob = new Scanner(System.in);
System.out.println("Enter the number of elements in the array");
int n = ob.nextInt();
int A[] = new int[n];
System.out.println("Enter the number of times the array needs to be rotated right");
int r = ob.nextInt();
System.out.println("Enter elements in array");
for(int j=0;j<n;j++)
{
A[j] = ob.nextInt();
}
RightRotateArray rra = new RightRotateArray();
rra.rotate(A, n, r);
}
}