Skip to main content

Command Palette

Search for a command to run...

Matrix (Level-2)

Questions on matrix level-2

Published
3 min readView as Markdown
Matrix (Level-2)
F

Assalamualaikum warahmatullah wabarakatuh( traditional Islamic greeting in Arabic "Assalamu alaikum": "Peace be upon you." "Wa rahmatullahi": "And the mercy of Allah." "Wa barakatuh": "And His blessings.") I’m Faraz Alam, and I’m documenting my journey through the world of software technology. Despite earning a master’s degree in Computer Applications and having access to opportunities provided by my tier-3 college, I struggled to take full advantage of them due to poor management and a less productive environment. This led to joblessness, primarily due to a lack of upskilling. Now, I am dedicated to enhancing my skills and knowledge with the aim of securing a valuable job offer from leading product-based companies, including those in the FAANG group (Facebook, Amazon, Apple, Netflix, Google) and other prominent tech giants. This documentation is not for self-promotion; rather, it is for anyone who is waiting for an opportunity but feels they lack the tools and skills required to overcome challenges. It’s a testament to the effort and responsibility needed to navigate the journey towards success when you take charge of your own path. Date: 31 July 2024, 07:25 AM This page will be updated regularly to reflect new achievements and milestones as I continue to build my career.

  1. Printing matrix in snake pattern.

     void printSnake(const vector<vector<int>> &arr)
     {
       int r = arr.size();
       int c = arr[0].size();
       for (int i = 0; i < r; i++)
       {
         if (i % 2 == 0)
         {
           for (int j = 0; j < c; j++)
           {
             cout << arr[i][j] << " ";
           }
         }
         else
         {
           for (int j = c - 1; j >= 0; j--)
           {
             cout << arr[i][j] << " ";
           }
         }
       }
     }
    

2) Rotate matrix by 90degree clockwise and anticlockwise

const int n = 4;
void rotateAnti(int mat[n][n])
{
  for (int i = 0; i < n; i++)
  {
    for (int j = i + 1; j < n; j++)
    {
      int temp = mat[i][j];
      mat[i][j] = mat[j][i];
      mat[j][i] = temp;
    }
  }
  for (int i = 0; i < n; i++)
  {
    int low = 0;
    int high = n - 1;
    while (low < high)
    {
      int temp = mat[low][i];
      mat[low][i] = mat[high][i]; // for clockwise, arr[i][low] and arr[i][high] are swapped
      mat[high][i] = temp;
      low++;
      high--;
    }
  }
}

3) Search in row and column wise sorted matrix

//stair case search
const int R = 4, C = 4;
void search(int arr[R][C], int x)
{
  int i = 0;
  int j = C - 1;
  while (i < R && j >= 0)
  {
    if (arr[i][j] == x)
    {
      cout << "found at (" << i << "," << j << ")";
      return;
    }
    else if (arr[i][j] > x)
    {
      j--;
    }
    else
    {
      i++;
    }
  }
  cout << "Not found";
}

4) Boundary traversal of matrix

/* Boundary Traversal in a matrix */
/* ------------------------------- */
/* Example:-
    Input:-    1   2   3   4
               5   6   7   8
               9  10  11  12
    Output:- 1  2  3  4  8  12 11 10 9 5
*/
#include <iostream>
#include <vector>
using namespace std;
void printBoundary(const vector<vector<int>> &arr)
{
  int m = arr.size();
  int n = arr[0].size();
  for (int i = 0; i < n; i++)
  {
    cout << arr[0][i] << " ";
  }
  for (int i = 1; i < m; i++)
  {
    cout << arr[i][n - 1] << " ";
  }
  if (m > 1)
  {
    for (int i = n - 2; i >= 0; i--)
    {
      cout << arr[m - 1][i] << " ";
    }
  }
  if (n > 1)
  {
    for (int i = m - 2; i >= 1; i--)
    {
      cout << arr[i][0] << " ";
    }
  }
}

5) Spiral traversal of matrix

#include <iostream>
using namespace std;

const int R = 4, C = 4;

void printSpiral(int mat[R][C], int R, int C)
{
  int top = 0, left = 0, bottom = R - 1, right = C - 1;

  while (top <= bottom && left <= right)
  {
    // Top row
    for (int i = left; i <= right; i++)
    {
      cout << mat[top][i] << " ";
    }
    top++;

    // Right column
    for (int i = top; i <= bottom; i++)
    {
      cout << mat[i][right] << " ";
    }
    right--;

    // Bottom row
    if (top <= bottom)
    {
      for (int i = right; i >= left; i--)
      {
        cout << mat[bottom][i] << " ";
      }
      bottom--;
    }

    // Left column
    if (left <= right)
    {
      for (int i = bottom; i >= top; i--)
      {
        cout << mat[i][left] << " ";
      }
      left++;
    }
  }
}

int main()
{
  int arr[R][C] = {
      {1, 2, 3, 4},
      {5, 6, 7, 8},
      {9, 10, 11, 12},
      {13, 14, 15, 16}};

  printSpiral(arr, R, C);

  return 0;
}