patternjavaMinor
Circular array rotation Java
Viewed 0 times
arraycircularjavarotation
Problem
I have a solution to the HackerRank Circular Array Rotation challenge. It passes 7 test cases out of 15. The rest of them are getting timed out. I think it's because of the huge data set that has been given as the input.
Input Format
The first line contains 3 space-separated integers, n (the length of the array), k (the number of right circular rotations), and q (the number of queries).
The second line contains n space-separated integers a0, a1, a2, …, an-1.
Each of the q subsequent lines contains a single integer denoting m. For each of those queries, output am of the rotated array.
Constraints
Can you point me out how can I improve this code in order to avoid those time out of test cases?
```
public class CircularArrayRotation {
public static int[] circularArray(int[] beforeArray){
int[] afterArray = new int[beforeArray.length];
afterArray[0] = beforeArray[beforeArray.length-1];
System.arraycopy(beforeArray,0,afterArray,1,beforeArray.length-1);
return afterArray;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int q = sc.nextInt();
sc.nextLine();
int[] source = new int[n];
String[] elements = sc.nextLine().split(" ");
for (int i=0;i<elements.length;i++){
source[i] = Integer.parseInt(elements[i]);
}
source = repeatCirculating(source,k);
int[] ques = new int[q];
for (int i=0;i<q;i++){
int position = Integer.parseInt(sc.nextLine().trim());
ques[i] = position;
}
for (int ask:ques) {
System.out.println(source[ask]);
}
}
public static int[] repeatCirculating(int[] source, int times){
for (int i =0; i<times; i++){
source = circularArray(sour
Input Format
The first line contains 3 space-separated integers, n (the length of the array), k (the number of right circular rotations), and q (the number of queries).
The second line contains n space-separated integers a0, a1, a2, …, an-1.
Each of the q subsequent lines contains a single integer denoting m. For each of those queries, output am of the rotated array.
Constraints
- 1 ≤ n ≤ 105
- 1 ≤ ai ≤ 105
- 1 ≤ k ≤ 105
- 1 ≤ q ≤ 500
Can you point me out how can I improve this code in order to avoid those time out of test cases?
```
public class CircularArrayRotation {
public static int[] circularArray(int[] beforeArray){
int[] afterArray = new int[beforeArray.length];
afterArray[0] = beforeArray[beforeArray.length-1];
System.arraycopy(beforeArray,0,afterArray,1,beforeArray.length-1);
return afterArray;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int q = sc.nextInt();
sc.nextLine();
int[] source = new int[n];
String[] elements = sc.nextLine().split(" ");
for (int i=0;i<elements.length;i++){
source[i] = Integer.parseInt(elements[i]);
}
source = repeatCirculating(source,k);
int[] ques = new int[q];
for (int i=0;i<q;i++){
int position = Integer.parseInt(sc.nextLine().trim());
ques[i] = position;
}
for (int ask:ques) {
System.out.println(source[ask]);
}
}
public static int[] repeatCirculating(int[] source, int times){
for (int i =0; i<times; i++){
source = circularArray(sour
Solution
The array may be up to 105 elements long. If you actually perform the rotation, then you will be copying at least 105 elements. (You actually do ridiculously more work, as @JoeC and @OhadR have both pointed out.)
However, there will be at most 500 queries. It would be nice if you didn't have to modify 105 entries just to satisfy 500 queries. You don't actually need to perform the rotation — you only need to pretend to have performed the rotation.
Strictly speaking, you don't even need to parse the ai as integers — you just need to read and regurgitate them. You also don't need to store all q queries — you can just reply to each one as soon as you read m.
However, there will be at most 500 queries. It would be nice if you didn't have to modify 105 entries just to satisfy 500 queries. You don't actually need to perform the rotation — you only need to pretend to have performed the rotation.
import java.util.Scanner;
public class CircularArrayRotation {
/**
* Performs one query.
*
* @param a The original array
* @param k The number of right circular rotations
* @param m The index of the rotated array to retrieve
*/
public static T query(T[] a, int k, int m) {
int n = a.length;
return a[(((m - k) % n) + n) % n];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), k = sc.nextInt(), q = sc.nextInt();
sc.nextLine(); // End of first line
String[] a = sc.nextLine().split(" "); // Second line
while (q-- > 0) { // Perform q queries
System.out.println(query(a, k, sc.nextInt()));
}
}
}Strictly speaking, you don't even need to parse the ai as integers — you just need to read and regurgitate them. You also don't need to store all q queries — you can just reply to each one as soon as you read m.
Code Snippets
import java.util.Scanner;
public class CircularArrayRotation {
/**
* Performs one query.
*
* @param a The original array
* @param k The number of right circular rotations
* @param m The index of the rotated array to retrieve
*/
public static <T> T query(T[] a, int k, int m) {
int n = a.length;
return a[(((m - k) % n) + n) % n];
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), k = sc.nextInt(), q = sc.nextInt();
sc.nextLine(); // End of first line
String[] a = sc.nextLine().split(" "); // Second line
while (q-- > 0) { // Perform q queries
System.out.println(query(a, k, sc.nextInt()));
}
}
}Context
StackExchange Code Review Q#145643, answer score: 6
Revisions (0)
No revisions yet.