HackerRank Java Circular Array Rotation
John Watson knows of an operation called a right circular rotation on an array of integers. One rotation operation moves the last array element to the first position and shifts all remaining elements right one. To test Sherlock's abilities, Watson provides Sherlock with an array of integers. Sherlock is to perform the rotation operation a number of times then determine the value of the element at a given position.
For each array, perform a number of right circular rotations and return the value of the element at a given index.
For example, array , number of rotations, and indices to check, .
First we perform the two rotations:
Now return the values from the zero-based indices and as indicated in the array.
First we perform the two rotations:
Now return the values from the zero-based indices and as indicated in the array.
Function Description
Complete the circularArrayRotation function in the editor below. It should return an array of integers representing the values at the specified indices.
circularArrayRotation has the following parameter(s):
- a: an array of integers to rotate
- k: an integer, the rotation count
- queries: an array of integers, the indices to report
Input Format
The first line contains space-separated integers, , , and , the number of elements in the integer array, the rotation count and the number of queries.
The second line contains space-separated integers, where each integer describes array element (where ).
Each of the subsequent lines contains a single integer denoting , the index of the element to return from .
The second line contains space-separated integers, where each integer describes array element (where ).
Each of the subsequent lines contains a single integer denoting , the index of the element to return from .
Constraints
Output Format
For each query, print the value of the element at index of the rotated array on a new line.
Sample Input 0
3 2 3
1 2 3
0
1
2
Sample Output 0
2
3
1
Explanation 0
After the first rotation, the array becomes .
After the second (and final) rotation, the array becomes .
After the second (and final) rotation, the array becomes .
Let's refer to the array's final state as array . For each query, we just have to print the value of on a new line:
- , .
- , .
- , .
문제 요약 :
첫번째 줄 인풋
배열길이, rotation 횟수, query 개수
두번째 줄 인풋
배열
세번째 줄 부터는 query 개수 만큼 배열이 주어진다.
주어진 배열을 주어진 횟수 만큼 rotation 시킨 후 query에 있는 element를 뽑아내면 된다.
중요한 포인트는 rotation 횟수를 효율적으로 연산하는 방법이다.
예를 들어 [ 1, 2, 3, 4, 5 ] 라는 배열이 있을 때,
1번 rotation [ 5, 1, 2, 3, 4 ]
2번 rotation [ 4, 5, 1, 2, 3 ]
3번 rotation [ 3, 4, 5, 1, 2 ]
4번 rotation [ 2, 3, 4, 5, 1 ]
5번 rotation [ 1, 2, 3, 4, 5 ]
결론은 배열의 길이의 주기로 rotation이 작동한다.
위의 예시에서 배열의 길이는 5이다.
따라서 rotation 횟수가 터무니 없는 횟수가 들어와도 rotation의 경우는 배열의 길이 범주에서 해결된다.
그 다음 rotation의 로직의 포인트는 rotation 횟수를 인덱스로 삼아서
뒤쪽의 엘리먼트를 앞쪽에
앞쪽의 엘리먼트를 뒤쪽에 위치시켜 주면 된다.
ex) [ 1, 2, 3, 4, 5 ] 에서 3번 rotation 시킬 경우
배열의 인덱스 3의 위치를 기준으로
즉, 엘리먼트 4의 위치부터 배열의 맨 처음 엘리먼트를 채운다.
=> [ 0, 0, 0, 1, 2 ]
그리고 배열의 길이 - rotation 횟수
즉, 지금의 예시 0 1 2 인덱스에 차례 대로 엘리먼트를 채운다
=> [ 3, 4, 5, 1, 2]
댓글
댓글 쓰기