RLE Iterator in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:03:20

321 Views

Suppose we have to create an iterator that iterates through a run-length encoded sequence. Here the iterator is initialized by calling RLEIterator(int[] A), where A is a run-length encoding of a sequence. So we can say that for all even i, A[i] tells us the number of times that the non-negative integer value A[i+1] is repeated in the sequence. Here iterator supports one function −next(int n): This function exhausts the next n elements (n >= 1) and returns the last element exhausted in this way. So if there is no element left to exhaust, next returns -1 instead.Suppose we start ... Read More

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

Maximum Side Length of a Square with Sum Less Than or Equal to Threshold in C++

Arnab Chakraborty
Updated on 30-Apr-2020 10:59:54

223 Views

Suppose we have a m x n matrix mat and an integer threshold. We have to the maximum side-length of a square with a sum less than or equal to the given threshold or return 0 if there is no such square. So if the input is like −113243211324321132432113243211324321132432And threshold is 4, then output will be 2, as there are two squares of side length 2, so max is 2To solve this, we will follow these steps −Define a method called ok, this will take x and matrix m and threshold thset curr := 0for i in range x – ... Read More

Bitwise ORs of Subarrays in C++

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

828 Views

Suppose we have an array A of non-negative integers. For every (contiguous) subarray say B = [A[i], A[i+1], ..., A[j]] (with i

Earliest Moment When Everyone Becomes 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

Possible Bipartition in C++

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

230 Views

Suppose we have a set of N people (they are numbered 1, 2, ..., N), we would like to split everyone into two subgroups of any size. Now each person may dislike some other people, and they should not go into the same group. So, if dislikes[i] = [a, b], it indicates that it is not allowed to put the people numbered a and b into the same group. We have to find if it is possible to split everyone into two groups in this way.So if the input is like N = 4 and dislike = [[1, 2], [1, ... Read More

Remove Covered Intervals in C++

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

320 Views

Suppose we have a list of intervals, we have to remove all intervals that are covered by another interval in the list. Here Interval [a,b) is covered by interval [c,d) if and only if c

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

Spiral Matrix III in C++

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

430 Views

Suppose we have a 2 dimensional grid with R rows and C columns, we start from (r0, c0) facing east. Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column. We will walk in a clockwise spiral shape to visit every position in this grid. When we are at the outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.). We have to find a list of coordinates representing the positions ... 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

Advertisements