We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // number of elements
int d = in.nextInt(); // number of left rotations
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
// Perform left rotations efficiently
int[] rotated = new int[n];
for (int i = 0; i < n; i++) {
rotated[i] = arr[(i + d) % n];
}
// Print result
for (int i = 0; i < n; i++) {
System.out.print(rotated[i] + (i < n - 1 ? " " : ""));
}
in.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Left Rotation
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.;
public class Solution {
}