Server Side Programming Articles

Page 1362 of 2109

Find a Corresponding Node of a Binary Tree in a Clone of That Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 715 Views

Suppose we have two binary trees original and cloned and given a reference to a node target in the original tree. The cloned tree is actually a copy of the original tree. We have to find a reference to the same node in the cloned tree.So if the tree is like below and the target is 3, then the output will be 3.To solve this, we will follow these steps −Define a method called solve(), this will take the node1m node2 and targetif node1 is null, then return nullif node1 is target and value of node 1 is the value ...

Read More

Design a Stack With Increment Operation in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 784 Views

Suppose we want to design a stack that supports the following operations.CustomStack(int maxSize) This initializes the object with maxSize which is the maximum number of elements in the stack or do nothing if the stack reached the maxSize.void push(int x) This inserts x to the top of the stack if the stack hasn't reached the maxSize.int pop() This deletes and returns the top of stack or -1 if the stack is empty.void inc(int k, int val) This increments the bottom k elements of the stack by val. If there are less than k elements in the stack, just increment all ...

Read More

Sort Integers by The Power Value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 306 Views

As we know that the power of an integer x is defined as the number of steps needed to transform x into 1 using the following steps −if x is even then x = x / 2if x is odd then x = 3 * x + 1So for example, the power of x = 3 is 7 because 3 uses 7 steps to become 1 (3 → 10 → 5 → 16 → 8 → 4 → 2 → 1). So if we have some integers lo, hi and k. We have to sort all integers in the interval ...

Read More

Convert Sorted List to Binary Search Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 260 Views

Suppose we have a singly linked list where elements are sorted in ascending order, we have to convert it to a height balanced BST. So if the list is like [-10, -3, 0, 5, 9], The possible tree will be like −To solve this, we will follow these steps −If the list is empty, then return nullDefine a recursive method called sortedListToBST() this will take list start nodex := address of the previous node of mid node from list amid := exact mid nodecreate a new node with value by taking from the value of midnextStart := next of mid ...

Read More

Sentence Screen Fitting in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 276 Views

Suppose we have a rows x cols screen and a sentence represented by a list of non-empty words, so we have to find how many times the given sentence can be fitted on the screen. There are certain properties −A word will not be split into two lines.The order of words in the sentence must not be changed.There will be only one space between two words.The total number of words in the sentence won't exceed 100.The length of each word is greater than 0 but less than 10.1 ≤ rows, cols ≤ 20, 000.So if the input is like rows ...

Read More

Ternary Expression Parser in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 691 Views

Suppose we have a string representing arbitrarily nested ternary expressions, we have to calculate the result of the expression. we can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F these few characters. (Here T and F represent True and False respectively). There are some properties −The length of the given string must be less than or equal to 10000.Each number will contain only one digit.The conditional expressions group right-to-left.The condition will always be either T or F. So the condition will never be a digit.The result of the expression ...

Read More

order_of_key() in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 939 Views

In this tutorial, we will be discussing a program to understand order_of_key() in C++.The function order_of_key() takes in a key and returns the number of elements which are less than the key provided as parameter in an ordered set.Example#include using namespace std; #include #include #include #include using namespace __gnu_pbds; using namespace std; //initializing ordered set typedef tree    ordered_set; int main(){    ordered_set mySet;    mySet.insert(5);    mySet.insert(2);    mySet.insert(6);    mySet.insert(4);       cout

Read More

Before and After Puzzle in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 221 Views

Suppose we have a list of phrases, generate a list of Before and After puzzles. Here a phrase is a string that consists of lowercase letters and spaces only. No space will be there at the starting and ending positions. There are no consecutive spaces in a phrase.The before and after puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase. We have to find the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] ...

Read More

Shortest Distance to Target Color in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 307 Views

Suppose we have an array color, in which there are three colors: 1, 2 and 3. We have given some queries. Each query consists of two integers i and c, we have to find the shortest distance between the given index i and the target color c. If there is no solution, then return -1. So if the colors array is like [1, 1, 2, 1, 3, 2, 2, 3, 3], and the queries array is like [[1, 3], [2, 2], [6, 1]], the output will be [3, 0, 3]. This is because the nearest 3 from index 1 is ...

Read More

multiset size() in C++ STL with Examples

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 202 Views

In this tutorial, we will be discussing a program to understand multiset size() in C++ STL.The function size() returns the number of elements present into a given container.Example#include using namespace std; int main(){    multiset s;    s.insert(10);    s.insert(13);    cout

Read More
Showing 13611–13620 of 21,090 articles
Advertisements