https://www.codechef.com/TNP42020/problems/TNP404

PROBLEM LINK: CodeChef: Practical coding for everyone

Practice

Author: Setter’s name
Tester: Tester’s name
Editorialist: Editorialist’s name
DIFFICULTY : EASY

PREREQUISITES:

Array or Matrix, loops.

PROBLEM:

A number of students are made to stand in n rows and m columns. Each student is identified by a random number. After making them stand in a row by column way it was noticed that the arrangement of the outer students was wrong and the position had to be shifted by 1 place counter clockwise. Display the corrected arrangement?

QUICK EXPLANATION:

Just change the position of outer elements of matrix in counterclockwise direction.

EXPLANATION:

There are 3 cases :
1.Rows=1 and Columns ≥ 1
2.Rows ≥ 1 and Columns = 1
3.All other cases (Rows >1 Columns > 1 )

Take 3 cases using if else if statements,
One approach
Use nested array, Store each row in an array and store it in another array

In case 1 print first row in reverse order
In case 2 print first column in reverse order
In case 3: Store the first element in a temporary variable, iterate through the first row and move elements left by one position. Then iterate through the first column from top to bottom, then last row (from left to right) and finally through the last column (bottom to top). Finally assign the value in the temporary variable to the first element of the second row.

SOLUTIONS:

Setter's Solution

#include
using namespace std;
int main() {
int m, n, nums[6][6];
cin>>m>>n;
for(int i=0; i<m; i++)
for(int j=0; j<n; j++)
cin>>nums[i][j];
int temp = nums[0][0];
if(n>1)
for(int i=1; i<n; i++)
nums[0][i-1] = nums[0][i];
if(m>1)
for(int i=1; i<m; i++)
nums[i-1][n-1] = nums[i][n-1];
if(n>1)
for(int i=n-2; i>=0; i–)
nums[m-1][i+1] = nums[m-1][i];
if(n>1){
for(int i=m-2; i>=0; i–)
nums[i+1][0] = nums[i][0];
nums[1][0] = temp;
}

for(int i=0; i<m; i++){
    for(int j=0; j<n; j++)
        cout<<nums[i][j]<<'\t';
    cout<<'\n';    
}
return 0;

}

Tester's Solution

Same Person

Editorialist's Solution

Same Person