Category: cpp-vector

Print Adjacency List for a Directed Graph

An Adjacency List is used for representing graphs. Here, for every vertex in the graph, we have a list of all the other vertices which the particular vertex has an edge to.Problem: Given the adjacency list and number of vertices and edges of a graph, the task is to represent the adjacency list for a […]

Continue Reading

Count of ways to select exactly K non-disjoint ranges from given N ranges

#include using namespace std;  int nCr(int n, int r, int* f){    return f[n] / f[r] * f[n – r];}  int NumberOfWaysToChooseKRanges(int L[], int R[],                                int N, int K){        int f[N + 1];    f[0] = 1;          for (int i = 1; i = K – 1) {                                ans += nCr(Cnt, K – 1, f);            }                          Cnt++;        }        else {                        Cnt–;        }    }          return ans;}  int main(){        int N = 7, K = 3;    int L[] […]

Continue Reading

push_back() vs emplace_back() in C++ STL Vectors

push_back() vs emplace_back() in C++ STL VectorsIn C++, vectors are dynamic arrays, that can grow or shrink and their storage is handled by the container itself. There are two ways of inserting an element in a vector. They are push_back() and emplace_back(). In this article, we will discuss the difference between them.push_back():This method is used […]

Continue Reading