CTKEY Editorial


#1

PROBLEM LINK:

Practice
Contest

Author: Siddhartha Paul
Editorialist: Baban Gain

DIFFICULTY:

EASY

PREREQUISITES:

None

PROBLEM:

A 8*8 matrix will be given. Rotate all elements of a selected row or column as specified by input.

EXPLANATION:

Let N = 8
If no. of shifts is greater than N,
then no, of shift N+1, 2N+1, 3N+1… are equivalent to 1 shift
similarly N+2, 2N+, 3N+2… are equivalent to 2 shift and so on.

Initialise an temporary array of size N.
So, if it is a Row operation,
for every element in that row R,
      temp[(i + shift) % N] = arr[R][i]
and copy back the temporary array to original matrix’s Rth row.

And it is a Column operation,
for every element in that col C,
      temp[(i + shift) % N] = arr[i][C];
and copy back the temporary array to original matrix’s Cth column.

Time Complexity: O(N) for each shift operation

AUTHOR’S SOLUTIONS:

Click for Python
Click for C++


#2

Time Complexity is not O(1) , please take care of such things when you are especially an editorialist of a problem.


#3

Thank you for pointing that out.