C++ Articles

Page 70 of 597

Design Hit Counter in C++

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

Suppose we want to design a hit counter which counts the number of hits received in the past 5 minutes. There will be a function and that accepts a timestamp parameter in the second unit and we may assume that calls are being made to the system in chronological order (so, the timestamp is monotonically increasing). We also assume that the earliest timestamp starts at 1.It is possible that several hits arrive roughly at the same time.So we will call the hit() function to hit and getHits() function to get the count of hits.To solve this, we will follow these ...

Read More

Find Leaves of Binary Tree in C++

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

Suppose we have a binary tree. We will collect and remove all leaves and repeat until the tree is empty.So, if the input is likethen the output will be [[4, 5, 3], [2], [1]]To solve this, we will follow these steps −Define one map szDefine one 2D array retDefine a function dfs(), this will take node, if node is null, then −sz[val of node] := 1 + maximum of dfs(left of node) and dfs(right of node)if size of ret < sz[val of node], then −Define an array tempinsert temp at the end of retinsert val of node at the end ...

Read More

Plus One Linked List in C++

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

Suppose we have a non-negative integer represented as non-empty a singly linked list of digits, now we have to plus one to the integer. We may assume the integer do not contain any leading zero, except the number 0 itself. In the linked list the most significant digit is at the head of the list.So, if the input is like [1, 2, 3], then the output will be [1, 2, 4]To solve this, we will follow these steps −if head is null, then −return headcurr = headreq = NULLwhile curr is non-zero, do −if val of curr is not equal ...

Read More

Design Phone Directory in C++

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

Suppose we want to design a Phone Directory which supports the following operations −get − This will provide a number that is not assigned to anyone.check − This will check whether a number is available or not.release − This will recycle or release a number.Using the initializer, we can initialize n numbers at firstTo solve this, we will follow these steps −Define one set sDefine one queue availableThe initializer will take maxNumbers.N := maxNumbersfor initialize i := 0, when i < N, update (increase i by 1), do −insert i into availableDefine a function get()if size of available is same ...

Read More

Sequence Reconstruction in C++

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

Suppose we have to check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. The original sequence is a permutation of the integers from 1 to n, and n in range 1 ≤ n ≤ 10^4. Here the reconstruction means making a shortest common supersequence of the sequences in seqs. We have to check whether there is only one sequence that can be reconstructed from seqs and it is the original sequence.So, if the input is like org = [1, 2, 3], seqs = [[1, 2], [1, 3]], then the output will be false, because ...

Read More

Find Permutation in C++

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

Suppose we have a secret signature consisting of character 'D' and 'I'. 'D' denotes the decreasing relationship between two numbers, 'I' denotes increasing relationship between two numbers. And the secret signature was constructed by a special integer array, which contains uniquely all the different number from 1 to n.For example, the secret signature "DI" can be constructed from an array like [2, 1, 3] or [3, 1, 2], but not be constructed using array like [3, 2, 4] or [2, 1, 3, 4], which are both illegal constructing special string that can't represent the "DI" secret signature.Now we have to ...

Read More

The Maze in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 4K+ Views

Suppose there is a ball in a maze with empty spaces and walls. Now the ball can go through empty paths by rolling any direction like up, down, left or right, but it won't stop rolling until hitting a wall. When the ball stops, it could choose the next direction.We have to start position of ball, the destination and the maze, we have to check whether the ball could stop at the destination. The maze is represented by one 2D array. Here 1 indicates the wall and 0 indicates the empty space. The borders of the maze are all walls. ...

Read More

Inorder Successor in BST II in C++

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

Suppose we have a node in a binary search tree, we have to find the in-order successor of that node in the BST. If there is no in-order successor, return null. As we know that the successor of a node is the node with the smallest key greater than value of node.We will have direct access to the node but not to the root of the tree. Here each node will have a reference to its parent node. Below is the definition for Node −class Node {    public int val;    public Node left;    public Node right;   ...

Read More

Lonely Pixel I in C++

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

Suppose we have a picture consisting of black and white pixels, we have to find the number of black lonely pixels. Here the picture is represented by a 2D char array consisting of 'B' and 'W', for the black and white pixels respectively.A black lonely pixel is actually 'B' that located at a specific position where the same row and same column don't have any other black pixels.If the input is like −WWBWBWBWWOutput will be 3. Because all the three 'B's are black lonely pixels.To solve this, we will follow these steps −n := size of picturem := (if n ...

Read More

Count number of pairs (A &lt;= N, B &lt;= N) such that gcd (A , B) is B in C++

Sunidhi Bansal
Sunidhi Bansal
Updated on 11-Mar-2026 755 Views

We are given an input N. The goal is to find all pairs of A, B such that 1

Read More
Showing 691–700 of 5,961 articles
« Prev 1 68 69 70 71 72 597 Next »
Advertisements