Found 7347 Articles for C++

Find the direction from given string in C++

Arnab Chakraborty
Updated on 19-Aug-2020 11:43:42

545 Views

Suppose we have a string which contains only L and R, this denotes left rotation and right rotation respectively, we have to find the final direction of pivot. Here directions are north(N), east(E), south(S) and west(W). We are assuming that the pivot is pointed towards north(N) in a compass.So, if the input is like "RRLRLLR", then the output will be E, as initial direction is N, RR will point to S, then LR will point to the same N again, then LL will point to previous position N, then R will point to E. So E is the final.To solve ... Read More

Find the closest element in Binary Search Tree in C++

Arnab Chakraborty
Updated on 19-Aug-2020 11:36:20

262 Views

Suppose we have one binary search tree (BST) and another target value; we have to find k values in that given BST that are closest to the target. Here the target value is a floating-point number. We can assume k is always valid, and k ≤ total nodes.So, if the input is liketarget = 3.714286, and k = 2, then the output will be [4, 3]To solve this, we will follow these steps −Define a function pushSmaller(), this will take node, stack st, and target, while node is not present, do −if val of node < target, then −insert node ... Read More

Find smallest range containing elements from k lists in C++

Arnab Chakraborty
Updated on 19-Aug-2020 11:22:18

58 Views

Suppose we have k different lists. The elements are sorted. We have to search the smallest range that includes at least one number from each of the k different lists. Here the range [a, b] is smaller than range [c, d] when b-a < d-c or a < c if b-a == d-c.So if the input is like [[4, 10, 15, 25, 26], [0, 9, 14, 20], [5, 18, 24, 30]], then the output will be [14, 18]To solve this, we will follow these steps −minRange := inf, maxRange := -inf, rangeSize := inf, tempMinRange := inf, tempMaxRange := -infn ... Read More

Find paths from corner cell to middle cell in maze in C++

Arnab Chakraborty
Updated on 19-Aug-2020 11:09:16

200 Views

Suppose we have a square maze filled with numbers; we have to find all paths from a corner cell to the middle cell. Here, we will proceed exactly n steps from a cell in 4 directions Up, Down, Right and Left where n is the value of the cell. Thus, we can move to cell [i+n, j] to [i-n, j], [i, j+n], and [i, j-n] from a cell [i, j] where n is value of cell [i, j].So, if the input is like344473463675662662334325472655123656334301434334321335354326443351375363624345451then the output will be(0, 0)→(0, 3)→(0, 7)→(6, 7)→(6, 3)→(3, 3)→(3, 4)→(5, 4)→(5, 2)→(1, 2)→(1, 7)→(7, 7)→(7, 1)→(2, ... Read More

Query for ancestor-descendant relationship in a tree in C++

Ayush Gupta
Updated on 19-Aug-2020 10:56:20

121 Views

In this tutorial, we will be discussing a program to find query for ancestor-descendant relationship in a tree.For this we will be provided with a rooted tree and Q queries. Our task is to find the two roots given in the query is an ancestor of the other or not.Example Live Demo#include using namespace std; //using DFS to find the relation between //given nodes void performingDFS(vector g[], int u, int parent, int timeIn[], int timeOut[], int& count) {    timeIn[u] = count++;    for (int i = 0; i < g[u].size(); i++) {       int v = g[u][i]; ... Read More

Find three integers less than or equal to N such that their LCM is maximum in C++

Ayush Gupta
Updated on 19-Aug-2020 10:54:33

69 Views

In this tutorial, we will be discussing a program to find three integers less than or equal to N such that their LCM is maximum.For this we will be provided with an integer value. Our task is to find other three integers smaller than the given value such that their LCM is maximum.Example Live Demo#include using namespace std; //finding three integers less than given value //having maximum LCM void findMaximumLCM(int n) {    if (n % 2 != 0) {       cout

Queries to return the absolute difference between Lth smallest number and the R-th smallest number in C++

Ayush Gupta
Updated on 19-Aug-2020 10:53:41

99 Views

In this tutorial, we will be discussing a program to find queries to return the absolute difference between L-th smallest number and the R-th smallest number.For this we will be provided with an array containing integers and Q queries. Our task is to find the absolute difference between the indices of Lth smallest and Rth smallest values.Example Live Demo#include using namespace std; //returning the result of a query int respondingQuery(pair arr[], int l, int r) {    int result = abs(arr[l - 1].second - arr[r - 1].second);    return result; } //implementing the queries void calcDifference(int givenarr[], int a, int ... Read More

Querying the number of distinct colors in a subtree of a colored tree using BIT in C++

Ayush Gupta
Updated on 19-Aug-2020 10:51:40

138 Views

In this tutorial, we will be discussing a program to find querying the number of distinct colors in a subtree of a colored tree using BIT.For this we will be provided with rooted tree where each node has a color denoted by given array. Our task is to find all the distinct coloured nodes below the given node in the tree.Example Live Demo#include #define MAXIMUM_COLOUR 1000005 #define MAXIMUM_NUMBER 100005 using namespace std; vector tree[MAXIMUM_NUMBER]; vector table[MAXIMUM_COLOUR]; int isTraversing[MAXIMUM_COLOUR]; int bit[MAXIMUM_NUMBER], getVisTime[MAXIMUM_NUMBER], getEndTime[MAXIMUM_NUMBER]; int getFlatTree[2 * MAXIMUM_NUMBER]; bool vis[MAXIMUM_NUMBER]; int tim = 0; vector< pair< pair, int> > queries; //storing results of ... Read More

Queries to update a given index and find gcd in range in C++

Ayush Gupta
Updated on 19-Aug-2020 10:48:16

77 Views

In this tutorial, we will be discussing a program to find queries to update a given index and find gcd in range.For this we will be provided with an array containing integers and Q queries. Our task is to find the result of given queries (updating a given value by X, finding the gcd between two given values).Example Live Demo#include using namespace std; //getting middle index int findMiddle(int s, int e) {    return (s + (e - s) / 2); } //updating values at given indices void updateIndexValue(int* st, int ss, int se, int i, int diff, int si) ... Read More

Find total number of distinct years from a string in C++

Ayush Gupta
Updated on 19-Aug-2020 10:45:52

2K+ Views

In this tutorial, we will be discussing a program to find total number of distinct years from a string.For this we will be provided with a string containing dates in format ‘DD-MM-YYYY’ format. Our task is to find the count of distinct years mentioned in the given string.Example Live Demo#include using namespace std; //calculating the distinct years mentioned int calculateDifferentYears(string str) {    unordered_set differentYears;    string str2 = "";    for (int i = 0; i < str.length(); i++) {       if (isdigit(str[i])) {          str2.push_back(str[i]);       }       if ... Read More

Advertisements