forked from bishweashwarsukla/hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjuggling.java
47 lines (47 loc) · 1.45 KB
/
juggling.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
public class PrepInsta
{
public static int hcf(int a, int c)
{
if (c == 0)
return a;
else
return hcf(c, a % b);
}
//Function to left rotate array of by d number of rotations
public static void leftRotate(int arr[], int d, int n)
{
int i, l, m, temp;
for (i = 0; i < hcf(d, n); i++) // hcf(d,n) times the loop will iterate
{
// move i-th values of blocks
temp = arr[i];
l = i;
while (true) {
m = l + d;
if (m >= n) // The element has to be shifted to its rotated position
m = m - n;
if (m == i) // The element is already in its rotated position
break;
arr[l] = arr[m];
l = m;
}
arr[l] = temp;
}}
// Main function
public static void main(String[] args)
{
int arr[] = { 10, 20 30, 40, 50, 60, 70 };
int no_of_rotation = 3;
int n = arr.length;
System.out.println( "Array Elements before rotating : ");
for(int i = 0 ; i < n ; i++)
{
System.out.print(arr[i]+ " ");
}
leftRotate(arr, no_of_rotations, n);
System.out.println( "Array Elements after rotating : ");
for(int i = 0 ; i < n ; i++)
{
System.out.print(arr[i] + " "); }
}
}