Only case 4 gives runtime error. I looked at other answers but couldn't find a solution
CodePudding user response:
k
can be much larger than the length of the array, so your approach is failing since the index is much larger than the array length.
To correctly handle this, make k
equal to k modulus array_length
, since rotating the array by array_length
times effectively makes no changes to the current ordering.