code
stringlengths
195
7.9k
space_complexity
stringclasses
6 values
time_complexity
stringclasses
7 values
// 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...
constant
linear
// 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...
constant
constant
// C++ program to find if a matrix is symmetric. #include <bits/stdc++.h> #define MAX 1000 using namespace std; void checkHV(int arr[][MAX], int N, int M) { // Initializing as both horizontal and vertical // symmetric. bool horizontal = true, vertical = true; // Checking for Horizontal Symmetry. We...
constant
quadratic
// C++ program to find maximum possible determinant // of 0/n matrix. #include <bits/stdc++.h> using namespace std; // Function for maximum determinant int maxDet(int n) { return (2*n*n*n); } // Function to print resultant matrix void resMatrix ( int n) { for (int i = 0; i < 3; i++) { for (int ...
constant
constant
// C++ program to find sum of // diagonals of spiral matrix #include<bits/stdc++.h> using namespace std; // function returns sum of diagonals int spiralDiaSum(int n) { if (n == 1) return 1; // as order should be only odd // we should pass only odd-integers return (4*n*n - 6*n + 6 + spiralDia...
linear
linear
// C++ program to find all permutations of a given row #include<bits/stdc++.h> #define MAX 100 using namespace std; // Function to find all permuted rows of a given row r void permutatedRows(int mat[][MAX], int m, int n, int r) { // Creating an empty set unordered_set<int> s; // Count frequencies of e...
linear
quadratic
// C++ program to find perimeter of area covered by // 1 in 2D matrix consists of 0's and 1's. #include<bits/stdc++.h> using namespace std; #define R 3 #define C 5 // Find the number of covered side for mat[i][j]. int numofneighbour(int mat[][C], int i, int j) { int count = 0; // UP if (i > 0 && mat[i ...
constant
quadratic
// C++ program to print cells with same rectangular // sum diagonally #include <bits/stdc++.h> using namespace std; #define R 4 #define C 4 // Method prints cell index at which rectangular sum is // same at prime diagonal and other diagonal void printCellWithSameRectangularArea(int mat[R][C], ...
quadratic
quadratic
// C++ program to print matrix in diagonal order #include <bits/stdc++.h> using namespace std; const int MAX = 100; void printMatrixDiagonal(int mat[MAX][MAX], int n) { // Initialize indexes of element to be printed next int i = 0, j = 0; // Direction is initially from down to up bool isUp = true; ...
constant
quadratic
// C++ program to print matrix in diagonal order #include <bits/stdc++.h> using namespace std; int main() { // Initialize matrix int mat[][4] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; // n - size // mode ...
constant
quadratic
// C++ program to find maximum difference of sum of // elements of two rows #include<bits/stdc++.h> #define MAX 100 using namespace std; // Function to find maximum difference of sum of // elements of two rows such that second row appears // before first row. int maxRowDiff(int mat[][MAX], int m, int n) { // auxi...
linear
quadratic
// C++ program to find a pair with given sum such that // every element of pair is in different rows. #include<bits/stdc++.h> using namespace std; const int MAX = 100; // Function to find pair for given sum in matrix // mat[][] --> given matrix // n --> order of matrix // sum --> given sum for which we need to find...
constant
cubic
// C++ program to get total coverage of all zeros in // a binary matrix #include <bits/stdc++.h> using namespace std; #define R 4 #define C 4 // Returns total coverage of all zeros in mat[][] int getTotalCoverageOfMatrix(int mat[R][C]) { int res = 0; // looping for all rows of matrix for (int i = 0; i...
constant
quadratic
// C++ program to find number of sorted rows #include <bits/stdc++.h> #define MAX 100 using namespace std; // Function to count all sorted rows in a matrix int sortedCount(int mat[][MAX], int r, int c) { int result = 0; // Initialize result // Start from left side of matrix to // count increasing order ...
constant
quadratic
// C++ program to Find maximum XOR value in // matrix either row / column wise #include<iostream> using namespace std; // maximum number of row and column const int MAX = 1000; // function return the maximum xor value that is // either row or column wise int maxXOR(int mat[][MAX], int N) { // for row xor and co...
constant
quadratic
// C++ program to find how many mirror can transfer // light from bottom to right #include <bits/stdc++.h> using namespace std; // method returns number of mirror which can transfer // light from bottom to right int maximumMirrorInMatrix(string mat[], int N) { // To store first obstacles horizontally (from right)...
linear
quadratic
// C++ program to tell the Current direction in // R x C grid #include <iostream> using namespace std; typedef long long int ll; // Function which tells the Current direction void direction(ll R, ll C) { if (R != C && R % 2 == 0 && C % 2 != 0 && R < C) { cout << "Left" << endl; return; } i...
constant
constant
// C++ program to check whether given matrix // is a Toeplitz matrix or not #include <iostream> using namespace std; #define N 5 #define M 4 // Function to check if all elements present in // descending diagonal starting from position // (i, j) in the matrix are all same or not bool checkDiagonal(int mat[N][M], int i...
constant
quadratic
// C++ program to check whether given // matrix is a Toeplitz matrix or not #include <bits/stdc++.h> using namespace std; bool isToeplitz(vector<vector<int>> matrix) { // row = number of rows // col = number of columns int row = matrix.size(); int col = matrix[0].size(); // HashMap to ...
linear
quadratic
// C++ Program to return previous element in an expanding // matrix. #include <bits/stdc++.h> using namespace std; // Returns left of str in an expanding matrix of // a, b, c, and d. string findLeft(string str) { int n = str.length(); // Start from rightmost position while (n--) { // If the ...
constant
linear
// C++ program to print a n x n spiral matrix // in clockwise direction using O(1) space #include <bits/stdc++.h> using namespace std; // Prints spiral matrix of size n x n containing // numbers from 1 to n x n void printSpiral(int n) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) ...
constant
quadratic
#include <iostream> #include <vector> #include <climits> #include <cstring> using namespace std; // Check if it is possible to go to (x, y) from the current position. The // function returns false if the cell has value 0 or already visited bool isSafe(vector<vector<int>> &mat, vector<vector<bool>> &visited, int x, i...
quadratic
np
// C++ program to find the shortest path between // a given source cell to a destination cell. #include <bits/stdc++.h> using namespace std; #define ROW 9 #define COL 10 //To store matrix cell coordinates struct Point { int x; int y; }; // A Data Structure for queue used in BFS struct queueNode { Point ...
quadratic
quadratic
// C++ program for finding orientation of the pattern // using KMP pattern searching algorithm #include<bits/stdc++.h> using namespace std; #define N 5 // Used in KMP Search for preprocessing the pattern void computeLPSArray(char *pat, int M, int *lps) { // length of the previous longest prefix suffix int len...
linear
quadratic
// A Naive method to find maximum value of mat[d][e] // - ma[a][b] such that d > a and e > b #include <bits/stdc++.h> using namespace std; #define N 5 // The function returns maximum value A(d,e) - A(a,b) // over all choices of indexes such that both d > a // and e > b. int findMaxValue(int mat[][N]) { // stores ...
constant
np
// An efficient method to find maximum value of mat[d] // - ma[a][b] such that c > a and d > b #include <bits/stdc++.h> using namespace std; #define N 5 // The function returns maximum value A(c,d) - A(a,b) // over all choices of indexes such that both c > a // and d > b. int findMaxValue(int mat[][N]) { //stores...
quadratic
quadratic
// An efficient C++ program to find maximum sum sub-square // matrix #include <bits/stdc++.h> using namespace std; // Size of given matrix #define N 5 // A O(n^2) function to the maximum sum sub-squares of size // k x k in a given square matrix of size n x n void printMaxSumSub(int mat[][N], int k) { // k must ...
quadratic
quadratic
// A Program to prints common element in all // rows of matrix #include <bits/stdc++.h> using namespace std; // Specify number of rows and columns #define M 4 #define N 5 // prints common element in all rows of matrix void printCommonElements(int mat[M][N]) { unordered_map<int, int> mp; // initialize 1st ...
linear
quadratic
// C++ Code For A Boolean Matrix Question #include <bits/stdc++.h> using namespace std; #define R 3 #define C 4 void modifyMatrix(bool mat[R][C]) { bool row[R]; bool col[C]; int i, j; /* Initialize all values of row[] as 0 */ for (i = 0; i < R; i++) row[i] = 0; /* Initialize al...
linear
quadratic
#include <bits/stdc++.h> using namespace std; #define R 3 #define C 4 void modifyMatrix(int mat[R][C]) { // variables to check if there are any 1 // in first row and column bool row_flag = false; bool col_flag = false; // updating the first row and col if 1 // is encountered for (int i =...
constant
quadratic
// C++ program to find i such that all entries in i'th row are 0 // and all entries in i't column are 1 #include <iostream> using namespace std; #define n 5 int find(bool arr[n][n]) { // Start from top-most rightmost corner // (We could start from other corners also) int i=0, j=n-1; // Initialize re...
constant
linear
// 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 ...
constant
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...
quadratic
quadratic
// C++ program to find rank of a matrix #include <bits/stdc++.h> using namespace std; #define R 3 #define C 3 /* function for exchanging two rows of a matrix */ void swap(int mat[R][C], int row1, int row2, int col) { for (int i = 0; i < col; i++) { int temp = mat[row1][i]; mat[row...
constant
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 ro...
linear
quadratic
// 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++) ...
quadratic
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)); // Constru...
linear
quadratic
// 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 ma...
linear
quadratic
// 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...
constant
quadratic
// 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...
linear
quadratic
// 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 m...
quadratic
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 wi...
quadratic
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]...
quadratic
quadratic
// C++ program for above approach #include <bits/stdc++.h> using namespace std; // Function to check valid coordinate int validCoord(int x, int y, int n, int m) { if (x < 0 || y < 0) { return 0; } if (x >= n || y >= m) { return 0; } return 1; } // Function to run bfs void bfs(i...
quadratic
quadratic
#include <bits/stdc++.h> using namespace std; // Function to print all elements of matrix in sorted orderd void sortedMatrix(int N, vector<vector<int> > Mat) { vector<int> temp; // Store all elements of matrix into temp for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { tem...
quadratic
quadratic
// C++ program to merge k sorted arrays of size n each. #include<iostream> #include<climits> using namespace std; #define N 4 // A min heap node struct MinHeapNode { int element; // The element to be stored int i; // index of the row from which the element is taken int j; // index of the next element to...
linear
quadratic
// A simple C++ program to find sum of all subsquares of // size k x k #include <iostream> using namespace std; // Size of given matrix #define n 5 // A simple function to find sum of all sub-squares of size // k x k in a given square matrix of size n x n void printSumSimple(int mat[][n], int k) { // k must be ...
constant
quadratic
// An efficient C++ program to find sum of all subsquares of // size k x k #include <iostream> using namespace std; // Size of given matrix #define n 5 // A O(n^2) function to find sum of all sub-squares of size // k x k in a given square matrix of size n x n void printSumTricky(int mat[][n], int k) { // k must...
quadratic
quadratic
// C++ program to find // transpose of a matrix #include <bits/stdc++.h> using namespace std; #define N 4 // This function stores transpose // of A[][] in B[][] void transpose(int A[][N], int B[][N]) { int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) B[i][j] = A[j][i]; } // Driv...
quadratic
quadratic
#include <bits/stdc++.h> using namespace std; #define N 4 // Converts A[][] to its transpose void transpose(int A[][N]) { for (int i = 0; i < N; i++) for (int j = i + 1; j < N; j++) swap(A[i][j], A[j][i]); } int main() { int A[N][N] = { { 1, 1, 1, 1 }, { 2, 2, 2, 2 ...
constant
quadratic
// C++ program for addition // of two matrices #include <bits/stdc++.h> using namespace std; #define N 4 // This function adds A[][] and B[][], and stores // the result in C[][] void add(int A[][N], int B[][N], int C[][N]) { int i, j; for (i = 0; i < N; i++) for (j = 0; j < N; j++) C[i][j]...
quadratic
quadratic
// A Memoization based program to find maximum collection // using two traversals of a grid #include<bits/stdc++.h> using namespace std; #define R 5 #define C 4 // checks whether a given input is valid or not bool isValid(int x, int y1, int y2) { return (x >= 0 && x < R && y1 >=0 && y1 < C && y2 >=0 &...
quadratic
cubic
// A Naive Recursive C++ program // to count paths with exactly // 'k' coins #include <bits/stdc++.h> #define R 3 #define C 3 using namespace std; // Recursive function to count paths with sum k from // (0, 0) to (m, n) int pathCountRec(int mat[][C], int m, int n, int k) { // Base cases if (m < 0 || n <...
quadratic
linear
// A Dynamic Programming based C++ program to count paths with // exactly 'k' coins #include <bits/stdc++.h> #define R 3 #define C 3 #define MAX_K 1000 using namespace std; int dp[R][C][MAX_K]; int pathCountDPRecDP(int mat[][C], int m, int n, int k) { // Base cases if (m < 0 || n < 0 || k < 0) return 0; ...
quadratic
quadratic
// C++ program to find minimum initial points to reach // destination #include <bits/stdc++.h> #define R 3 #define C 3 using namespace std; int minInitialPoints(int points[][C]) { // dp[i][j] represents the minimum initial points player // should have so that when starts with cell(i, j) // successfully ...
quadratic
quadratic
// Program to find maximum sum subarray // in a given 2D array #include <bits/stdc++.h> using namespace std; #define ROW 4 #define COL 5 // Implementation of Kadane's algorithm for // 1D array. The function returns the maximum // sum and stores starting and ending indexes // of the maximum sum subarray at addresses...
linear
cubic
/* Program to implement a stack using two queue */ #include <bits/stdc++.h> using namespace std; class Stack { // Two inbuilt queues queue<int> q1, q2; public: void push(int x) { // Push x first in empty q2 q2.push(x); // Push all the remaining // elements in q1 t...
linear
linear
// Program to implement a stack // using two queue #include <bits/stdc++.h> using namespace std; class Stack { queue<int> q1, q2; public: void pop() { if (q1.empty()) return; // Leave one element in q1 and // push others in q2. while (q1.size() != 1) { ...
linear
linear
#include <bits/stdc++.h> using namespace std; // Stack Class that acts as a queue class Stack { queue<int> q; public: void push(int data); void pop(); int top(); int size(); bool empty(); }; // Push operation void Stack::push(int data) { // Get previous size of queue int s = q.s...
linear
linear
#include <bits/stdc++.h> using namespace std; struct QNode { int data; QNode* next; QNode(int d) { data = d; next = NULL; } }; struct Queue { QNode *front, *rear; Queue() { front = rear = NULL; } void enQueue(int x) { // Create a new LL node QN...
constant
constant
// C++ implementation of De-queue using circular // array #include <iostream> using namespace std; // Maximum size of array or Dequeue #define MAX 100 // A structure to represent a Deque class Deque { int arr[MAX]; int front; int rear; int size; public: Deque(int size) { front = -1...
linear
linear
// C++ Program to implement stack and queue using Deque #include <bits/stdc++.h> using namespace std; // structure for a node of deque struct DQueNode { int value; DQueNode* next; DQueNode* prev; }; // Implementation of deque class class Deque { private: // pointers to head and tail of deque D...
linear
linear
// Program to print BFS traversal from a given // source vertex. BFS(int s) traverses vertices // reachable from s. #include<bits/stdc++.h> using namespace std; // This class represents a directed graph using // adjacency list representation class Graph { int V; // No. of vertices // Pointer to an array ...
linear
linear
// Recursive CPP program for level // order traversal of Binary Tree #include <bits/stdc++.h> using namespace std; /* A binary tree node has data, pointer to left child and a pointer to right child */ class node { public: int data; node *left, *right; }; /* Function prototypes */ void printCurrentLevel(node...
linear
quadratic
/* C++ program to print level order traversal using STL */ #include <bits/stdc++.h> using namespace std; // A Binary Tree Node struct Node { int data; struct Node *left, *right; }; // Iterative method to find height of Binary Tree void printLevelOrder(Node* root) { // Base Case if (root == NULL)...
linear
linear
// C++ program to create a Complete Binary tree from its Linked List // Representation #include <iostream> #include <string> #include <queue> using namespace std; // Linked list node struct ListNode { int data; ListNode* next; }; // Binary tree node structure struct BinaryTreeNode { int data; Binary...
np
linear
// C++ program to check if a given binary tree is complete // or not #include <bits/stdc++.h> using namespace std; // A binary tree node has data, pointer to left child and a // pointer to right child struct node { int data; struct node* left; struct node* right; }; // Given a binary tree, return true...
linear
linear
// C++ implementation of a O(n) time method for // Zigzag order traversal #include <iostream> #include <stack> using namespace std; // Binary Tree node struct Node { int data; struct Node *left, *right; }; // function to print the zigzag traversal void zizagtraversal(struct Node* root) { // if null then...
linear
linear
// C++ implementation of a O(n) time method for // Zigzag order traversal #include <bits/stdc++.h> #include <iostream> using namespace std; // Binary Tree node class Node { public: int data; Node *left, *right; }; // Function to print the zigzag traversal vector<int> zigZagTraversal(Node* root) { deque<...
linear
linear
// C++ implementation of a O(n) time method for // Zigzag order traversal #include <bits/stdc++.h> #include <iostream> using namespace std; // Binary Tree node class Node { public: int data; Node *left, *right; }; // Function to print the zigzag traversal vector<int> zigZagTraversal(Node* root) { if(roo...
linear
linear
// C++ program to demonstrate // working of LIFO // using stack in C++ #include<bits/stdc++.h> using namespace std; // Pushing element on the top of the stack stack<int> stack_push(stack<int> stack) { for (int i = 0; i < 5; i++) { stack.push(i); } return stack; } // Popping element from the ...
linear
linear
// CPP program to reverse a Queue #include <bits/stdc++.h> using namespace std; // Utility function to print the queue void Print(queue<int>& Queue) { while (!Queue.empty()) { cout << Queue.front() << " "; Queue.pop(); } } // Function to reverse the queue void reverseQueue(queue<int>& Queue)...
linear
linear
// CPP program to reverse a Queue #include <bits/stdc++.h> using namespace std; // Utility function to print the queue void Print(queue<int>& Queue) { while (!Queue.empty()) { cout << Queue.front() << " "; Queue.pop(); } } // Function to reverse the queue void reverseQueue(queue<int>& q) { ...
linear
linear
// C++ code for reversing a queue #include <bits/stdc++.h> using namespace std; // Utility function to print the queue void printQueue(queue<long long int> Queue) { while (!Queue.empty()) { cout << Queue.front() << " "; Queue.pop(); } } // Recursive function to reverse the queue void reverse...
linear
linear
// C++ program to reverse first // k elements of a queue. #include <bits/stdc++.h> using namespace std; /* Function to reverse the first K elements of the Queue */ void reverseQueueFirstKElements(int k, queue<int>& Queue) { if (Queue.empty() == true || k > Queue.size()) return; if (k <= 0) ...
linear
linear
// C++ program to interleave the first half of the queue // with the second half #include <bits/stdc++.h> using namespace std; // Function to interleave the queue void interLeaveQueue(queue<int>& q) { // To check the even number of elements if (q.size() % 2 != 0) cout << "Input even number of integers...
linear
linear
// C++ program for recursive level // order traversal in spiral form #include <bits/stdc++.h> using namespace std; // A binary tree node has data, // pointer to left child and a // pointer to right child struct node { int data; struct node* left; struct node* right; }; // Function prototypes void printG...
linear
quadratic
// C++ implementation of a O(n) time method for spiral order // traversal #include <iostream> #include <stack> using namespace std; // Binary Tree node struct node { int data; struct node *left, *right; }; void printSpiral(struct node* root) { if (root == NULL) return; // NULL check // Cre...
linear
linear
// C++ implementation of above approach #include <iostream> #include <stack> using namespace std; // Binary Tree node struct Node { int key; struct Node *left, *right; }; void spiralPrint(struct Node* root) { // Declare a deque deque<Node*> dq; // Insert the root of the tree into the deque ...
linear
linear
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Method to find the maximum for each // and every contiguous subarray of size K. void printKMax(int arr[], int N, int K) { int j, max; for (int i = 0; i <= N - K; i++) { max = arr[i]; for (j = 1; j < K;...
constant
quadratic
// CPP program for the above approach #include <bits/stdc++.h> using namespace std; // A Dequeue (Double ended queue) based // method for printing maximum element of // all subarrays of size k void printKMax(int arr[], int N, int K) { // Create a Double Ended Queue, // Qi that will store indexes // of a...
linear
linear
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; struct node { int data; int maximum; }; // It is a modification in the way of implementation of // queue using two stack void insert(stack<node>& s2, int val) { // inserting the element in s2 node other; oth...
linear
linear
// C++ program to find circular tour for a truck #include <bits/stdc++.h> using namespace std; // A petrol pump has petrol and distance to next petrol pump class petrolPump { public: int petrol; int distance; }; // The function returns starting point if there is a possible solution, // otherwise returns...
constant
linear
// C++ program to find circular tour for a truck #include <bits/stdc++.h> using namespace std; // A petrol pump has petrol and distance to next petrol pump class petrolPump { public: int petrol; int distance; }; // The function returns starting point if there is a // possible solution, otherwise returns -1 ...
constant
linear
// C++ program to find circular tour for a truck #include <bits/stdc++.h> using namespace std; // A petrol pump has petrol and distance to next petrol pump class petrolPump { public: int petrol; int distance; }; // The function returns starting point if there is a // possible solution, otherwise returns -1 ...
constant
linear
// CPP program to find smallest multiple of a // given number made of digits 0 and 9 only #include <bits/stdc++.h> using namespace std; // Maximum number of numbers made of 0 and 9 #define MAX_COUNT 10000 // vector to store all numbers that can be formed // using digits 0 and 9 and are less than 10^5 vector<string>...
np
linear
#include <iostream> #include <queue> using namespace std; // This approach counts the number of nodes from root to the // leaf to calculate the height of the tree. // Defining the structure of a Node. class Node { public: int data; Node* left; Node* right; }; // Helper function to create a newnode...
linear
linear
// C++ program to generate binary numbers from 1 to n #include <bits/stdc++.h> using namespace std; // This function uses queue data structure to print binary // numbers void generatePrintBinary(int n) { // Create an empty queue of strings queue<string> q; // Enqueue the first binary number q.push("...
linear
linear
// C++ program to find minimum time required to make all // oranges rotten #include <bits/stdc++.h> #define R 3 #define C 5 using namespace std; // function to check whether a cell is valid / invalid bool isvalid(int i, int j) { return (i >= 0 && j >= 0 && i < R && j < C); } // structure for storing coordinates...
quadratic
quadratic
// C++ program to find sum of all minimum and maximum // elements Of Sub-array Size k. #include<bits/stdc++.h> using namespace std; // Returns sum of min and max element of all subarrays // of size k int SumOfKsubArray(int arr[] , int n , int k) { int sum = 0; // Initialize result // The queue will store i...
constant
linear
// C++ program to find distance of nearest // cell having 1 in a binary matrix. #include <bits/stdc++.h> #define N 3 #define M 4 using namespace std; // Print the distance of nearest cell // having 1 for each cell. void printDistance(int mat[N][M]) { int ans[N][M]; // Initialize the answer matrix with INT_M...
quadratic
quadratic
// C++ program to find distance of nearest // cell having 1 in a binary matrix. #include <bits/stdc++.h> #define MAX 500 #define N 3 #define M 4 using namespace std; // Making a class of graph with bfs function. class graph { private: vector<int> g[MAX]; int n, m; public: graph(int a, int b) { ...
quadratic
quadratic
// Java program to find distance of nearest // cell having 1 in a binary matrix. import java.util.*; class gfg { static int N = 3; static int M = 4; static int MAX = 500; // Making a class of graph with bfs function. static class graph { ArrayList<ArrayList<Integer> > g; int n, m; graph(...
quadratic
quadratic
// C++ program to do level order traversal line by // line #include <bits/stdc++.h> using namespace std; struct Node { int data; Node *left, *right; }; // Prints level order traversal line by line // using two queues. void levelOrder(Node *root) { queue<Node *> q1, q2; if (root == NULL) re...
linear
linear
// C++ program to find min sum of squares // of characters after k removals #include <bits/stdc++.h> using namespace std; const int MAX_CHAR = 26; // Main Function to calculate min sum of // squares of characters after k removals int minStringValue(string str, int k) { int l = str.length(); // find length of st...
constant
nlogn
// C++ program to find min sum of squares // of characters after k removals #include <bits/stdc++.h> using namespace std; const int MAX_CHAR = 26; // Main Function to calculate min sum of // squares of characters after k removals int minStringValue(string str, int k) { int alphabetCount[MAX_CHAR]= {0}; //...
linear
linear
// C++ program for a Queue based approach // to find first non-repeating character #include <bits/stdc++.h> using namespace std; const int MAX_CHAR = 26; // function to find first non repeating // character of sa stream void firstnonrepeating(char str[]) { queue<char> q; int charCount[MAX_CHAR] = { 0 }; ...
linear
linear
// C++ code for the above approach #include <bits/stdc++.h> using namespace std; // A binary tree node has data, pointer to // left child and a pointer to right child struct Node { int val; struct Node *left, *right; }; // Initialising a map with key as levels of the tree map<int, pair<double, double> > m...
logn
nlogn
// Given two arrays, check if one array is // stack permutation of other. #include<bits/stdc++.h> using namespace std; // function to check if input queue is // permutable to output queue bool checkStackPermutation(int ip[], int op[], int n) { // Input queue queue<int> input; for (int i=0;i<n;i++) ...
linear
linear
// Given two arrays, check if one array is // stack permutation of other. #include<bits/stdc++.h> using namespace std; // function to check if input array is // permutable to output array bool checkStackPermutation(int ip[], int op[], int n) { // we will be pushing elements from input array to stack uptill top o...
linear
linear