forked from GauravWalia19/Algorithms-and-Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayRotation2.java
More file actions
35 lines (34 loc) · 771 Bytes
/
ArrayRotation2.java
File metadata and controls
35 lines (34 loc) · 771 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
/*
* ARRAY ROTATION USING ONE ELEMENT
* LEFT ROTATE BY 2
*/
import java.util.*;
public class ArrayRotation2
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int[] arr={1,2,3,4,5,6,7};
int shift=3;
rotate(arr, shift, arr.length);
//printing array
for(int i=0;i<arr.length;i++)
{
System.out.print(arr[i]+" ");
}
in.close();
}
public static void rotate(int[] arr,int d,int n)
{
//we can shift one by one
for(int i=0;i<d;i++)
{
int temp = arr[0];
for(int j=0;j<n-1;j++)
{
arr[j]=arr[j+1];
}
arr[n-1]=temp;
}
}
}