-2

Here is the Link of the problem which I was solving on Hackerrank:-https://www.hackerrank.com/challenges/circular-array-rotation/problem

Here is my code this code passes all the testcases except the fourth test will someone please help me what's wrong here.

public class Solution {
    public static void main(String[] args) {
        try(Scanner in = new Scanner(System.in)) {
            int n = in.nextInt();
            int k = in.nextInt();
            int q = in.nextInt();
            int a[] = new int[n];

            for(int i = 0; i < n; i++)
                a[i] = in.nextInt();

            for(int i = 0; i < q; i++) {
                int m = in.nextInt();
                System.out.println(a[(n - k + m) % n]);
            }
        }
    }

}

Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35
Saurabh Dubey
  • 17
  • 1
  • 4

1 Answers1

1

You're almost there! You forget a case where k is greater than length of the array (i.e. more than one cycle); so you have to limit it only to one cycle.

a[(n - (k % n) + m) % n];
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35