code stringlengths 195 7.9k | space_complexity stringclasses 6
values | time_complexity stringclasses 7
values |
|---|---|---|
// C++ program to check if all rows of a matrix
// are rotations of each other
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
// Returns true if all rows of mat[0..n-1][0..n-1]
// are rotations of each other.
bool isPermutedMatrix( int mat[MAX][MAX], int n)
{
// Creating a string that contain... | linear | cubic |
// C++ implementation to sort the given matrix
#include <bits/stdc++.h>
using namespace std;
#define SIZE 10
// function to sort the given matrix
void sortMat(int mat[SIZE][SIZE], int n)
{
// temporary matrix of size n^2
int temp[n * n];
int k = 0;
// copy the elements of matrix one by one
// ... | quadratic | quadratic |
// C++ program to find median of a matrix
// sorted row wise
#include<bits/stdc++.h>
using namespace std;
const int MAX = 100;
// function to find median in the matrix
int binaryMedian(int m[][MAX], int r ,int c)
{
int min = INT_MAX, max = INT_MIN;
for (int i=0; i<r; i++)
{
// Finding the minimu... | constant | nlogn |
// C++ program to print Lower
// triangular and Upper triangular
// matrix of an array
#include<iostream>
using namespace std;
// Function to form
// lower triangular matrix
void lower(int matrix[3][3], int row, int col)
{
int i, j;
for (i = 0; i < row; i++)
{
for (j = 0; j < col; j++)
{... | constant | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
vector<int> spiralOrder(vector<vector<int> >& matrix)
{
int m = matrix.size(), n = matrix[0].size();
vector<int> ans;
if (m == 0)
return ans;
vector<vector<bool> > seen(m, vector<bool>(n, false));
int... | linear | linear |
// C++ Program to print a matrix spirally
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
void spiralPrint(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
... | constant | quadratic |
// C++. program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
// Function for printing matrix in spiral
// form i, j: Start index of matrix, row
// and column respectively m, n: End index
// of matrix row and column respectively
void print(int arr[R][C], int i, int j, ... | constant | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
bool isInBounds(int i, int j)
{
if (i < 0 || i >= R || j < 0 || j >= C)
return false;
return true;
}
// check if the position is blocked
bool isBlocked(int matrix[R][C], int i, int j)
{
... | constant | quadratic |
// C++ program to find unique element in matrix
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
// function that calculate unique element
int unique(int mat[R][C], int n, int m)
{
// declare map for hashing
unordered_map<int, int> mp;
for (int i = 0; i < n; i++)
for (int j ... | quadratic | quadratic |
// C++ program to shift k elements in a matrix.
#include <bits/stdc++.h>
using namespace std;
#define N 4
// Function to shift first k elements of
// each row of matrix.
void shiftMatrixByK(int mat[N][N], int k)
{
if (k > N) {
cout << "shifting is not possible" << endl;
return;
}
int... | constant | quadratic |
// CPP Program to swap diagonal of a matrix
#include <bits/stdc++.h>
using namespace std;
// size of square matrix
#define N 3
// Function to swap diagonal of matrix
void swapDiagonal(int matrix[][N]) {
for (int i = 0; i < N; i++)
swap(matrix[i][i], matrix[i][N - i - 1]);
}
// Driver Code
int main() {
int... | constant | quadratic |
// CPP program for finding max path in matrix
#include <bits/stdc++.h>
#define N 4
#define M 6
using namespace std;
// To calculate max path in matrix
int findMaxPath(int mat[][M])
{
for (int i = 1; i < N; i++) {
for (int j = 0; j < M; j++) {
// When all paths are possible
if (... | constant | quadratic |
// C++ code to move matrix elements
// in given direction with add
// element with same value
#include <bits/stdc++.h>
using namespace std;
#define MAX 50
// Function to shift the matrix
// in the given direction
void moveMatrix(char d[], int n,
int a[MAX][MAX])
{
// For right shift move.
... | linear | quadratic |
// C++ implementation to sort the rows
// of matrix in ascending order followed by
// sorting the columns in descending order
#include <bits/stdc++.h>
using namespace std;
#define MAX_SIZE 10
// function to sort each row of the matrix
// according to the order specified by
// ascending.
void sortByRow(int mat[][MAX... | constant | quadratic |
// C++ program to find sum of
// middle row and column in matrix
#include <iostream>
using namespace std;
const int MAX = 100;
void middlesum(int mat[][MAX], int n)
{
int row_sum = 0, col_sum = 0;
//loop for sum of row
for (int i = 0; i < n; i++)
row_sum += mat[n / 2][i];
cout <... | constant | linear |
// C++ program showing time difference
// in row major and column major access
#include <stdio.h>
#include <time.h>
// taking MAX 10000 so that time difference
// can be shown
#define MAX 10000
int arr[MAX][MAX] = { 0 };
void rowMajor()
{
int i, j;
// accessing element row wise
for (i = 0; i < MAX... | quadratic | quadratic |
// CPP program to rotate a matrix right by k times
#include <iostream>
// size of matrix
#define M 3
#define N 3
using namespace std;
// function to rotate matrix by k times
void rotateMatrix(int matrix[][M], int k) {
// temporary array of size M
int temp[M];
// within the size of matrix
k = k % M;
... | linear | quadratic |
// Program to check given matrix
// is idempotent matrix or not.
#include<bits/stdc++.h>
#define N 3
using namespace std;
// Function for matrix multiplication.
void multiply(int mat[][N], int res[][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
res[i][j] = 0;
... | quadratic | cubic |
// Program to implement involutory matrix.
#include <bits/stdc++.h>
#define N 3
using namespace std;
// Function for matrix multiplication.
void multiply(int mat[][N], int res[][N])
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
res[i][j] = 0;
for (int k = 0; k < N; ... | quadratic | cubic |
// C++ code to swap the element of first
// and last row and display the result
#include <iostream>
using namespace std;
#define n 4
void interchangeFirstLast(int m[][n])
{
int rows = n;
// swapping of element between first
// and last rows
for (int i = 0; i < n; i++) {
int t = m[0][i];
... | constant | linear |
/* C++ Program to print matrix in Zig-zag pattern*/
#include <iostream>
using namespace std;
#define C 3
// Utility function to print matrix
// in zig-zag form
void zigZagMatrix(int arr[][C], int n, int m)
{
int row = 0, col = 0;
// Boolean variable that will true if we
// need to increment 'row' value ... | constant | quadratic |
// C++ code to
// sort 2D matrix row-wise
#include<bits/stdc++.h>
using namespace std;
void sortRowWise(int m[][4],
int r, int c)
{
// loop for rows of matrix
for (int i = 0; i < r; i++)
{
// loop for column of matrix
for (int j = 0; j < c; j++)
{
// loop for comparison and sw... | constant | quadratic |
// C++ code to sort 2D
// matrix row-wise
#include <bits/stdc++.h>
using namespace std;
#define M 4
#define N 4
int sortRowWise(int m[M][N])
{
// One by one sort
// individual rows.
for (int i = 0; i < M; i++)
sort(m[i], m[i] + N);
// Printing the sorted matrix
for (int i = 0; i < M; i++)
{
for ... | constant | quadratic |
// C++ code to check Markov Matrix
#include <iostream>
using namespace std;
#define n 3
bool checkMarkov(double m[][n])
{
// outer loop to access rows
// and inner to access columns
for (int i = 0; i <n; i++) {
// Find sum of current row
double sum = 0;
for (int j = 0; j < n; j... | constant | quadratic |
// Program to check matrix is diagonal matrix or not.
#include <bits/stdc++.h>
#define N 4
using namespace std;
// Function to check matrix
// is diagonal matrix or not.
bool isDiagonalMatrix(int mat[N][N])
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
// condition to check oth... | constant | quadratic |
// Program to check matrix is scalar matrix or not.
#include <bits/stdc++.h>
#define N 4
using namespace std;
// Function to check matrix is scalar matrix or not.
bool isScalarMatrix(int mat[N][N])
{
// Check all elements except main diagonal are
// zero or not.
for (int i = 0; i < N; i++)
for (in... | constant | quadratic |
// C++ implementation to sort the matrix row-wise
// and column-wise
#include <bits/stdc++.h>
using namespace std;
#define MAX_SIZE 10
// function to sort each row of the matrix
void sortByRow(int mat[MAX_SIZE][MAX_SIZE], int n)
{
for (int i = 0; i < n; i++)
// sorting row number 'i'
sort(ma... | constant | quadratic |
// C++ Program to count islands in boolean 2D matrix
#include <bits/stdc++.h>
using namespace std;
#define ROW 5
#define COL 5
// A function to check if a given
// cell (row, col) can be included in DFS
int isSafe(int M[][COL], int row, int col,
bool visited[][COL])
{
// row number is in range, colum... | quadratic | quadratic |
// C++Program to count islands in boolean 2D matrix
#include <bits/stdc++.h>
using namespace std;
// A utility function to do DFS for a 2D
// boolean matrix. It only considers
// the 8 neighbours as adjacent vertices
void DFS(vector<vector<int> >& M, int i, int j, int ROW,
int COL)
{
// Base condition
... | quadratic | quadratic |
// C++ Program to print Magic square
// of Doubly even order
#include<iostream>
using namespace std;
// Function for calculating Magic square
void doublyEven( int n )
{
int arr[n][n], i, j;
// filling matrix with its count value
// starting from 1;
for ( i = 0; i < n; i++)
for ( j = 0; j < n... | quadratic | quadratic |
// C++ program to generate odd sized magic squares
#include <bits/stdc++.h>
using namespace std;
// A function to generate odd sized magic squares
void generateSquare(int n)
{
int magicSquare[n][n];
// set all slots as 0
memset(magicSquare, 0, sizeof(magicSquare));
// Initialize position for 1
... | quadratic | quadratic |
// C++ program to check whether a given
// matrix is magic matrix or not
#include <bits/stdc++.h>
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
using namespace std;
// Returns true if mat[][] is magic
// square, else returns false.
bool isMagicSquare(int mat[][3])
{
int n = my_sizeof(mat)/my_sizeo... | constant | quadratic |
// C++ program to check whether a given
// matrix is magic matrix or not
#include <bits/stdc++.h>
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
using namespace std;
// Returns true if mat[][] is magic
// square, else returns false.
bool isMagicSquare(int mat[][3])
{
int n = my_sizeof(mat)/my_sizeo... | constant | quadratic |
// C++ code to find the Kronecker Product of two
// matrices and stores it as matrix C
#include <bits/stdc++.h>
using namespace std;
// rowa and cola are no of rows and columns
// of matrix A
// rowb and colb are no of rows and columns
// of matrix B
const int cola = 2, rowa = 3, colb = 3, rowb = 2;
// Function to ... | np | np |
// C++ implementation to count sub-matrices having sum
// divisible by the value 'k'
#include <bits/stdc++.h>
using namespace std;
#define SIZE 10
// function to count all sub-arrays divisible by k
int subCount(int arr[], int n, int k)
{
// create auxiliary hash array to count frequency
// of remainders
... | linear | cubic |
// 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... | constant | quadratic |
// 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)
{
... | linear | quadratic |
// 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 (fr... | constant | 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 st... | quadratic | 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 ro... | constant | quadratic |
// 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... | constant | quadratic |
// 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]... | constant | quadratic |
// 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)
... | constant | quadratic |
// 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... | constant | quadratic |
// 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(i... | constant | linear |
// 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)
... | constant | quadratic |
// 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)
... | constant | quadratic |
// 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 m... | constant | quadratic |
// 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();
i... | linear | quadratic |
// 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 = ... | linear | quadratic |
// 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++)
... | constant | quadratic |
// A brute force approach based CPP program to
// find if there is a rectangle with 1 as corners.
#include <bits/stdc++.h>
using namespace std;
// Returns true if there is a rectangle with
// 1 as corners.
bool isRectangle(const vector<vector<int> >& m)
{
// finding row and column size
int rows = m.size();
... | constant | quadratic |
// An efficient approach based CPP program to
// find if there is a rectangle with 1 as
// corners.
#include <bits/stdc++.h>
using namespace std;
// Returns true if there is a rectangle with
// 1 as corners.
bool isRectangle(const vector<vector<int> >& matrix)
{
// finding row and column size
int rows = matri... | quadratic | quadratic |
// C++ implementation comes from:
// https://github.com/MichaelWehar/FourCornersProblem
// Written by Niteesh Kumar and Michael Wehar
// References:
// [1] F. Mráz, D. Prusa, and M. Wehar.
// Two-dimensional Pattern Matching against
// Basic Picture Languages. CIAA 2019.
// [2] D. Prusa and M. Wehar. Complexit... | quadratic | quadratic |
// 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;
//... | quadratic | quadratic |
// C++ Code implementation for above problem
#include <bits/stdc++.h>
using namespace std;
#define N 4
#define M 4
// QItem for current location and distance
// from source location
class QItem {
public:
int row;
int col;
int dist;
QItem(int x, int y, int w)
: row(x), col(y), dist(w)
{
... | quadratic | quadratic |
// CPP program to compute number of sets
// in a binary matrix.
#include <bits/stdc++.h>
using namespace std;
const int m = 3; // no of columns
const int n = 2; // no of rows
// function to calculate the number of
// non empty sets of cell
long long countSets(int a[n][m])
{
// stores the final answer
long... | constant | quadratic |
// C++ program to search an element in row-wise
// and column-wise sorted matrix
#include <bits/stdc++.h>
using namespace std;
/* Searches the element x in mat[][]. If the
element is found, then prints its position
and returns true, otherwise prints "not found"
and returns false */
int search(int mat[4][4], int n, ... | constant | quadratic |
// C++ program to search an element in row-wise
// and column-wise sorted matrix
#include <bits/stdc++.h>
using namespace std;
/* Searches the element x in mat[][]. If the
element is found, then prints its position
and returns true, otherwise prints "not found"
and returns false */
int search(int mat[4][4], int n, in... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
// Function to print alternating rectangles of 0 and X
void fill0X(int m, int n)
{
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator */
int i, k = 0, l = 0;
// St... | quadratic | quadratic |
// C++ program to print all elements
// of given matrix in diagonal order
#include <bits/stdc++.h>
using namespace std;
#define ROW 5
#define COL 4
// A utility function to find min
// of two integers
int minu(int a, int b)
{
return (a < b) ? a : b;
}
// A utility function to find min
// of three integers
int... | constant | quadratic |
#include <bits/stdc++.h>
#define R 5
#define C 4
using namespace std;
bool isValid(int i, int j)
{
if (i < 0 || i >= R
|| j >= C || j < 0)
return false;
return true;
}
void diagonalOrder(int arr[][C])
{
/* through this for loop we choose
each element of first column as
star... | constant | quadratic |
#include <bits/stdc++.h>
#define R 5
#define C 4
using namespace std;
void diagonalOrder(int arr[][C],
int n, int m)
{
// we will use a 2D vector to
// store the diagonals of our array
// the 2D vector will have (n+m-1)
// rows that is equal to the number of
// diagonals
vec... | linear | quadratic |
// C++ implementation to find the total energy
// required to rearrange the numbers
#include <bits/stdc++.h>
using namespace std;
#define SIZE 100
// function to find the total energy
// required to rearrange the numbers
int calculateEnergy(int mat[SIZE][SIZE], int n)
{
int i_des, j_des, q;
int tot_energy... | constant | quadratic |
// C++ program to count unique cells in
// a matrix
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
// Returns true if mat[i][j] is unique
bool isUnique(int mat[][MAX], int i, int j,
int n, int m)
{
// checking in row calculating sumrow
// will be moving colum... | constant | quadratic |
// Efficient C++ program to count unique
// cells in a binary matrix
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
int countUnique(int mat[][MAX], int n, int m)
{
int rowsum[n], colsum[m];
memset(colsum, 0, sizeof(colsum));
memset(rowsum, 0, sizeof(rowsum));
// Count number of... | linear | quadratic |
// CPP program for counting number of cell
// equals to given x
#include<bits/stdc++.h>
using namespace std;
// function to count factors as number of cell
int count (int n, int x)
{
int count=0;
// traverse and find the factors
for (int i=1; i<=n && i<=x ; i++)
{
// x%i == 0 means i is factor... | constant | linear |
// CPP code to check if a matrix is
// sparse.
#include <iostream>
using namespace std;
const int MAX = 100;
bool isSparse(int array[][MAX], int m, int n)
{
int counter = 0;
// Count number of zeros in the matrix
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j)
if (array[i][... | constant | quadratic |
// CPP program to find common elements in
// two diagonals.
#include <iostream>
#define MAX 100
using namespace std;
// Returns count of row wise same
// elements in two diagonals of
// mat[n][n]
int countCommon(int mat[][MAX], int n)
{
int res = 0;
for (int i=0;i<n;i++)
if (mat[i][i] == mat[i][n-i-1]... | constant | linear |
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
// Function to check the if sum of a row
// is same as corresponding column
bool areSumSame(int a[][MAX], int n, int m)
{
int sum1 = 0, sum2 = 0;
for (int i = 0; i < min(n, m); i++) {
sum1 = 0, sum2 = 0;
for (int j = 0; j < min... | constant | linear |
// CPP program to find row with maximum 1
// in row sorted binary matrix
#include<bits/stdc++.h>
#define N 4
using namespace std;
// function for finding row with maximum 1
void findMax (int arr[][N])
{
int row = 0, i, j;
for (i=0, j=N-1; i<N;i++)
{
// find left most position of 1 in a row
... | constant | linear |
// Simple c++ code for check a matrix is
// symmetric or not.
#include <iostream>
using namespace std;
const int MAX = 100;
// Fills transpose of mat[N][N] in tr[N][N]
void transpose(int mat[][MAX], int tr[][MAX], int N)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
tr[i][j] = ma... | quadratic | quadratic |
// Efficient c++ code for check a matrix is
// symmetric or not.
#include <iostream>
using namespace std;
const int MAX = 100;
// Returns true if mat[N][N] is symmetric, else false
bool isSymmetric(int mat[][MAX], int N)
{
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (mat[i][j]... | constant | quadratic |
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// function which tells all cells are visited or not
bool isAllCellTraversed(vector<vector<pair<int,int>>>grid, int n, int m)
{
bool visited[n][m];
int total = n*m;
// starting cell values
int startx = grid[0][0].f... | quadratic | linear |
// CPP program to find number of possible moves of knight
#include <bits/stdc++.h>
#define n 4
#define m 4
using namespace std;
// To calculate possible moves
int findPossibleMoves(int mat[n][m], int p, int q)
{
// All possible moves of a knight
int X[8] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int Y[8] = { 1, 2... | constant | constant |
// A simple C++ program to find sum of diagonals
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void printDiagonalSums(int mat[][MAX], int n)
{
int principal = 0, secondary = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
// Condition for principal d... | constant | quadratic |
// An efficient C++ program to find sum of diagonals
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void printDiagonalSums(int mat[][MAX], int n)
{
int principal = 0, secondary = 0;
for (int i = 0; i < n; i++) {
principal += mat[i][i];
secondary += mat[i][n - i - 1]; ... | constant | linear |
// C++ program to print boundary element of
// matrix.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
void printBoundary(int a[][MAX], int m, int n)
{
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0 || j == 0 || i == n - 1
|| j == n ... | constant | quadratic |
// C++ program to find sum of boundary elements
// of matrix.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
int getBoundarySum(int a[][MAX], int m, int n)
{
long long int sum = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == 0)
... | constant | quadratic |
// C++ program to print a matrix in spiral
// form.
#include <iostream>
using namespace std;
const int MAX = 100;
void printSpiral(int mat[][MAX], int r, int c)
{
int i, a = 0, b = 2;
int low_row = (0 > a) ? 0 : a;
int low_column = (0 > b) ? 0 : b - 1;
int high_row = ((a + 1) >= r) ? r - 1 : a +... | constant | quadratic |
// C++ program to print matrix in snake order
#include <iostream>
#define M 4
#define N 4
using namespace std;
void print(int mat[M][N])
{
// Traverse through all rows
for (int i = 0; i < M; i++) {
// If current row is even, print from
// left to right
if (i % 2 == 0) {
f... | constant | quadratic |
// C++ program to find the difference
// between the sum of diagonal.
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
int difference(int arr[][MAX], int n)
{
// Initialize sums of diagonals
int d1 = 0, d2 = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
... | constant | quadratic |
// C++ program to find the difference
// between the sum of diagonal.
#include <bits/stdc++.h>
#define MAX 100
using namespace std;
int difference(int arr[][MAX], int n)
{
// Initialize sums of diagonals
int d1 = 0, d2 = 0;
for (int i = 0; i < n; i++)
{
// d1 store the sum of diagonal from... | constant | linear |
// C++ program to construct ancestor matrix for
// given tree.
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
/* A binary tree node */
struct Node
{
int data;
Node *left, *right;
};
// Creating a global boolean matrix for simplicity
bool mat[MAX][MAX];
// anc[] stores all ancestors of curren... | quadratic | quadratic |
// C++ program to construct ancestor matrix for
// given tree.
#include<bits/stdc++.h>
using namespace std;
#define size 6
int M[size][size]={0};
/* A binary tree node */
struct Node
{
int data;
Node *left, *right;
};
/* Helper function to create a new node */
Node* newnode(int data)
{
Node* node = ne... | quadratic | quadratic |
// Given an ancestor matrix for binary tree, construct
// the tree.
#include <bits/stdc++.h>
using namespace std;
# define N 6
/* A binary tree node */
struct Node
{
int data;
Node *left, *right;
};
/* Helper function to create a new node */
Node* newNode(int data)
{
Node* node = new Node;
n... | quadratic | quadratic |
// C++ program to fill a matrix with values from
// 1 to n*n in spiral fashion.
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
// Fills a[m][n] with values from 1 to m*n in
// spiral fashion.
void spiralFill(int m, int n, int a[][MAX])
{
// Initialize value to be filled in matrix
int val... | quadratic | quadratic |
/* C++ program for Sudoku generator */
#include <bits/stdc++.h>
using namespace std;
class Sudoku {
public:
int** mat;
int N;
// number of columns/rows.
int SRN;
// square root of N
int K;
// No. Of missing digits
// Constructor
Sudoku(int N, int K)
{
this->N... | quadratic | quadratic |
#include <iostream>
using namespace std;
// change row and column value to set the canvas size
const int row = 5;
const int col = 4;
// creates row boundary
int row_line()
{
cout << endl;
for (int i = 0; i < col; i++) {
cout << " -----";
}
cout << endl;
}
// returns the count of alive neig... | quadratic | quadratic |
// C++ program to find maximum sum of hour
// glass in matrix
#include<bits/stdc++.h>
using namespace std;
const int R = 5;
const int C = 5;
// Returns maximum sum of hour glass in ar[][]
int findMaxSum(int mat[R][C])
{
if (R<3 || C<3){
cout << "Not possible" << endl;
exit(0);
}
// He... | constant | quadratic |
// C++ program for finding maximum and minimum in
// a matrix.
#include<bits/stdc++.h>
using namespace std;
#define MAX 100
// Finds maximum and minimum in arr[0..n-1][0..n-1]
// using pair wise comparisons
void maxMin(int arr[][MAX], int n)
{
int min = INT_MAX;
int max = INT_MIN;
// Traverses rows on... | constant | quadratic |
// C++ program to print matrix in anti-spiral form
#include <bits/stdc++.h>
using namespace std;
#define R 4
#define C 5
void antiSpiralTraversal(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending col... | quadratic | quadratic |
// C++ program to find trace and normal
// of given matrix
#include<bits/stdc++.h>
using namespace std;
// Size of given matrix
const int MAX = 100;
// Returns Normal of a matrix of size n x n
int findNormal(int mat[][MAX], int n)
{
int sum = 0;
for (int i=0; i<n; i++)
for (int j=0; j<n; j++)
... | constant | quadratic |
// C++ program to find minimum operations required
// to set all the element of binary matrix
#include <bits/stdc++.h>
#define N 5
#define M 5
using namespace std;
// Return minimum operation required to make all 1s.
int minOperation(bool arr[N][M])
{
int ans = 0;
for (int i = N - 1; i >= 0; i--)
{
... | quadratic | quadratic |
// This is a modified code of
// https://www.geeksforgeeks.org/print-a-given-matrix-in-spiral-form/
#include <iostream>
#define R 3
#define C 6
using namespace std;
// Function that print matrix in reverse spiral form.
void ReversespiralPrint(int m, int n, int a[R][C])
{
// Large array to initialize it
// wit... | constant | quadratic |
// C++ program to find sum of matrix element
// where each element is integer division of
// row and column.
#include<bits/stdc++.h>
using namespace std;
// Return sum of matrix element where each element
// is division of its corresponding row and column.
int findSum(int n)
{
int ans = 0;
for (int i = 1; i <... | constant | quadratic |
// C++ program to find sum of matrix element
// where each element is integer division of
// row and column.
#include<bits/stdc++.h>
using namespace std;
// Return sum of matrix element where each
// element is division of its corresponding
// row and column.
int findSum(int n)
{
int ans = 0, temp = 0, num;
... | constant | quadratic |
// C++ program to find
// number of countOpsation
// to make two matrix equals
#include <bits/stdc++.h>
using namespace std;
const int MAX = 1000;
int countOps(int A[][MAX], int B[][MAX],
int m, int n)
{
// Update matrix A[][]
// so that only A[][]
// has to be countOpsed
for (int i = 0... | constant | quadratic |
// C++ program to print 2 coils of a
// 4n x 4n matrix.
#include<iostream>
using namespace std;
// Print coils in a matrix of size 4n x 4n
void printCoils(int n)
{
// Number of elements in each coil
int m = 8*n*n;
// Let us fill elements in coil 1.
int coil1[m];
// First element of coil1
/... | quadratic | quadratic |
// C++ program to find sum of matrix in which each
// element is absolute difference of its corresponding
// row and column number row.
#include<bits/stdc++.h>
using namespace std;
// Return the sum of matrix in which each element
// is absolute difference of its corresponding row
// and column number row
int findSum... | quadratic | quadratic |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.