Banana-Leopard/TASTY huggingface dataset:
// CPP Program to check whether given matrix
// is Diagonally Dominant Matrix.
#include <bits/stdc++.h>
#define N 3
using namespace std;
 
// check the given  matrix is Diagonally
// Dominant Matrix or not.
bool isDDM(int m[N][N], int n)
{
    // for each row
    for (int i = 0; i < n; i++)
   {       
 
        // for each column, finding sum of each row.
        int sum = 0;
        for (int j = 0; j < n; j++)            
            sum += abs(m[i][j]);       
 
        // removing the diagonal element.
        sum -= abs(m[i][i]);
 
        // checking if diagonal element is less
        // than sum of non-diagonal element.
        if (abs(m[i][i]) < sum)
            return false;
        
    }
 
    return true;
}
 
// Driven Program
int main()
{
    int n = 3;
    int m[N][N] = { { 3, -2, 1 },
                    { 1, -3, 2 },
                    { -1, 2, 4 } };
 
    (isDDM(m, n)) ? (cout << "YES") : (cout << "NO");
 
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ Program to Find minimum number of operation required
// such that sum of elements on each row and column becomes
// same*/
#include <bits/stdc++.h>
using namespace std;
 
// Function to find minimum operation required to make sum
// of each row and column equals
int findMinOpeartion(int matrix[][2], int n)
{
    // Initialize the sumRow[] and sumCol[] array to 0
    int sumRow[n], sumCol[n];
    memset(sumRow, 0, sizeof(sumRow));
    memset(sumCol, 0, sizeof(sumCol));
    // Calculate sumRow[] and sumCol[] array
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) {
            sumRow[i] += matrix[i][j];
            sumCol[j] += matrix[i][j];
        }
    // Find maximum sum value in either row or in column
    int maxSum = 0;
    for (int i = 0; i < n; ++i) {
        maxSum = max(maxSum, sumRow[i]);
        maxSum = max(maxSum, sumCol[i]);
    }
    int count = 0;
    for (int i = 0, j = 0; i < n && j < n;) {
        // Find minimum increment required in either row or
        // column
        int diff
            = min(maxSum - sumRow[i], maxSum - sumCol[j]);
        // Add difference in corresponding cell, sumRow[]
        // and sumCol[] array
        matrix[i][j] += diff;
        sumRow[i] += diff;
        sumCol[j] += diff;
        // Update the count variable
        count += diff;
        // If ith row satisfied, increment ith value for
        // next iteration
        if (sumRow[i] == maxSum)
            ++i;
        // If jth column satisfied, increment jth value for
        // next iteration
        if (sumCol[j] == maxSum)
            ++j;
    }
    return count;
}
 
// Utility function to print matrix
void printMatrix(int matrix[][2], int n)
{
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j)
            cout << matrix[i][j] << " ";
        cout << "\n";
    }
}
 
// Driver code
int main()
{
    int matrix[][2] = { { 1, 2 }, { 3, 4 } };
    cout << findMinOpeartion(matrix, 2) << "\n";
    printMatrix(matrix, 2);
    return 0;
}
 
// This code is contributed by Sania Kumari Gupta

Time complexity: quadratic
Space complexity: linear
// CPP program to find the frequency of k
// in matrix where m(i, j)=i+j
#include <bits/stdc++.h>
using namespace std;
int find(int n, int k)
{
    if (n + 1 >= k)
        return (k - 1);
    else
        return (2 * n + 1 - k);
}
 
// Driver Code
int main()
{
    int n = 4, k = 7;
    int freq = find(n, k);
    if (freq < 0)
        cout << " element not exist \n ";
    else
        cout << " Frequency of " << k
             << " is " << freq << "\n";
    return 0;
}

Time complexity: constant
Space complexity: constant
// CPP program to print  given number of 1's,
// 2's, 3's ....k's in  zig-zag way.
#include <bits/stdc++.h>
using namespace std;
 
// function that prints  given number of 1's,
// 2's, 3's ....k's in zig-zag way.
void ZigZag(int rows, int columns, int numbers[])
{
    int k = 0;
     
    // two-dimensional array to store numbers.
    int arr[rows][columns];
     
    for (int i=0; i<rows; i++)
    {
        // for even row.
        if (i%2==0)
        {
            // for each column.
            for (int j=0; j<columns and
                    numbers[k]>0; j++)
            {
                // storing element.
                arr[i][j] = k+1;
 
                // decrement element at
                // kth index.
                numbers[k]--;
                 
                // if array contains zero
                // then increment index to
                // make this next index
                if (numbers[k] == 0)
                    k++;
            }
        }
         
        // for odd row.
        else
        {
            // for each column.
            for (int j=columns-1; j>=0 and
                numbers[k]>0; j--)
            {
                // storing element.
                arr[i][j] = k+1;
                 
                // decrement element
                // at kth index.
                numbers[k]--;
                 
                // if array contains zero then
                // increment index to make this
                // next index.
                if (numbers[k]==0)
                    k++;
            }
        }
    }
     
    // printing the stored elements.
    for (int i=0;i<rows;i++)
    {
        for (int j=0;j<columns;j++)
            cout << arr[i][j] << " ";
             
        cout << endl;
    }
}
 
// Driver code for above function.
int main()
{
    int rows = 4;
    int columns = 5;
    int Numbers[] = {3, 4, 2, 2, 3, 1, 5};
    ZigZag(rows, columns, Numbers);   
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to find out the maximum product
// in the matrix which four elements are
// adjacent to each other in one direction
#include <bits/stdc++.h>
using namespace std;
 
const int n = 5;
 
// function to find max product
int FindMaxProduct(int arr[][n], int n)
{
    int max = 0, result;
 
    // iterate the rows.
    for (int i = 0; i < n; i++)
    {
 
        // iterate the columns.
        for (int j = 0; j < n; j++)
        {
 
            // check the maximum product
            // in horizontal row.
            if ((j - 3) >= 0)
            {
                result = arr[i][j] * arr[i][j - 1] *
                    arr[i][j - 2] * arr[i][j - 3];
                 
                if (max < result)
                    max = result;
            }
 
            // check the maximum product
            // in vertical row.
            if ((i - 3) >= 0)
            {
                result = arr[i][j] * arr[i - 1][j] *
                    arr[i - 2][j] * arr[i - 3][j];
                 
                if (max < result)
                    max = result;
            }
 
            // check the maximum product in
            // diagonal (going through down - right)
            if ((i - 3) >= 0 && (j - 3) >= 0)
            {
                result = arr[i][j] * arr[i - 1][j - 1] *
                    arr[i - 2][j - 2] * arr[i - 3][j - 3];
                 
                if (max < result)
                    max = result;
            }
             
            // check the maximum product in
            // diagonal (going through up - right)
            if ((i - 3) >= 0 && (j - 3) <= 0)
            {
                result = arr[i][j] * arr[i - 1][j + 1] *
                    arr[i - 2][j + 2] * arr[i - 3][j + 3];
     
                if (max < result)
                    max = result;
            }
        }
    }
 
    return max;
}
 
// Driver code
int main()
{
 
    /* int arr[][4] = {{6, 2, 3, 4},
                    {5, 4, 3, 1},
                    {7, 4, 5, 6},
                    {8, 3, 1, 0}};*/
    /* int arr[][5] = {{1, 2, 1, 3, 4},
                    {5, 6, 3, 9, 2},
                    {7, 8, 8, 1, 2},
                    {1, 0, 7, 9, 3},
                    {3, 0, 8, 4, 9}};*/
                         
    int arr[][5] = {{1, 2, 3, 4, 5},
                    {6, 7, 8, 9, 1},
                    {2, 3, 4, 5, 6},
                    {7, 8, 9, 1, 0},
                    {9, 6, 4, 2, 3}};
 
    cout << FindMaxProduct(arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
 
int maxPro(int a[6][5], int n, int m, int k)
{
    int maxi(1), mp(1);
    for (int i = 0; i < n; ++i)
    {
        // Window Product for each row.
        int wp(1);
        for (int l = 0; l < k; ++l)
        {
            wp *= a[i][l];
        }
       
        // Maximum window product for each row
        mp = wp;
        for (int j = k; j < m; ++j)
        {
            wp = wp * a[i][j] / a[i][j - k];
           
            // Global maximum window product
            maxi = max(maxi,max(mp,wp));
        }
    }
    return maxi;
}
 
// Driver Code
int main()
{
    int n = 6, m = 5, k = 4;
    int a[6][5] = { { 1, 2, 3, 4, 5 },
                    { 6, 7, 8, 9, 1 },
                    { 2, 3, 4, 5, 6 },
                    { 7, 8, 9, 1, 0 },
                    { 9, 6, 4, 2, 3 },
                   { 1, 1, 2, 1, 1 } };
 
    cout << maxPro(a, n, m, k);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// Program to check lower
// triangular matrix.
#include <bits/stdc++.h>
#define N 4
using namespace std;
 
// Function to check matrix is in
// lower triangular form or not.
bool isLowerTriangularMatrix(int mat[N][N])
{
    for (int i = 0; i < N-1; i++)
        for (int j = i + 1; j < N; j++)
            if (mat[i][j] != 0)
                return false;
    return true;
}
 
// Driver function.
int main()
{
    int mat[N][N] = { { 1, 0, 0, 0 },
                      { 1, 4, 0, 0 },
                      { 4, 6, 2, 0 },
                      { 0, 4, 7, 6 } };
 
    // Function call
    if (isLowerTriangularMatrix(mat))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// Program to check upper triangular matrix.
#include <bits/stdc++.h>
#define N 4
using namespace std;
 
// Function to check matrix is in upper triangular
// form or not.
bool isUpperTriangularMatrix(int mat[N][N])
{
    for (int i = 1; i < N; i++)
        for (int j = 0; j < i; j++)
            if (mat[i][j] != 0)
                return false;
    return true;
}
 
// Driver function.
int main()
{
    int mat[N][N] = { { 1, 3, 5, 3 },
                      { 0, 4, 6, 2 },
                      { 0, 0, 2, 5 },
                      { 0, 0, 0, 6 } };
    if (isUpperTriangularMatrix(mat))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ Program to Find the frequency
// of even and odd numbers in a matrix
#include<bits/stdc++.h>
using namespace std;
 
#define MAX 100
 
// function for calculating frequency
void freq(int ar[][MAX], int m, int n)
{
    int even = 0, odd = 0;
     
    for (int i = 0; i < m; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            // modulo by 2 to check
            // even and odd
            if ((ar[i][j] % 2) == 0)
                ++even;
            else
                ++odd;
        }
    }
     
    // print Frequency of numbers
    printf(" Frequency of odd number = %d \n", odd);
    printf(" Frequency of even number = %d \n", even);
}
 
// Driver code
int main()
{
    int m = 3, n = 3;   
     
    int array[][MAX] = { { 1, 2, 3 },
                        { 4, 5, 6 },
                        { 7, 8, 9 } };
 
    freq(array, m, n);
    return 0;
}       

Time complexity: quadratic
Space complexity: constant
// C++ Program to check if the center
// element is equal to the individual
// sum of all the half diagonals
#include <stdio.h>
#include<bits/stdc++.h>
using namespace std;
 
const int MAX = 100;
 
// Function to Check center element
// is equal to the individual
// sum of all the half diagonals
bool HalfDiagonalSums(int mat[][MAX], int n)
{   
    // Find sums of half diagonals
    int diag1_left = 0, diag1_right = 0;
    int diag2_left = 0, diag2_right = 0;   
    for (int i = 0, j = n - 1; i < n; i++, j--) {
         
        if (i < n/2) {
            diag1_left += mat[i][i];
            diag2_left += mat[j][i];          
        }
        else if (i > n/2) {
            diag1_right += mat[i][i];
            diag2_right += mat[j][i];          
        }
    }
     
    return (diag1_left == diag2_right &&
            diag2_right == diag2_left &&
            diag1_right == diag2_left &&
            diag2_right == mat[n/2][n/2]);
}
 
// Driver code
int main()
{
    int a[][MAX] = { { 2, 9, 1, 4, -2},
                     { 6, 7, 2, 11, 4},
                     { 4, 2, 9, 2, 4},
                     { 1, 9, 2, 4, 4},
                     { 0, 2, 4, 2, 5} };
        cout << ( HalfDiagonalSums(a, 5) ? "Yes" : "No" );
    return 0;
}

Time complexity: linear
Space complexity: constant
// C++ program to print Identity Matrix
#include<bits/stdc++.h>
using namespace std;
 
int Identity(int num)
{
    int row, col;
     
    for (row = 0; row < num; row++)
    {
        for (col = 0; col < num; col++)
        {
            // Checking if row is equal to column
            if (row == col)
                cout << 1 << " ";
            else
                cout << 0 << " ";
        }
        cout << endl;
    }
    return 0;
}
 
// Driver Code
int main()
{
    int size = 5;
    Identity(size);
    return 0;
}
 
// This code is contributed by shubhamsingh10

Time complexity: quadratic
Space complexity: constant
// CPP program to check if a given matrix is identity
#include<iostream>
using namespace std;
 
const int MAX = 100;
 
bool isIdentity(int mat[][MAX], int N)
{
    for (int row = 0; row < N; row++)
    {
        for (int col = 0; col < N; col++)
        {
            if (row == col && mat[row][col] != 1)
                return false;
            else if (row != col && mat[row][col] != 0)
                return false;
        }
    }
    return true;
}
 
// Driver Code
int main()
{
    int N = 4;
    int mat[][MAX] = {{1, 0, 0, 0},
                    {0, 1, 0, 0},
                    {0, 0, 1, 0},
                    {0, 0, 0, 1}};
    if (isIdentity(mat, N))
       cout << "Yes ";
    else
       cout << "No ";
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// CPP Program to implement matrix
// for swapping the upper diagonal
// elements with lower diagonal
// elements of matrix.
#include <bits/stdc++.h>
#define n 4
using namespace std;
 
// Function to swap the diagonal
// elements in a matrix.
void swapUpperToLower(int arr[n][n])
{
    // Loop for swap the elements of matrix.
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            int temp = arr[i][j];
            arr[i][j] = arr[j][i];
            arr[j][i] = temp;
        }
    }
     
    // Loop for print the matrix elements.
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << arr[i][j] << " ";
        cout << endl;
    }
}
 
// Driver function to run the program
int main()
{
    int arr[n][n] = { { 2, 3, 5, 6 },
                    { 4, 5, 7, 9 },
                    { 8, 6, 4, 9 },
                    { 1, 3, 5, 6 } };
 
    // Function call
    swapUpperToLower(arr);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// CPP program to find sparse matrix rep-
// resentation using CSR
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
 
typedef std::vector<int> vi;
 
typedef vector<vector<int> > matrix;
 
// Utility Function to print a Matrix
void printMatrix(const matrix& M)
{
    int m = M.size();
    int n = M[0].size();
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << M[i][j] << " ";       
        cout << endl;
    }
}
 
// Utility Function to print A, IA, JA vectors
// with some decoration.
void printVector(const vi& V, char* msg)
{
 
    cout << msg << "[ ";
    for_each(V.begin(), V.end(), [](int a) {
        cout << a << " ";
    });
    cout << "]" << endl;
}
 
// Generate the three vectors A, IA, JA
void sparesify(const matrix& M)
{
    int m = M.size();
    int n = M[0].size(), i, j;
    vi A;
    vi IA = { 0 }; // IA matrix has N+1 rows
    vi JA;
    int NNZ = 0;
 
    for (i = 0; i < m; i++) {
        for (j = 0; j < n; j++) {
            if (M[i][j] != 0) {
                A.push_back(M[i][j]);
                JA.push_back(j);
 
                // Count Number of Non Zero
                // Elements in row i
                NNZ++;
            }
        }
        IA.push_back(NNZ);
    }
 
    printMatrix(M);
    printVector(A, (char*)"A = ");
    printVector(IA, (char*)"IA = ");
    printVector(JA, (char*)"JA = ");
}
 
// Driver code
int main()
{
    matrix M = {
        { 0, 0, 0, 0, 1 },
        { 5, 8, 0, 0, 0 },
        { 0, 0, 3, 0, 0 },
        { 0, 6, 0, 0, 1 },
    };
 
    sparesify(M);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// Simple CPP program to find mirror of
// matrix across diagonal.
#include <bits/stdc++.h>
using namespace std;
 
const int MAX = 100;
 
void imageSwap(int mat[][MAX], int n)
{
    // for diagonal which start from at
    // first row of matrix
    int row = 0;
 
    // traverse all top right diagonal
    for (int j = 0; j < n; j++) {
 
        // here we use stack for reversing
        // the element of diagonal
        stack<int> s;
        int i = row, k = j;
        while (i < n && k >= 0)
            s.push(mat[i++][k--]);
         
        // push all element back to matrix
        // in reverse order
        i = row, k = j;
        while (i < n && k >= 0) {
            mat[i++][k--] = s.top();
            s.pop();
        }
    }
 
    // do the same process for all the
    // diagonal which start from last
    // column
    int column = n - 1;
    for (int j = 1; j < n; j++) {
 
        // here we use stack for reversing
        // the elements of diagonal
        stack<int> s;
        int i = j, k = column;
        while (i < n && k >= 0)
            s.push(mat[i++][k--]);
         
        // push all element back to matrix
        // in reverse order
        i = j;
        k = column;
        while (i < n && k >= 0) {
            mat[i++][k--] = s.top();
            s.pop();
        }
    }
}
 
// Utility function to print a matrix
void printMatrix(int mat[][MAX], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }
}
 
// driver program to test above function
int main()
{
    int mat[][MAX] = { { 1, 2, 3, 4 },
                     { 5, 6, 7, 8 },
                     { 9, 10, 11, 12 },
                     { 13, 14, 15, 16 } };
    int n = 4;
    imageSwap(mat, n);
    printMatrix(mat, n);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// Efficient CPP program to find mirror of
// matrix across diagonal.
#include <bits/stdc++.h>
using namespace std;
 
const int MAX = 100;
 
void imageSwap(int mat[][MAX], int n)
{
    // traverse a matrix and swap
    // mat[i][j] with mat[j][i]
    for (int i = 0; i < n; i++)
        for (int j = 0; j <= i; j++)
            mat[i][j] = mat[i][j] + mat[j][i] -
                       (mat[j][i] = mat[i][j]);      
}
 
// Utility function to print a matrix
void printMatrix(int mat[][MAX], int n)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cout << mat[i][j] << " ";
        cout << endl;
    }
}
 
// driver program to test above function
int main()
{
    int mat[][MAX] = { { 1, 2, 3, 4 },
                     { 5, 6, 7, 8 },
                     { 9, 10, 11, 12 },
                     { 13, 14, 15, 16 } };
    int n = 4;
    imageSwap(mat, n);
    printMatrix(mat, n);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
void findend(int i,int j, vector<vector<int>> &a,
             vector<vector<int>> &output,int index)
{
  int x = a.size();
  int y = a[0].size();
 
  // flag to check column edge case,
  // initializing with 0
  int flagc = 0;
 
  // flag to check row edge case,
  // initializing with 0
  int flagr = 0;
  int n, m;
 
  for (m = i; m < x; m++)
  {
 
    // loop breaks where first 1 encounters
    if (a[m][j] == 1)
    {
      flagr = 1; // set the flag
      break;
    }
 
    // pass because already processed
    if (a[m][j] == 5) continue;
 
    for (n = j; n < y; n++)
    {
      // loop breaks where first 1 encounters
      if (a[m][n] == 1)
      {
        flagc = 1; // set the flag
        break;
      }
 
      // fill rectangle elements with any
      // number so that we can exclude
      // next time
      a[m][n] = 5;
    }
  }
 
  if (flagr == 1)
    output[index].push_back(m-1);
  else
    // when end point touch the boundary
    output[index].push_back(m);
 
  if (flagc == 1)
    output[index].push_back(n-1);
  else
    // when end point touch the boundary
    output[index].push_back(n);
}
 
void get_rectangle_coordinates(vector<vector<int>> a)
{
 
  // retrieving the column size of array
  int size_of_array = a.size();
 
  // output array where we are going
  // to store our output
  vector<vector<int>> output;
 
  // It will be used for storing start
  // and end location in the same index
  int index = -1;
 
  for (int i = 0; i < size_of_array; i++)
  {
    for (int j = 0; j < a[0].size(); j++)
    {
      if (a[i][j] == 0)
      {
 
        // storing initial position
        // of rectangle
        output.push_back({i, j});
 
        // will be used for the
        // last position
        index = index + 1;
        findend(i, j, a, output, index);
      }
    }
  }
 
  cout << "[";
  int aa = 2, bb = 0;
 
  for(auto i:output)
  {
    bb = 3;
    cout << "[";
    for(int j:i)
    {
      if(bb)
        cout << j << ", ";
      else
        cout << j;
      bb--;
    }
    cout << "]";
    if(aa)
      cout << ", ";
    aa--;
 
  }
  cout << "]";
}
 
// Driver code
int main()
{
  vector<vector<int>> tests = {
    {1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 1, 1, 1, 1},
    {1, 1, 1, 0, 0, 0, 1},
    {1, 0, 1, 0, 0, 0, 1},
    {1, 0, 1, 1, 1, 1, 1},
    {1, 0, 1, 0, 0, 0, 0},
    {1, 1, 1, 0, 0, 0, 1},
    {1, 1, 1, 1, 1, 1, 1}
  };
 
  get_rectangle_coordinates(tests);
 
  return 0;
}
 
// This code is contributed by mohit kumar 29.

Time complexity: quadratic
Space complexity: quadratic