Articles on Trending Technologies

Technical articles with clear explanations and examples

Populating Next Right Pointers in Each Node in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 309 Views

Suppose we have a complete binary tree, where each node has following fields: (data, left, right, next), the left will point to left subtree, right will point to right subtree, and the next pointer will point to the next node. If there is no node in the right hand side, then that will be null. So initially each next pointer is set to null, we have to make the links. Suppose the tree is like the first one, it will be converted to the next node −To solve this, we will follow these steps −set pre := root, nextPre := ...

Read More

Sort Integers by The Power Value in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 283 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

Design a Stack With Increment Operation in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 751 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

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 693 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

Time Needed to Inform All Employees in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 501 Views

Suppose we have a company has n employees with a unique ID for each employee. These IDs are ranging from 0 to n - 1. The head of the company has is the one with headID. Each employee has one direct manager given in the manager array where manager[i] is the direct manager of the i-th employee, manager[headID] = -1. Also it's guaranteed that the subordination relationships have a tree-like structure. Here the head of the company wants to inform all the employees of the company of an urgent piece of news. He can inform his direct subordinates and they ...

Read More

Bulb Switcher III in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 477 Views

Suppose we have a room with n bulbs, these are numbered from 1 to n, arranged in a row from left to right. Initially, all the bulbs are turned off. At moment k (for k in range 0 to n - 1), we turn on the light[k] bulb. A bulb changes color to blue only if it is on and all the previous bulbs (to the left) are turned on too. We have to find the number of moments in which all turned on bulbs is blue. So this is an example −The output will be 3 as the moments ...

Read More

Longest ZigZag Path in a Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 956 Views

Suppose we have a binary tree root, a ZigZag path for a binary tree is defined as follow −Choose any node in the binary tree and a direction (either right or left).If the current direction is right then moving towards the right child of the current node otherwise move towards the left child.Then change the direction from right to left or vice versa.Repeat the second and third steps until we can't move in the tree.Here the Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0). We have to find ...

Read More

Find the Longest Substring Containing Vowels in Even Counts in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 392 Views

Suppose we have the string s, we have to find the size of the longest substring containing each vowel an even number of times. That is, 'a', 'e', 'i', 'o', and 'u' must appear an even number of times. So if the string is like “helloworld”, then the output will be 8.To solve this, we will follow these steps −ret := 0, define two maps m and cnt, set m[“00000”] := -1store vowels into vowels arrayfor i in range 0 to size of sx := s[i], and ok := falseincrease cnt[x] by 1, set temp := empty stringfor k in ...

Read More

Linked List in Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 3K+ Views

Suppose we have a binary tree root and a linked list with a head as the first node. We have to return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise False. So if the tree is like −And the linked list is [1, 4, 2, 6], then the output will be true.To solve this, we will follow these steps −Define a map dpDefine a method called solve(), this will take head, root, and flagif the head is null, then return true, or if the ...

Read More

Find All Anagrams in a String in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 29-Apr-2020 2K+ Views

Suppose we have a string s and a non-empty string p, we have to find all the start indices of p's anagrams in s. The strings consist of lowercase letters only and the length of both strings s and p will not be larger than 20 and 100. So for example, if s: "cbaebabacd" p: "abc", then the output will be [0, 6], at index 0, it is “cba”, and another is “bac”, these are the anagrams of “abc”.To solve this, we will follow these steps −Define a map m, n := size of s, set left := 0, right ...

Read More
Showing 47661–47670 of 61,248 articles
Advertisements