- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 7347 Articles for C++
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Arnab Chakraborty](https://www.tutorialspoint.com/assets/profiles/123881/profile/60_2940367-1661162113.png)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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
![Ayush Gupta](https://www.tutorialspoint.com/assets/profiles/134135/profile/60_13095-1568350695.jpg)
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