Write a function that rotates an integer array by a given number k. k elements from the end should move to the beginning of array, and all other elements should move to right to make the space.
The rotation should be done in-place.
Algorithm should not run in more than O(n), where n is the size of array.
Also a constant memory must be used to perform the operation.
For example,
if array is initialized with elements arr = {1, 2, 3, 4, 5, 6, 7, 8, 9}
rotate(arr, 3) will result the elements to be {7, 8, 9, 1, 2, 3, 4, 5, 6}
rotate(arr, 6) will result the {4, 5, 6, 7, 8, 9, 1, 2, 3}