Server Side Programming Articles - Page 1859 of 2650

Path In Zigzag Labelled Binary Tree in Python

Arnab Chakraborty
Updated on 30-Apr-2020 11:00:19

228 Views

Suppose in an infinite binary tree where every node has two children, the nodes are labelled in row order. Now in the odd numbered rows (the first, third, fifth, ...), the labelling is left to right, and in the even numbered rows (second, fourth, sixth, ...), the labelling is right to left. So the tree will be like −So we have given the label of a node in this tree, we have to find the labels in the path from the root of the tree to the node with that label. So if the input is label = 14, then ... Read More

The Earliest Moment When Everyone Become Friends in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:56:38

143 Views

Suppose in a social group, there are N different people, with unique integer ids from 0 to N-1. Here we have a list of logs, where each logs[i] = [time, id_A, id_B] contains a non-negative integer timestamp, and the ids of two different people. Each log is showing the time in which two different people became friends. If A is friends with B, then B is friends with A. Let's say that person A is acquainted with person B if A is friends with B, or A is a friend of someone acquainted with B. We have to find the ... Read More

Broken Calculator in Python

Arnab Chakraborty
Updated on 30-Apr-2020 10:51:35

370 Views

Suppose we have a broken calculator that has a number showing on its display, we can perform only two operations −Double − This will multiply the number on the display by 2, or;Decrement − This will decrease the number that is showing, by 1, Initially, the calculator is displaying the number X. We have to find the minimum number of operations needed to display the number Y.So if the input is like X = 5 and Y is 8, then the output will be 2, as decrement, it once, then double itTo solve this, we will follow these steps −res ... Read More

Complete Binary Tree Inserter in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:50:52

1K+ Views

As we know a complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. We have to write a data structure CBTInserter that is initialized with a complete binary tree and it supports the following operations−CBTInserter(TreeNode root) this initializes the data structure on a given tree with head node root;CBTInserter.insert(int v) will used to insert a TreeNode into the tree with a value node.val = v so that the tree remains complete, and returns the value of the parent of the inserted TreeNode;CBTInserter.get_root() ... Read More

Sum of Subarray Minimums in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:35:55

275 Views

Suppose we have an array of integers A. We have to find the sum of min(B), where B ranges over every (contiguous) subarray of A. Since the answer may be very large, then return the answer in modulo 10^9 + 7. So if the input is like [3, 1, 2, 4], then the output will be 17, because the subarrays are [3], [1], [2], [4], [3, 1], [1, 2], [2, 4], [3, 1, 2], [1, 2, 4], [3, 1, 2, 4], so minimums are [3, 1, 2, 4, 1, 1, 2, 1, 1, 1], and the sum is 17.To solve ... Read More

All Possible Full Binary Trees in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:34:58

347 Views

Suppose a full binary tree is a binary tree where each node has exactly 0 or 2 children. So we have to find a list of all possible full binary trees with N nodes. Each node of each tree in the answer must have node.val = 0. The returned trees can be in any order. So if the input is 7, then the trees are −To solve this, we will follow these steps −Define a map m of integer type key and tree type value.define a method called allPossibleFBT(), this will take N as inputis N is 1, then create ... Read More

Smallest Subtree with all the Deepest Nodes in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:33:30

171 Views

Suppose we have a binary tree rooted at root, the depth of each node is the shortest distance to the root. Here a node is deepest if it has the largest depth possible among any node in the entire tree. The subtree of a node is that node, plus the set of all descendants of that node. We have to find the node with the largest depth such that it contains all the deepest nodes in its subtree. So if the tree is like −Then the deepest subtree will be −To solve this, we will follow these steps −Define a ... Read More

Task Scheduler n C++

Arnab Chakraborty
Updated on 30-Apr-2020 08:47:54

4K+ Views

Suppose we have a char array representing tasks CPU need to do. This contains uppercase letters A to Z where different letters represent different tasks. The tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one job or just be idle. However, there is a non-negative cooling interval called n that means between two same tasks, there must be at least n intervals that CPU are doing different tasks or just be idle. We have to find the least number of intervals the CPU will take to finish ... Read More

Find All Anagrams in a String in C++

Arnab Chakraborty
Updated on 29-Apr-2020 13:07:56

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

Insert Delete GetRandom O(1) in Python

Arnab Chakraborty
Updated on 29-Apr-2020 12:34:50

571 Views

Suppose we have a data structure that supports all following operations in average O(1) time.insert(val) − This will Insert an item val to the set if that is not already present.remove(val) − This will remove an item val from the set if it presents.getRandom − This will return a random element from current set of elements. Each element must have the same probability of being returned.To solve this, we will follow these steps −For the initialization, define a parent map, and elements arrayFor the insert() function, it will take val as inputif val is not present in parent map, or ... Read More

Advertisements