forked from TECHOUS/DSKaKhel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateArray.java
More file actions
41 lines (40 loc) · 1009 Bytes
/
RotateArray.java
File metadata and controls
41 lines (40 loc) · 1009 Bytes
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
import java.util.*;
public class RotateArray
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int arraySize = in.nextInt();
int[] arr = new int[arraySize];
for(int i=0;i<arr.length;i++)
{
arr[i] = in.nextInt();
}
int rotation = in.nextInt();
arr = solve(arr,rotation);
System.out.println(Arrays.toString(arr));
in.close();
}
/**
* this function contains the logic for rotating array to right side
* Time Complexity: O(n*rotation)
* Space Complexity: O(1)
* @param arr
* @param rotation
* @return
**/
private static int[] solve(int[] arr, int rotation)
{
for(int i=0;i<rotation;i++)
{
int temp = arr[0];
int j = 0;
for(j=0;j<arr.length-1;j++)
{
arr[j] = arr[j+1];
}
arr[j] = temp;
}
return arr;
}
}