-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRightRotationBFM.java
More file actions
50 lines (34 loc) · 1.17 KB
/
RightRotationBFM.java
File metadata and controls
50 lines (34 loc) · 1.17 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
package DSA;
import java.util.Scanner;
public class RightRotationBFM {
//THIS IS THE BRUTE FORCE APPROACH
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of array : ");
int size = sc.nextInt();
int[] arr = new int[size];
System.out.println("\nEnter the array elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("Element %d : ", i);
arr[i] = sc.nextInt();
}
System.out.print("\nEnter number of place/s by which the array should be rotated : ");
int num = sc.nextInt();
System.out.print("\nArray before rotation : ");
for(int i : arr){
System.out.print(i + " ");
}
num%=size;
for(int i=1;i<=num;i++){
int temp = arr[size-1];
for(int j=size-2;j>=0;j--){
arr[j+1]=arr[j];
}
arr[0]=temp;
}
System.out.print("\nArray after rotation : ");
for(int j : arr){
System.out.print(j + " ");
}
}
}