Rotate by 90 Degrees Counter-Clockwise - Amazon Top Interview Questions


Problem Statement :


Given a two-dimensional square matrix, rotate in-place it 90 degrees counter-clockwise.

Constraints

n = m ≤ 250 where n and m are the number of rows and columns in matrix

Example 1

Input

matrix = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
]

Output

[
    [3, 6, 9],
    [2, 5, 8],
    [1, 4, 7]
]



Solution :



title-img




                        Solution in C++ :

void modify(int pos, vector<vector<int>>& matrix) {
    int n = matrix.size();
    for (int i = 0; i < n - 2 * pos - 1; ++i) {
        int tmp = matrix[n - 1 - pos - i][pos], tmp1;
        swap(matrix[pos][pos + i], matrix[n - 1 - pos - i][pos]);
        swap(tmp, matrix[n - 1 - pos][n - 1 - pos - i]);
        swap(tmp, matrix[pos + i][n - 1 - pos]);
        swap(tmp, matrix[pos][pos + i]);
    }
}

vector<vector<int>> solve(vector<vector<int>>& matrix) {
    for (int i = 0, n = matrix.size(); n > 1; i++, n -= 2) {
        modify(i, matrix);
    }

    return matrix;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int[][] solve(int[][] matrix) {
        for (int[] row : matrix) {
            reverse(row);
        }
        int n = matrix.length;
        int m = matrix[0].length;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < i; j++) {
                int tmp = matrix[i][j];
                matrix[i][j] = matrix[j][i];
                matrix[j][i] = tmp;
            }
        }
        return matrix;
    }

    private void reverse(int[] arr) {
        int i = 0;
        int j = arr.length - 1;
        while (i < j) {
            int tmp = arr[i];
            arr[i] = arr[j];
            arr[j] = tmp;
            i++;
            j--;
        }
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, matrix):

        for i in range(0, len(matrix)):
            for j in range(i, len(matrix)):
                matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]

        for i in range(0, len(matrix) // 2):
            matrix[i], matrix[len(matrix) - i - 1] = matrix[len(matrix) - i - 1], matrix[i]

        return matrix
                    


View More Similar Problems

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →

Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

View Solution →

Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.

View Solution →