Banana-Leopard/TASTY huggingface dataset:
// C++ program to check whether a given tic tac toe
// board is valid or not
#include <iostream>
using namespace std;
 
// This matrix is used to find indexes to check all
// possible winning triplets in board[0..8]
int win[8][3] = {{0, 1, 2}, // Check first row.
                {3, 4, 5}, // Check second Row
                {6, 7, 8}, // Check third Row
                {0, 3, 6}, // Check first column
                {1, 4, 7}, // Check second Column
                {2, 5, 8}, // Check third Column
                {0, 4, 8}, // Check first Diagonal
                {2, 4, 6}}; // Check second Diagonal
 
// Returns true if character 'c' wins. c can be either
// 'X' or 'O'
bool isCWin(char *board, char c)
{
    // Check all possible winning combinations
    for (int i=0; i<8; i++)
        if (board[win[i][0]] == c &&
            board[win[i][1]] == c &&
            board[win[i][2]] == c )
            return true;
    return false;
}
 
// Returns true if given board is valid, else returns false
bool isValid(char board[9])
{
    // Count number of 'X' and 'O' in the given board
    int xCount=0, oCount=0;
    for (int i=0; i<9; i++)
    {
    if (board[i]=='X') xCount++;
    if (board[i]=='O') oCount++;
    }
 
    // Board can be valid only if either xCount and oCount
    // is same or count is one more than oCount
    if (xCount==oCount || xCount==oCount+1)
    {
        // Check if 'O' is winner
        if (isCWin(board, 'O'))
        {
            // Check if 'X' is also winner, then
            // return false
            if (isCWin(board, 'X'))
                return false;
 
            // Else return true xCount and yCount are same
            return (xCount == oCount);
        }
 
        // If 'X' wins, then count of X must be greater
        if (isCWin(board, 'X') && xCount != oCount + 1)
        return false;
 
        // If 'O' is not winner, then return true
        return true;
    }
    return false;
}
 
// Driver program
int main()
{
char board[] = {'X', 'X', 'O',
                'O', 'O', 'X',
                'X', 'O', 'X'};
(isValid(board))? cout << "Given board is valid":
                    cout << "Given board is not valid";
return 0;
}

Time complexity: constant
Space complexity: constant
// C++ program to compute submatrix query sum in O(1)
// time
#include<iostream>
using namespace std;
#define M 4
#define N 5
 
// Function to preprocess input mat[M][N].  This function
// mainly fills aux[M][N] such that aux[i][j] stores sum
// of elements from (0,0) to (i,j)
int preProcess(int mat[M][N], int aux[M][N])
{
   // Copy first row of mat[][] to aux[][]
   for (int i=0; i<N; i++)
      aux[0][i] = mat[0][i];
 
   // Do column wise sum
   for (int i=1; i<M; i++)
      for (int j=0; j<N; j++)
         aux[i][j] = mat[i][j] + aux[i-1][j];
 
   // Do row wise sum
   for (int i=0; i<M; i++)
      for (int j=1; j<N; j++)
         aux[i][j] += aux[i][j-1];
}
 
// A O(1) time function to compute sum of submatrix
// between (tli, tlj) and (rbi, rbj) using aux[][]
// which is built by the preprocess function
int sumQuery(int aux[M][N], int tli, int tlj, int rbi,
                                              int rbj)
{
    // result is now sum of elements between (0, 0) and
    // (rbi, rbj)
    int res = aux[rbi][rbj];
 
    // Remove elements between (0, 0) and (tli-1, rbj)
    if (tli > 0)
       res = res - aux[tli-1][rbj];
 
    // Remove elements between (0, 0) and (rbi, tlj-1)
    if (tlj > 0)
       res = res - aux[rbi][tlj-1];
 
    // Add aux[tli-1][tlj-1] as elements between (0, 0)
    // and (tli-1, tlj-1) are subtracted twice
    if (tli > 0 && tlj > 0)
       res = res + aux[tli-1][tlj-1];
 
    return res;
}
 
// Driver program
int main()
{
   int mat[M][N] = {{1, 2, 3, 4, 6},
                    {5, 3, 8, 1, 2},
                    {4, 6, 7, 5, 5},
                    {2, 4, 8, 9, 4} };
   int aux[M][N];
 
   preProcess(mat, aux);
 
   int tli = 2, tlj = 2, rbi = 3, rbj = 4;
   cout << "\nQuery1: " << sumQuery(aux, tli, tlj, rbi, rbj);
 
   tli = 0, tlj = 0, rbi = 1, rbj = 1;
   cout << "\nQuery2: " << sumQuery(aux, tli, tlj, rbi, rbj);
 
   tli = 1, tlj = 2, rbi = 3, rbj = 3;
   cout << "\nQuery3: " << sumQuery(aux, tli, tlj, rbi, rbj);
 
   return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// C++ program to find largest
// rectangle with all 1s
// in a binary matrix
#include <bits/stdc++.h>
using namespace std;
 
// Rows and columns in input matrix
#define R 4
#define C 4
 
// Finds the maximum area under
// the histogram represented
// by histogram.  See below article for details.
 
 
int maxHist(int row[])
{
    // Create an empty stack.
    // The stack holds indexes of
    // hist[] array/ The bars stored
    // in stack are always
    // in increasing order of their heights.
    stack<int> result;
 
    int top_val; // Top of stack
 
    int max_area = 0; // Initialize max area in current
    // row (or histogram)
 
    int area = 0; // Initialize area with current top
 
    // Run through all bars of given histogram (or row)
    int i = 0;
    while (i < C) {
        // If this bar is higher than the bar on top stack,
        // push it to stack
        if (result.empty() || row[result.top()] <= row[i])
            result.push(i++);
 
        else {
            // If this bar is lower than top of stack, then
            // calculate area of rectangle with stack top as
            // the smallest (or minimum height) bar. 'i' is
            // 'right index' for the top and element before
            // top in stack is 'left index'
            top_val = row[result.top()];
            result.pop();
            area = top_val * i;
 
            if (!result.empty())
                area = top_val * (i - result.top() - 1);
            max_area = max(area, max_area);
        }
    }
 
    // Now pop the remaining bars from stack and calculate
    // area with every popped bar as the smallest bar
    while (!result.empty()) {
        top_val = row[result.top()];
        result.pop();
        area = top_val * i;
        if (!result.empty())
            area = top_val * (i - result.top() - 1);
 
        max_area = max(area, max_area);
    }
    return max_area;
}
 
// Returns area of the largest rectangle with all 1s in
// A[][]
int maxRectangle(int A[][C])
{
    // Calculate area for first row and initialize it as
    // result
    int result = maxHist(A[0]);
 
    // iterate over row to find maximum rectangular area
    // considering each row as histogram
    for (int i = 1; i < R; i++) {
 
        for (int j = 0; j < C; j++)
 
            // if A[i][j] is 1 then add A[i -1][j]
            if (A[i][j])
                A[i][j] += A[i - 1][j];
 
        // Update result if area with current row (as last
        // row) of rectangle) is more
        result = max(result, maxHist(A[i]));
    }
 
    return result;
}
 
// Driver code
int main()
{
    int A[][C] = {
        { 0, 1, 1, 0 },
        { 1, 1, 1, 1 },
        { 1, 1, 1, 1 },
        { 1, 1, 0, 0 },
    };
 
    cout << "Area of maximum rectangle is "
         << maxRectangle(A);
 
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// C++ code for Maximum size square
// sub-matrix with all 1s
#include <bits/stdc++.h>
#define bool int
#define R 6
#define C 5
using namespace std;
 
void printMaxSubSquare(bool M[R][C])
{
    int i, j;
    int S[R][C];
    int max_of_s, max_i, max_j;
 
    /* Set first column of S[][]*/
    for (i = 0; i < R; i++)
        S[i][0] = M[i][0];
 
    /* Set first row of S[][]*/
    for (j = 0; j < C; j++)
        S[0][j] = M[0][j];
 
    /* Construct other entries of S[][]*/
    for (i = 1; i < R; i++) {
        for (j = 1; j < C; j++) {
            if (M[i][j] == 1)
                S[i][j]
                    = min({ S[i][j - 1], S[i - 1][j],
                            S[i - 1][j - 1] })
                      + 1; // better of using min in case of
                           // arguments more than 2
            else
                S[i][j] = 0;
        }
    }
 
    /* Find the maximum entry, and indexes of maximum entry
        in S[][] */
    max_of_s = S[0][0];
    max_i = 0;
    max_j = 0;
    for (i = 0; i < R; i++) {
        for (j = 0; j < C; j++) {
            if (max_of_s < S[i][j]) {
                max_of_s = S[i][j];
                max_i = i;
                max_j = j;
            }
        }
    }
 
    cout << "Maximum size sub-matrix is: \n";
    for (i = max_i; i > max_i - max_of_s; i--) {
        for (j = max_j; j > max_j - max_of_s; j--) {
            cout << M[i][j] << " ";
        }
        cout << "\n";
    }
}
 
/* Driver code */
int main()
{
    bool M[R][C] = { { 0, 1, 1, 0, 1 }, { 1, 1, 0, 1, 0 },
                     { 0, 1, 1, 1, 0 }, { 1, 1, 1, 1, 0 },
                     { 1, 1, 1, 1, 1 }, { 0, 0, 0, 0, 0 } };
 
    printMaxSubSquare(M);
}
 
// This code is contributed by rathbhupendra

Time complexity: quadratic
Space complexity: quadratic
// C++ code for Maximum size square
// sub-matrix with all 1s
// (space optimized solution)
#include <bits/stdc++.h>
 
using namespace std;
 
#define R 6
#define C 5
 
void printMaxSubSquare(bool M[R][C])
{
    int S[2][C], Max = 0;
 
    // set all elements of S to 0 first
    memset(S, 0, sizeof(S));
 
    // Construct the entries
    for (int i = 0; i < R; i++)
        for (int j = 0; j < C; j++) {
 
            // Compute the entrie at the current position
            int Entrie = M[i][j];
            if (Entrie)
                if (j)
                    Entrie
                        = 1
                          + min(S[1][j - 1],
                                min(S[0][j - 1], S[1][j]));
 
            // Save the last entrie and add the new one
            S[0][j] = S[1][j];
            S[1][j] = Entrie;
 
            // Keep track of the max square length
            Max = max(Max, Entrie);
        }
 
    // Print the square
    cout << "Maximum size sub-matrix is: \n";
    for (int i = 0; i < Max; i++, cout << '\n')
        for (int j = 0; j < Max; j++)
            cout << "1 ";
}
 
// Driver code
int main()
{
    bool M[R][C] = { { 0, 1, 1, 0, 1 }, { 1, 1, 0, 1, 0 },
                     { 0, 1, 1, 1, 0 }, { 1, 1, 1, 1, 0 },
                     { 1, 1, 1, 1, 1 }, { 0, 0, 0, 0, 0 } };
 
    printMaxSubSquare(M);
 
    return 0;
 
    // This code is contributed
    // by Gatea David
}

Time complexity: quadratic
Space complexity: linear
// An efficient C++ program to compute sum for given array of cell indexes
#include<bits/stdc++.h>
#define R 3
#define C 3
using namespace std;
 
// A structure to represent a cell index
struct Cell
{
    int r; // r is row, varies from 0 to R-1
    int c; // c is column, varies from 0 to C-1
};
 
void printSums(int mat[][C], struct Cell arr[], int n)
{
    int sum = 0;
    int row[R] = {};
    int col[C] = {};
 
    // Compute sum of all elements, sum of every row and sum every column
    for (int i=0; i<R; i++)
    {
      for (int j=0; j<C; j++)
       {
             sum += mat[i][j];
             col[j] += mat[i][j];
             row[i] += mat[i][j];
       }
    }
 
    // Compute the desired sum for all given cell indexes
    for (int i=0; i<n; i++)
    {
        int ro = arr[i].r, co = arr[i].c;
        cout << sum - row[ro] - col[co] + mat[ro][co] << endl;
    }
}
 
// Driver program to test above function
int main()
{
    int mat[][C] = {{1, 1, 2}, {3, 4, 6}, {5, 3, 2}};
    struct Cell arr[] = {{0, 0}, {1, 1}, {0, 1}};
    int n = sizeof(arr)/sizeof(arr[0]);
    printSums(mat, arr, n);
    return 0;
}

Time complexity: quadratic
Space complexity: linear
// A C++ program to count the number of rectangular
// islands where every island is separated by a line
#include<iostream>
using namespace std;
 
// Size of given matrix is M X N
#define M 6
#define N 3
 
// This function takes a matrix of 'X' and 'O'
// and returns the number of rectangular islands
// of 'X' where no two islands are row-wise or
// column-wise adjacent, the islands may be diagonally
// adjacent
int countIslands(int mat[][N])
{
    int count = 0; // Initialize result
 
    // Traverse the input matrix
    for (int i=0; i<M; i++)
    {
        for (int j=0; j<N; j++)
        {
            // If current cell is 'X', then check
            // whether this is top-leftmost of a
            // rectangle. If yes, then increment count
            if (mat[i][j] == 'X')
            {
                if ((i == 0 || mat[i-1][j] == 'O') &&
                    (j == 0 || mat[i][j-1] == 'O'))
                    count++;
            }
        }
    }
 
    return count;
}
 
// Driver program to test above function
int main()
{
    int mat[M][N] =  {{'O', 'O', 'O'},
                      {'X', 'X', 'O'},
                      {'X', 'X', 'O'},
                      {'O', 'O', 'X'},
                      {'O', 'O', 'X'},
                      {'X', 'X', 'O'}
                    };
    cout << "Number of rectangular islands is "
         << countIslands(mat);
    return 0;
}

Time complexity: quadratic
Space complexity: constant
// A C++ program to find a common element in all rows of a
// row wise sorted array
#include <bits/stdc++.h>
using namespace std;
 
// Specify number of rows and columns
#define M 4
#define N 5
 
// Returns common element in all rows of mat[M][N]. If there is no
// common element, then -1 is returned
int findCommon(int mat[M][N])
{
    // An array to store indexes of current last column
    int column[M];
    int min_row; // To store index of row whose current
    // last element is minimum
 
    // Initialize current last element of all rows
    int i;
    for (i = 0; i < M; i++)
        column[i] = N - 1;
 
    min_row = 0; // Initialize min_row as first row
 
    // Keep finding min_row in current last column, till either
    // all elements of last column become same or we hit first column.
    while (column[min_row] >= 0) {
        // Find minimum in current last column
        for (i = 0; i < M; i++) {
            if (mat[i][column[i]] < mat[min_row][column[min_row]])
                min_row = i;
        }
 
        // eq_count is count of elements equal to minimum in current last
        // column.
        int eq_count = 0;
 
        // Traverse current last column elements again to update it
        for (i = 0; i < M; i++) {
            // Decrease last column index of a row whose value is more
            // than minimum.
            if (mat[i][column[i]] > mat[min_row][column[min_row]]) {
                if (column[i] == 0)
                    return -1;
 
                column[i] -= 1; // Reduce last column index by 1
            }
            else
                eq_count++;
        }
 
        // If equal count becomes M, return the value
        if (eq_count == M)
            return mat[min_row][column[min_row]];
    }
    return -1;
}
 
// Driver Code
int main()
{
    int mat[M][N] = {
        { 1, 2, 3, 4, 5 },
        { 2, 4, 5, 8, 10 },
        { 3, 5, 7, 9, 11 },
        { 1, 3, 5, 7, 9 },
    };
    int result = findCommon(mat);
    if (result == -1)
        cout << "No common element";
    else
        cout << "Common element is " << result;
    return 0;
}
 
// This code is contributed
// by Akanksha Rai

Time complexity: quadratic
Space complexity: linear
// A C++ program to find  the largest subsquare
// surrounded by 'X' in a given matrix of 'O' and 'X'
#include <iostream>
using namespace std;
 
// Size of given matrix is N X N
#define N 6
 
// A utility function to find minimum of two numbers
int getMin(int x, int y) { return (x < y) ? x : y; }
 
// Returns size of maximum size subsquare matrix
// surrounded by 'X'
int findSubSquare(int mat[][N])
{
    int max = 0; // Initialize result
 
    // Initialize the left-top value in hor[][] and ver[][]
    int hor[N][N], ver[N][N];
    hor[0][0] = ver[0][0] = (mat[0][0] == 'X');
 
    // Fill values in hor[][] and ver[][]
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            if (mat[i][j] == 'O')
                ver[i][j] = hor[i][j] = 0;
            else
            {
                hor[i][j]
                    = (j == 0) ? 1 : hor[i][j - 1] + 1;
                ver[i][j]
                    = (i == 0) ? 1 : ver[i - 1][j] + 1;
            }
        }
    }
 
    // Start from the rightmost-bottommost corner element
    // and find the largest ssubsquare with the help of
    // hor[][] and ver[][]
    for (int i = N - 1; i >= 1; i--)
    {
        for (int j = N - 1; j >= 1; j--)
        {
            // Find smaller of values in hor[][] and ver[][]
            // A Square can only be made by taking smaller
            // value
            int small = getMin(hor[i][j], ver[i][j]);
 
            // At this point, we are sure that there is a
            // right vertical line and bottom horizontal
            // line of length at least 'small'.
 
            // We found a bigger square if following
            // conditions are met: 1)If side of square is
            // greater than max. 2)There is a left vertical
            // line of length >= 'small' 3)There is a top
            // horizontal line of length >= 'small'
            while (small > max)
            {
                if (ver[i][j - small + 1] >= small
                    && hor[i - small + 1][j] >= small)
                {
                    max = small;
                }
                small--;
            }
        }
    }
    return max;
}
 
// Driver code
int main()
{
    int mat[][N] = {
        { 'X', 'O', 'X', 'X', 'X', 'X' },
        { 'X', 'O', 'X', 'X', 'O', 'X' },
        { 'X', 'X', 'X', 'O', 'O', 'X' },
        { 'O', 'X', 'X', 'X', 'X', 'X' },
        { 'X', 'X', 'X', 'O', 'X', 'O' },
        { 'O', 'O', 'X', 'O', 'O', 'O' },
    };
   
    // Function call
    cout << findSubSquare(mat);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// A C++ program to find  the largest subsquare
// surrounded by 'X' in a given matrix of 'O' and 'X'
#include <bits/stdc++.h>
using namespace std;
 
// Size of given matrix is N X N
#define N 6
 
int maximumSubSquare(int arr[][N])
{
    pair<int, int> dp[51][51];
    int maxside[51][51];
 
    // Initialize maxside with 0
    memset(maxside, 0, sizeof(maxside));
 
    int x = 0, y = 0;
 
    // Fill the dp matrix horizontally.
    // for contiguous 'X' increment the value of x,
    // otherwise make it 0
    for (int i = 0; i < N; i++)
    {
        x = 0;
        for (int j = 0; j < N; j++)
        {
            if (arr[i][j] == 'X')
                x += 1;
            else
                x = 0;
            dp[i][j].first = x;
        }
    }
 
    // Fill the dp matrix vertically.
    // For contiguous 'X' increment the value of y,
    // otherwise make it 0
    for (int i = 0; i < N; i++)
    {
          y=0;
        for (int j = 0; j < N; j++)
        {
            if (arr[j][i] == 'X')
                y += 1;
            else
                y = 0;
            dp[j][i].second = y;
        }
    }
 
    // Now check , for every value of (i, j) if sub-square
    // is possible,
    // traverse back horizontally by value val, and check if
    // vertical contiguous
    // 'X'enfing at (i , j-val+1) is greater than equal to
    // val.
    // Similarly, check if traversing back vertically, the
    // horizontal contiguous
    // 'X'ending at (i-val+1, j) is greater than equal to
    // val.
    int maxval = 0, val = 0;
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            val = min(dp[i][j].first, dp[i][j].second);
            if (dp[i][j - val + 1].second >= val
                && dp[i - val + 1][j].first >= val)
                maxside[i][j] = val;
            else
                maxside[i][j] = 0;
 
            // store the final answer in maxval
            maxval = max(maxval, maxside[i][j]);
        }
    }
 
    // return the final answe.
    return maxval;
}
 
// Driver code
int main()
{
    int mat[][N] = {
        { 'X', 'O', 'X', 'X', 'X', 'X' },
        { 'X', 'O', 'X', 'X', 'O', 'X' },
        { 'X', 'X', 'X', 'O', 'O', 'X' },
        { 'O', 'X', 'X', 'X', 'X', 'X' },
        { 'X', 'X', 'X', 'O', 'X', 'O' },
        { 'O', 'O', 'X', 'O', 'O', 'O' },
    };
 
    // Function call
    cout << maximumSubSquare(mat);
    return 0;
}

Time complexity: quadratic
Space complexity: quadratic
// A C++ program to implement flood fill algorithm
#include<iostream>
using namespace std;
 
// Dimensions of paint screen
#define M 8
#define N 8
 
// A recursive function to replace previous color 'prevC' at  '(x, y)'
// and all surrounding pixels of (x, y) with new color 'newC' and
void floodFillUtil(int screen[][N], int x, int y, int prevC, int newC)
{
    // Base cases
    if (x < 0 || x >= M || y < 0 || y >= N)
        return;
    if (screen[x][y] != prevC)
        return;
    if (screen[x][y] == newC)
        return;
 
    // Replace the color at (x, y)
    screen[x][y] = newC;
 
    // Recur for north, east, south and west
    floodFillUtil(screen, x+1, y, prevC, newC);
    floodFillUtil(screen, x-1, y, prevC, newC);
    floodFillUtil(screen, x, y+1, prevC, newC);
    floodFillUtil(screen, x, y-1, prevC, newC);
}
 
// It mainly finds the previous color on (x, y) and
// calls floodFillUtil()
void floodFill(int screen[][N], int x, int y, int newC)
{
    int prevC = screen[x][y];
      if(prevC==newC) return;
    floodFillUtil(screen, x, y, prevC, newC);
}
 
// Driver code
int main()
{
    int screen[M][N] = {{1, 1, 1, 1, 1, 1, 1, 1},
                      {1, 1, 1, 1, 1, 1, 0, 0},
                      {1, 0, 0, 1, 1, 0, 1, 1},
                      {1, 2, 2, 2, 2, 0, 1, 0},
                      {1, 1, 1, 2, 2, 0, 1, 0},
                      {1, 1, 1, 2, 2, 2, 2, 0},
                      {1, 1, 1, 1, 1, 2, 1, 1},
                      {1, 1, 1, 1, 1, 2, 2, 1},
                     };
    int x = 4, y = 4, newC = 3;
    floodFill(screen, x, y, newC);
 
    cout << "Updated screen after call to floodFill: \n";
    for (int i=0; i<M; i++)
    {
        for (int j=0; j<N; j++)
           cout << screen[i][j] << " ";
        cout << endl;
    }
}
//Updated by Arun Pandey

Time complexity: quadratic
Space complexity: quadratic