- Java Online Training In Andhra Pradesh and Telangana

Opening Hours :7AM to 9PM

This program uses the concept of "juggling" to rotate the elements of the array. It first uses the gcd (greatest common divisor) of the array length and the number of rotations to determine the number of sets. Then, it repeatedly swaps the elements in each set until all elements have been moved to their final positions.import java.util.Arrays; class ArrayRotate { public void leftRotate(int[] arr, int n, int k) { int i, j, temp; for (i = 0; i < gcd(n, k); i++) { temp = arr[i]; j = i; while (true) { int d = j + k; if (d >= n) { d = d - n; } if (d == i) { break; } arr[j] = arr[d]; j = d; } arr[j] = temp; } } public int gcd(int n, int k) { if (k == 0) { return n; } else { return gcd(k, n % k); } } } public class Main { public static void main(String[] args) { ArrayRotate ar=new ArrayRotate(); int[] arr = {1, 2, 3, 4, 5}; int n = arr.length; int k = 4; ar.leftRotate(arr, n, k); System.out.println(Arrays.toString(arr)); } }

In this example, the array [1, 2, 3, 4, 5] is rotated 3 times to the left, so the output is [4, 5, 1, 2, 3].

You can change the number of rotations you want to do by changing the value of k.

- 13 . Sunny Number in Java

- 14 . Tech Number in Java

- 15 . Keith Number in Java

- 16 . Neon Number in Java

- 17 . Spy Number in Java

- 18 . ATM program Java

- 19 . Emirp Number in Java

- 54 . Write a Java program to count the letters, spaces, numbers and other characters of an input string

- 100 . Write a Java program that takes a year from user and print whether that year is a leap year or not

- Duaration(Core & Advanced) : 90 Hours
- Leactures : 90
- Quizzes : 60
- Interview Questions : 600
- Important Programs: 300
- Seminars: 20