Articles on Trending Technologies

Technical articles with clear explanations and examples

Position after taking N steps to the right and left in an alternate manner in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 703 Views

In this problem, we are given three integers N, A and B. there is a person who is standing at coordinate 0 movesA step towards the right and then B step towards left. Then again right. Our task is to print the final position of the element after N moves.Let’s take an example to understand the problem, Input − N = 4, A = 3, B = 1Output −Explanation −1st move -> right 3, +3 2nd move -> left 1, -1 3rd move -> right 3, +3 4th move -> left 1, -1. Position after 4 moves, +3-1+3-1 = 4.To ...

Read More

Remove All Adjacent Duplicates in String II in C++

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

Suppose a string s is given, a k duplicate removal consists of choosing k adjacent and equal letters from string s and removing them causing the left and the right side of the deleted substring to concatenate together. We will repeatedly make k duplicate removals on the given string s until we cannot change any remaining. We have to find the final string after all such duplicate removals have been made. So if the input is like s = “deeedbbcccbdaa”, and k = 3, then the output will be “aa”, at first delete the “eee” and “ccc” and we will ...

Read More

Boats to Save People in C++

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

Suppose we have an array called people. Now the i-th person has weight people[i], and each boat can carry a maximum weight of limit. If each boat carries at most 2 people at the same time, provided the sum of the weight of those people is at most limit. We have to find the minimum number of boats to carry every given person. So if the input is like [3,2,1,2], and limit is 3, then we need three boats. [(1,2), (2), (3)].To solve this, we will follow these steps −sort the people arrayi := 0, j := size of people array – 1, ret := 0while i

Read More

Stepping Numbers in C++

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

Suppose we have two integers low and high, we have to find and show a sorted list of all the Stepping Numbers in the range [low, high] inclusive. A Stepping Number is an integer means all of its adjacent digits have an absolute difference of exactly 1. For example, 321 is a Stepping Number but 421 is not. So if the input is like low := 0 and high := 21, then the result will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 21]To solve this, we will follow these steps −create one array tempmake ...

Read More

Swapping of subranges from different containers in C++

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

In this tutorial, we will be discussing a program to understand swapping of subranges of different containers in C++.For this we will be provided with vectors and lists, and we need to swap some of their elements.Example#include #include #include #include using namespace std; int main(){    vector v = { -10, -15, -30, 20, 500 };    list lt = { 10, 50, 30, 100, 50 };    swap_ranges(v.begin(), v.begin() + 3, lt.begin());    for (int n : v)       cout

Read More

Populate Inorder Successor for all nodes in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 325 Views

In this problem, we are given a tree. The structure contains a pointer next. Our task is to populate this pointer with the inorder successor of the node.struct node {    int value;    struct node* left;    struct node* right;    struct node* next; }All the next pointer are set to NULL and we have to set the pointer to the inorder successor of the node.Inorder traversal − This traverses in the following form, Left node -> root Node -> right node.Inorder successor − the inorder successor is the node that comes after the current node is the inorder ...

Read More

Path with Maximum Gold in C++

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

Suppose in a gold mine grid of size m * n, each cell in this mine has an integer representing the amount of gold in that cell, 0 means that is empty. We have to find the maximum amount of gold that you can collect under the conditions −Every time we are pointing a cell we will collect all the gold in that cell.From our position we can walk one step to the left, right, up or down.We cannot visit the same cell more than once.Never visit a cell with 0 gold.So if the input is like [[0, 6, 0], ...

Read More

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 713 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

Menu Driven C++ Program for a Simple Calculator

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 1K+ Views

In this tutorial, we will be discussing a program to create a menu driven program for a simple calculator.This program will give user the ability to choose among the following mathematical operations − addition, subtraction, multiplication, division, HCF and LCM.Example#include using namespace std; //displaying the menu void menu(){    cout

Read More

Dice Roll Simulation in C++

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

Suppose a die simulator generates a random number from 1 to 6 for each roll. We want to introduced a constraint to the generator such that it cannot roll the number i more than rollMax[i] (1-indexed) consecutive times. Consider we have an array of integers rollMax and an integer n, we have to return the number of distinct sequences that can be obtained with exact n rolls. The two sequences are considered different if at least one element differs from each other. So if n is 2, then rollMax = [1, 1, 2, 2, 2, 3], then the output will ...

Read More
Showing 27711–27720 of 61,297 articles
Advertisements