-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightRotateBySteps.java
More file actions
58 lines (44 loc) · 1.37 KB
/
RightRotateBySteps.java
File metadata and controls
58 lines (44 loc) · 1.37 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
54
55
56
57
58
package array_Programming.medium.day_13;
//1. Write a Java program to rotate an array to the right by a given number of steps.
import java.util.Arrays;
import java.util.Scanner;
public class RightRotateBySteps
{
public static void rightRotate(int[] arr, int k)
{
int n = arr.length;
k = k % n;
int[] temp = new int[n];
int index = 0;
for (int i = n - k; i < n; i++)
{
temp[index++] = arr[i];
}
for (int i = 0; i < n - k; i++)
{
temp[index++] = arr[i];
}
for (int i = 0; i < n; i++)
{
arr[i] = temp[i];
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("Enter " + size + " elements:");
for (int i = 0; i < arr.length; i++)
{
arr[i] = sc.nextInt();
}
System.out.println("Enter number of right rotations:");
int k = sc.nextInt();
System.out.println("Original array: " + Arrays.toString(arr));
rightRotate(arr, k);
System.out.println("Array after right rotation: " + Arrays.toString(arr));
sc.close();
}
}