code
stringlengths
195
7.9k
space_complexity
stringclasses
6 values
time_complexity
stringclasses
7 values
// C++ program to print DFS traversal from // a given vertex in a given graph #include <bits/stdc++.h> using namespace std; // Graph class represents a directed graph // using adjacency list representation class Graph { public: map<int, bool> visited; map<int, list<int> > adj; // function to add an edg...
linear
linear
// C++ program to print DFS // traversal for a given // graph #include <bits/stdc++.h> using namespace std; class Graph { // A function used by DFS void DFSUtil(int v); public: map<int, bool> visited; map<int, list<int> > adj; // function to add an edge to graph void addEdge(int v, int w);...
linear
linear
// C++ program to find a mother vertex in O(V+E) time #include <bits/stdc++.h> using namespace std; class Graph { int V; // No. of vertices list<int>* adj; // adjacency lists // A recursive function to print DFS starting from v void DFSUtil(int v, vector<bool>& visited); public: Graph(int V); ...
linear
linear
/* * C++ program to count all paths from a source to a * destination. * https://www.geeksforgeeks.org/count-possible-paths-two-vertices/ * Note that the original example has been refactored. */ #include <bits/stdc++.h> using namespace std; /* * A directed graph using adjacency list representation; * every vert...
linear
np
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; void printpath(map<pii, pii> mp, pii u) { if (u.first == 0 && u.second == 0) { cout << 0 << " " << 0 << endl; return; } printpath(mp, mp[u]); cout << u.first << " " << u.second << endl; } void BFS(int a, int b, int...
quadratic
quadratic
// CPP Program to determine level of each node // and print level #include <bits/stdc++.h> using namespace std; // function to determine level of each node starting // from x using BFS void printLevels(vector<int> graph[], int V, int x) { // array to store level of each node int level[V]; bool marked[V]; ...
linear
linear
// C++ program to print all paths of source to // destination in given graph #include <bits/stdc++.h> using namespace std; // utility function for printing // the found path in graph void printpath(vector<int>& path) { int size = path.size(); for (int i = 0; i < size; i++) cout << path[i] << " "; ...
quadratic
np
// C++ program to find minimum edge // between given two vertex of Graph #include<bits/stdc++.h> using namespace std; // function for finding minimum no. of edge // using BFS int minEdgeBFS(vector <int> edges[], int u, int v, int n) { // visited[n] for keeping track of visited //...
linear
linear
// C++ program to find minimum steps to reach to // specific cell in minimum moves by Knight #include <bits/stdc++.h> using namespace std; // structure for storing a cell's data struct cell { int x, y; int dis; cell() {} cell(int x, int y, int dis) : x(x) , y(y) , dis(dis) ...
quadratic
quadratic
// C++ Program to find the length of the largest // region in boolean 2D-matrix #include <bits/stdc++.h> using namespace std; #define ROW 4 #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]) { // r...
quadratic
quadratic
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find unit area of the largest region of 1s. int largestRegion(vector<vector<int> >& grid) { int m = grid.size(); int n = grid[0].size(); // creating a queue that will help in bfs traversal queue<pair...
quadratic
quadratic
// A C++ Program to detect // cycle in an undirected graph #include <iostream> #include <limits.h> #include <list> using namespace std; // Class for an undirected graph class Graph { // No. of vertices int V; // Pointer to an array // containing adjacency lists list<int>* adj; bool isCycli...
linear
linear
// A DFS based approach to find if there is a cycle // in a directed graph. This approach strictly follows // the algorithm given in CLRS book. #include <bits/stdc++.h> using namespace std; enum Color {WHITE, GRAY, BLACK}; // Graph class represents a directed graph using // adjacency list representation class Grap...
linear
linear
// C++ program to check if there is a cycle of // total odd weight #include <bits/stdc++.h> using namespace std; // This function returns true if the current subpart // of the forest is two colorable, else false. bool twoColorUtil(vector<int>G[], int src, int N, int colorArr[]) { ...
linear
quadratic
// CPP program to implement Union-Find with union // by rank and path compression. #include <bits/stdc++.h> using namespace std; const int MAX_VERTEX = 101; // Arr to represent parent of index i int Arr[MAX_VERTEX]; // Size to represent the number of nodes // in subgraph rooted at index i int size[MAX_VERTEX]; ...
constant
logn
// C++ program to find number of magical // indices in the given array. #include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back #define mod 1000000007 // Function to count number of magical indices. int solve(int A[], int n) { int i, cnt = 0, j; // Array to store parent nod...
linear
linear
// C++ program to print all topological sorts of a graph #include <bits/stdc++.h> using namespace std; class Graph { int V; // No. of vertices // Pointer to an array containing adjacency list list<int> *adj; // Vector to store indegree of vertices vector<int> indegree; // A function u...
linear
quadratic
// A C++ program to print topological // sorting of a graph using indegrees. #include <bits/stdc++.h> using namespace std; // Class to represent a graph class Graph { // No. of vertices' int V; // Pointer to an array containing // adjacency listsList list<int>* adj; public: // Constructor ...
linear
linear
// A C++ program for Prim's Minimum // Spanning Tree (MST) algorithm. The program is // for adjacency matrix representation of the graph #include <bits/stdc++.h> using namespace std; // Number of vertices in the graph #define V 5 // A utility function to find the vertex with // minimum key value, from the set of ve...
linear
quadratic
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // DSU data structure // path compression + rank by union class DSU { int* parent; int* rank; public: DSU(int n) { parent = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) { ...
linear
nlogn
/* C++ program to solve N Queen Problem using backtracking */ #include <bits/stdc++.h> #define N 4 using namespace std; /* A utility function to print solution */ void printSolution(int board[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) cout << " " << board[i][j] <...
quadratic
np
/* C++ program to solve N Queen Problem using backtracking */ #include<bits/stdc++.h> using namespace std; #define N 4 /* ld is an array where its indices indicate row-col+N-1 (N-1) is for shifting the difference to store negative indices */ int ld[30] = { 0 }; /* rd is an array where its indices indicate row+col...
linear
np
// C++ program for Dijkstra's single source shortest path // algorithm. The program is for adjacency matrix // representation of the graph #include <iostream> using namespace std; #include <limits.h> // Number of vertices in the graph #define V 9 // A utility function to find the vertex with minimum // distance val...
linear
quadratic
// C++ Program to find Dijkstra's shortest path using // priority_queue in STL #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f // iPair ==> Integer Pair typedef pair<int, int> iPair; // This class represents a directed graph using // adjacency list representation class Graph { int V; // No....
linear
nlogn
// A C++ program for Bellman-Ford's single source // shortest path algorithm. #include <bits/stdc++.h> using namespace std; // a structure to represent a weighted edge in graph struct Edge { int src, dest, weight; }; // a structure to represent a connected, directed and // weighted graph struct Graph { // V...
linear
quadratic
// C++ Program for Floyd Warshall Algorithm #include <bits/stdc++.h> using namespace std; // Number of vertices in the graph #define V 4 /* Define Infinite as a large enough value.This value will be used for vertices not connected to each other */ #define INF 99999 // A function to print the solution matrix void ...
quadratic
cubic
// Dynamic Programming based C++ program to find shortest path with // exactly k edges #include <iostream> #include <climits> using namespace std; // Define number of vertices in the graph and infinite value #define V 4 #define INF INT_MAX // A Dynamic programming based function to find the shortest path from // u ...
np
cubic
// CPP code for printing shortest path between // two vertices of unweighted graph #include <bits/stdc++.h> using namespace std; // utility function to form edge between two vertices // source and dest void add_edge(vector<int> adj[], int src, int dest) { adj[src].push_back(dest); adj[dest].push_back(src); } ...
linear
linear
// C++ program to get least cost path in a grid from // top-left to bottom-right #include <bits/stdc++.h> using namespace std; #define ROW 5 #define COL 5 // structure for information of each cell struct cell { int x, y; int distance; cell(int x, int y, int distance) : x(x) , y(y) ...
quadratic
quadratic
// C++ program to replace all of the O's in the matrix // with their shortest distance from a guard #include <bits/stdc++.h> using namespace std; // store dimensions of the matrix #define M 5 #define N 5 // An Data Structure for queue used in BFS struct queueNode { // i, j and distance stores x and y-coordinate...
quadratic
quadratic
// C++ program to find articulation points in an undirected graph #include <bits/stdc++.h> using namespace std; // A recursive function that find articulation // points using DFS traversal // adj[] --> Adjacency List representation of the graph // u --> The vertex to be visited next // visited[] --> keeps track of vi...
linear
linear
// 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 find number of islands // using Disjoint Set data structure. #include <bits/stdc++.h> using namespace std; // Class to represent // Disjoint Set Data structure class DisjointUnionSets { vector<int> rank, parent; int n; public: DisjointUnionSets(int n) { ra...
quadratic
quadratic
// C++ program to count walks from u to // v with exactly k edges #include <iostream> using namespace std; // Number of vertices in the graph #define V 4 // A naive recursive function to count // walks from u to v with k edges int countwalks(int graph[][V], int u, int v, int k) { // Base cases if (k == 0 &&...
linear
quadratic
// C++ program to count walks from // u to v with exactly k edges #include <iostream> using namespace std; // Number of vertices in the graph #define V 4 // A Dynamic programming based function to count walks from u // to v with k edges int countwalks(int graph[][V], int u, int v, int k) { // Table to be filled...
np
cubic
// C++ program to find length // of the shortest chain // transformation from source // to target #include <bits/stdc++.h> using namespace std; // Returns length of shortest chain // to reach 'target' from 'start' // using minimum number of adjacent // moves. D is dictionary int shortestChainLen( string start, strin...
quadratic
cubic
// C++ program to find length // of the shortest chain // transformation from source // to target #include <bits/stdc++.h> using namespace std; // Returns length of shortest chain // to reach 'target' from 'start' // using minimum number of adjacent // moves. D is dictionary int shortestChainLen( string start, strin...
quadratic
cubic
// C++ program to print all paths // from a source to destination. #include <iostream> #include <list> using namespace std; // A directed graph using // adjacency list representation class Graph { int V; // No. of vertices in graph list<int>* adj; // Pointer to an array containing // adjac...
np
quadratic
// C++ code to check if cyclic order is possible among strings // under given constraints #include <bits/stdc++.h> using namespace std; #define M 26 // Utility method for a depth first search among vertices void dfs(vector<int> g[], int u, vector<bool> &visit) { visit[u] = true; for (int i = 0; i < g[u].si...
linear
linear
// C++ program to count cyclic points // in an array using Kosaraju's Algorithm #include <bits/stdc++.h> using namespace std; // Most of the code is taken from below link // https://www.geeksforgeeks.org/strongly-connected-components/ class Graph { int V; list<int>* adj; void fillOrder(int v, bool visited...
linear
linear
// C++ program to print connected components in // an undirected graph #include <bits/stdc++.h> using namespace std; // Graph class represents a undirected graph // using adjacency list representation class Graph { int V; // No. of vertices // Pointer to an array containing adjacency lists list<int>* ad...
linear
linear
#include <bits/stdc++.h> using namespace std; int merge(int* parent, int x) { if (parent[x] == x) return x; return merge(parent, parent[x]); } int connectedcomponents(int n, vector<vector<int> >& edges) { int parent[n]; for (int i = 0; i < n; i++) { parent[i] = i; } for (auto...
linear
linear
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; int maxindex(int* dist, int n) { int mi = 0; for (int i = 0; i < n; i++) { if (dist[i] > dist[mi]) mi = i; } return mi; } void selectKcities(int n, int weights[4][4], int k) { int* dist = new ...
constant
quadratic
// 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
// CPP14 program to find common contacts. #include <bits/stdc++.h> using namespace std; // The class DSU will implement the Union Find class DSU { vector<int> parent, size; public: // In the constructor, we assign the each index as its // own parent and size as the number of contacts // available ...
linear
np
#include <bits/stdc++.h> using namespace std; void print_sieve(int& x) { int i,temp,digit; bool check; for(i=0;i<=x;i++) { if(i<10) { cout<<i<<" "; continue; } check=1; temp=i; digit=temp%10; temp/=10; whil...
constant
linear
// Finds and prints all jumping numbers smaller than or // equal to x. #include <bits/stdc++.h> using namespace std; // Prints all jumping numbers smaller than or equal to x starting // with 'num'. It mainly does BFS starting from 'num'. void bfs(int x, int num) { // Create a queue and enqueue 'i' to it queue...
constant
linear
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to find and print pair bool chkPair(int A[], int size, int x) { for (int i = 0; i < (size - 1); i++) { for (int j = (i + 1); j < size; j++) { if (A[i] + A[j] == x) { return 1; ...
constant
quadratic
// C++ program to check if given array // has 2 elements whose sum is equal // to the given value #include <bits/stdc++.h> using namespace std; // Function to check if array has 2 elements // whose sum is equal to the given value bool hasArrayTwoCandidates(int A[], int arr_size, int sum) { int l, r; /* So...
constant
nlogn
// C++ program to check if given array // has 2 elements whose sum is equal // to the given value #include <bits/stdc++.h> using namespace std; bool binarySearch(int A[], int low, int high, int searchKey) { while (low <= high) { int m = low + (high - low) / 2; // Check if searchKey is presen...
constant
nlogn
// C++ program to check if given array // has 2 elements whose sum is equal // to the given value #include <bits/stdc++.h> using namespace std; void printPairs(int arr[], int arr_size, int sum) { unordered_set<int> s; for (int i = 0; i < arr_size; i++) { int temp = sum - arr[i]; if (s.find...
linear
linear
// Code in cpp to tell if there exists a pair in array whose // sum results in x. #include <iostream> using namespace std; // Function to print pairs void printPairs(int a[], int n, int x) { int i; int rem[x]; // initializing the rem values with 0's. for (i = 0; i < x; i++) rem[i] = 0; // ...
linear
linear
// CPP program to find the minimum number of // operations required to make all elements // of array equal #include <bits/stdc++.h> using namespace std; // function for min operation int minOperation (int arr[], int n) { // Insert all elements in hash. unordered_map<int, int> hash; for (int i=0; i<n; i++)...
linear
linear
// C++ program to find maximum distance between two // same occurrences of a number. #include<bits/stdc++.h> using namespace std; // Function to find maximum distance between equal elements int maxDistance(int arr[], int n) { // Used to store element to first index mapping unordered_map<int, int> mp; //...
linear
linear
/* C/C++ program to find maximum number of point which lie on same line */ #include <bits/stdc++.h> #include <boost/functional/hash.hpp> using namespace std; // method to find maximum collinear point int maxPointOnSameLine(vector< pair<int, int> > points) { int N = points.size(); if (N < 2) return N...
linear
quadratic
// C++ implementation of the // above approach #include <bits/stdc++.h> using namespace std; // Function to find the Duplicates, // if duplicate occurs 2 times or // more than 2 times in array so, // it will print duplicate value // only once at output void findDuplicates(int arr[], int len) { // Initialize...
linear
quadratic
// C++ program to find // duplicates in the given array #include <bits/stdc++.h> using namespace std; // function to find and print duplicates void printDuplicates(int arr[], int n) { // unordered_map to store frequencies unordered_map<int, int> freq; for (int i=0; i<n; i++) freq[arr[i]]++; ...
linear
linear
// C++ program to find top k elements in a stream #include <bits/stdc++.h> using namespace std; // Function to print top k numbers void kTop(int a[], int n, int k) { // vector of size k+1 to store elements vector<int> top(k + 1); // array to keep track of frequency unordered_map<int, int> freq; ...
linear
quadratic
// CPP program to find the most frequent element // in an array. #include <bits/stdc++.h> using namespace std; int mostFrequent(int arr[], int n) { // Sort the array sort(arr, arr + n); // Find the max frequency using linear traversal int max_count = 1, res = arr[0], curr_count = 1; for (int i =...
constant
nlogn
// CPP program to find the most frequent element // in an array. #include <bits/stdc++.h> using namespace std; int mostFrequent(int arr[], int n) { // Insert all elements in hash. unordered_map<int, int> hash; for (int i = 0; i < n; i++) hash[arr[i]]++; // find the max frequency int max_...
linear
linear
#include <iostream> using namespace std; int maxFreq(int *arr, int n) { //using moore's voting algorithm int res = 0; int count = 1; for(int i = 1; i < n; i++) { if(arr[i] == arr[res]) { count++; } else { count--; } if(count == 0) { ...
constant
linear
// C++ implementation to find first // element occurring k times #include <bits/stdc++.h> using namespace std; // Function to find the first element // occurring k number of times int firstElement(int arr[], int n, int k) { // This loop is used for selection // of elements for (int i = 0; i < n; i++) { ...
constant
quadratic
// C++ implementation to find first // element occurring k times #include <bits/stdc++.h> using namespace std; // function to find the first element // occurring k number of times int firstElement(int arr[], int n, int k) { // unordered_map to count // occurrences of each element unordered_map<int, int>...
linear
linear
// C++ implementation to find first // element occurring k times #include <bits/stdc++.h> using namespace std; // function to find the first element // occurring k number of times int firstElement(int arr[], int n, int k) { // unordered_map to count // occurrences of each element map<int, int> count_map; fo...
linear
nlogn
// A C++ program to find all symmetric pairs in a given // array of pairs #include <bits/stdc++.h> using namespace std; void findSymPairs(int arr[][2], int row) { // This loop for selection of one pair for (int i = 0; i < row; i++) { // This loop for searching of symmetric pair for (...
constant
quadratic
#include<bits/stdc++.h> using namespace std; // A C++ program to find all symmetric pairs in a given array of pairs // Print all pairs that have a symmetric counterpart void findSymPairs(int arr[][2], int row) { // Creates an empty hashMap hM unordered_map<int, int> hM; // Traverse through the given arr...
linear
linear
// C++ program to find the only repeating // element in an array where elements are // from 1 to N-1. #include <bits/stdc++.h> using namespace std; int findRepeating(int arr[], int N) { for (int i = 0; i < N; i++) { for (int j = i + 1; j < N; j++) { if (arr[i] == arr[j]) return...
constant
quadratic
// CPP program to find the only repeating // element in an array where elements are // from 1 to N-1. #include <bits/stdc++.h> using namespace std; int findRepeating(int arr[], int N) { sort(arr, arr + N); // sort array for (int i = 0; i < N; i++) { // compare array element with its index ...
constant
nlogn
// C++ program to find the only repeating // element in an array where elements are // from 1 to N-1. #include <bits/stdc++.h> using namespace std; int findRepeating(int arr[], int N) { unordered_set<int> s; for (int i = 0; i < N; i++) { if (s.find(arr[i]) != s.end()) return arr[i]; ...
linear
linear
// CPP program to find the only repeating // element in an array where elements are // from 1 to N-1. #include <bits/stdc++.h> using namespace std; int findRepeating(int arr[], int N) { // Find array sum and subtract sum // first N-1 natural numbers from it // to find the result. return accumulate(arr...
constant
linear
// CPP program to find the only repeating // element in an array where elements are // from 1 to N-1. #include <bits/stdc++.h> using namespace std; int findRepeating(int arr[], int N) { // res is going to store value of // 1 ^ 2 ^ 3 .. ^ (N-1) ^ arr[0] ^ // arr[1] ^ .... arr[N-1] int res = 0; fo...
constant
linear
// CPP program to find the only // repeating element in an array // where elements are from 1 to N-1. #include <bits/stdc++.h> using namespace std; // Function to find repeated element int findRepeating(int arr[], int N) { int missingElement = 0; // indexing based for (int i = 0; i < N; i++) { ...
constant
linear
#include <bits/stdc++.h> using namespace std; int findDuplicate(vector<int>& nums) { int slow = nums[0]; int fast = nums[0]; do { slow = nums[slow]; fast = nums[nums[fast]]; } while (slow != fast); fast = nums[0]; while (slow != fast) { slow = nums[slow]; fa...
constant
linear
// C++ program to find one of the repeating // elements in a read only array #include <bits/stdc++.h> using namespace std; // Function to find one of the repeating // elements int findRepeatingNumber(const int arr[], int n) { for (int i = 0; i < n; i++) { int count = 0; for (int j = 0; j < n; j++)...
constant
quadratic
// C++ Program to Find the top three repeated numbers #include <bits/stdc++.h> using namespace std; /* Function to print top three repeated numbers */ void top3Repeated(int arr[], int n) { // There should be atleast two elements if (n < 3) { cout << "Invalid Input"; return; } // Coun...
linear
linear
// A simple C++ program to group multiple occurrences of individual // array elements #include<bits/stdc++.h> using namespace std; // A simple method to group all occurrences of individual elements void groupElements(int arr[], int n) { // Initialize all elements as not visited bool *visited = new bool[n]; ...
linear
quadratic
#include<bits/stdc++.h> using namespace std; // C++ program to group multiple // occurrences of individual array elements // A hashing based method to group // all occurrences of individual elements void orderedGroup(vector<int>&arr){ // Creates an empty hashmap map<int,int>hM; // Traverse the a...
linear
linear
// A Simple C++ program to check if two sets are disjoint #include<bits/stdc++.h> using namespace std; // Returns true if set1[] and set2[] are disjoint, else false bool areDisjoint(int set1[], int set2[], int m, int n) { // Take every element of set1[] and search it in set2 for (int i=0; i<m; i++) for ...
constant
quadratic
/* C++ program to check if two sets are distinct or not */ #include<bits/stdc++.h> using namespace std; // This function prints all distinct elements bool areDisjoint(int set1[], int set2[], int n1, int n2) { // Creates an empty hashset set<int> myset; // Traverse the first set and store its elements in...
linear
linear
// CPP program to find Non-overlapping sum #include <bits/stdc++.h> using namespace std; // function for calculating // Non-overlapping sum of two array int findSum(int A[], int B[], int n) { // Insert elements of both arrays unordered_map<int, int> hash; for (int i = 0; i < n; i++) { hash[A[...
linear
linear
// C++ simple program to // find elements which are // not present in second array #include<bits/stdc++.h> using namespace std; // Function for finding // elements which are there // in a[] but not in b[]. void findMissing(int a[], int b[], int n, int m) { for (int i = 0; i < n; i++) { ...
constant
quadratic
// C++ efficient program to // find elements which are not // present in second array #include<bits/stdc++.h> using namespace std; // Function for finding // elements which are there // in a[] but not in b[]. void findMissing(int a[], int b[], int n, int m) { // Store all elements of // second...
linear
linear
// C++ program to find given two array // are equal or not #include <bits/stdc++.h> using namespace std; // Returns true if arr1[0..n-1] and arr2[0..m-1] // contain same elements. bool areEqual(int arr1[], int arr2[], int N, int M) { // If lengths of array are not equal means // array are not equal if (N ...
constant
nlogn
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Returns true if arr1[0..N-1] and arr2[0..M-1] // contain same elements. bool areEqual(int arr1[], int arr2[], int N, int M) { // If lengths of arrays are not equal if (N != M) return false; // Store arr1[] e...
linear
linear
// C++ code to find maximum shortest distance // from endpoints #include <bits/stdc++.h> using namespace std; // function to find maximum shortest distance int find_maximum(int a[], int n, int k) { // stores the shortest distance of every // element in original array. unordered_map<int, int> b; ...
linear
linear
// C++ program to find the k-th missing element // in a given sequence #include <bits/stdc++.h> using namespace std; // Returns k-th missing element. It returns -1 if // no k is more than number of missing elements. int find(int a[], int b[], int k, int n1, int n2) { // Insert all elements of givens sequence b[]....
quadratic
linear
// C++ program to find a pair with product // in given array. #include<bits/stdc++.h> using namespace std; // Function to find greatest number that us int findGreatest( int arr[] , int n) { int result = -1; for (int i = 0; i < n ; i++) for (int j = 0; j < n-1; j++) for (int k = j+1 ; k < n...
constant
cubic
// C++ program to find the largest product number #include <bits/stdc++.h> using namespace std; // Function to find greatest number int findGreatest(int arr[], int n) { // Store occurrences of all elements in hash // array unordered_map<int, int> m; for (int i = 0; i < n; i++) m[arr[i]]++; ...
linear
nlogn
// A sorting based solution to find the // minimum number of subsets of a set // such that every subset contains distinct // elements. #include <bits/stdc++.h> using namespace std; // Function to count subsets such that all // subsets have distinct elements. int subset(int ar[], int n) { // Take input and initial...
constant
quadratic
// A hashing based solution to find the // minimum number of subsets of a set // such that every subset contains distinct // elements. #include <bits/stdc++.h> using namespace std; // Function to count subsets such that all // subsets have distinct elements. int subset(int arr[], int n) { // Traverse the input ...
linear
linear
// CPP program to find minimum element // to remove so no common element // exist in both array #include <bits/stdc++.h> using namespace std; // To find no elements to remove // so no common element exist int minRemove(int a[], int b[], int n, int m) { // To store count of array element unordered_map<int, int...
linear
linear
// C++ implementation to count items common to both // the lists but with different prices #include <bits/stdc++.h> using namespace std; // details of an item struct item { string name; int price; }; // function to count items common to both // the lists but with different prices int countItems(item list1...
constant
quadratic
// C++ implementation to count // items common to both the lists // but with different prices #include <bits/stdc++.h> using namespace std; // Details of an item struct item { string name; int price; }; // comparator function // used for sorting bool compare(struct item a, struct item b) { return...
constant
nlogn
// C++ implementation to count items common to both // the lists but with different prices #include <bits/stdc++.h> using namespace std; // details of an item struct item { string name; int price; }; // function to count items common to both // the lists but with different prices int countItems(item list1...
linear
linear
#include <bits/stdc++.h> using namespace std; // Function to print common strings with minimum index sum void find(vector<string> list1, vector<string> list2) { vector<string> res; // resultant list int max_possible_sum = list1.size() + list2.size() - 2; // iterating over sum in ascending order for ...
quadratic
linear
// Hashing based C++ program to find common elements // with minimum index sum. #include <bits/stdc++.h> using namespace std; // Function to print common strings with minimum index sum void find(vector<string> list1, vector<string> list2) { // mapping strings to their indices unordered_map<string, int> map; ...
quadratic
linear
// 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
// 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++ 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