Found 7197 Articles for C++

K-th Symbol in Grammar in C++

Arnab Chakraborty
Updated on 04-May-2020 14:10:46

1K+ Views

Suppose on the first row, we have a 0. Now in every subsequent row, we look at the previous row and replace each occurrence of 0 by 01, and each occurrence of 1 by 10. Suppose we have N rows and index K, we have to find the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). So if N = 4 and K = 5, then the output will be 1. This is because −Row 1: 0Row 2: 01Row 3: 0110Row 4: 01101001To solve this, we will follow these steps −Suppose the name of ... Read More

Global and Local Inversions in C++

Arnab Chakraborty
Updated on 04-May-2020 14:08:15

375 Views

Suppose we have some permutation A of [0, 1, ..., N - 1], where N is the length of A. Now the number of (global) inversions is the number of i < j with 0 A[j]. And the number of local inversions is the number of i with 0 A[i+1]. We have to return true if and only if the number of global inversions is equal to the number of local inversions. So if the input is like [1, 0, 2], then return true, as there is only one local inversion and one global inversion.To solve this, we ... Read More

Reach a Number in C++

Arnab Chakraborty
Updated on 04-May-2020 13:50:17

228 Views

Suppose you are standing at position 0 on an infinite number line. Now there is a goal at position target. Here in each move, you can either go to the left side or the right side. During the n-th move (starting from 1), you take n steps. We have to find the minimum number of steps required to reach the destination. So if the input is like target = 3, then we need 2 steps. From 0 to 1, from 1 to 3.To solve this, we will follow these steps −target := |target|, cnt := 0while target > 0, decrease ... Read More

Monotone Increasing Digits in C++

Arnab Chakraborty
Updated on 04-May-2020 13:47:09

535 Views

Suppose we have a non-negative integer N, we have to find the largest number that is less than or equal to N with monotone increasing digits. We know that an integer has monotone increasing digits if and only if each pair of adjacent digits’ x and y satisfy x = s[i – 1]increase i by 1if i < nwhile i > 0 and s[i – 1] > s[i], thendecrease i by 1decrease s[i] by 1for j in range i + 1 to ns[j] := ‘9’return s as numberLet us see the following implementation to get better understanding −Example Live Demo#include ... Read More

Knight Probability in Chessboard in C++

Arnab Chakraborty
Updated on 04-May-2020 13:44:19

409 Views

Suppose we have one NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exactly K moves. Here the rows and columns are 0 indexed, so the top-left square is (0, 0), and the bottom-right square is (N-1, N-1).A knight can move in 8 different cells from a cell, that can be shown in this diagram −Each time the knight is to move, it chooses one of eight possible moves randomly. The knight continues moving until it has made exactly K moves or has moved off the chessboard. We have to find the probability ... Read More

Bulb Switcher II in C++

Arnab Chakraborty
Updated on 04-May-2020 13:40:34

293 Views

Suppose there is a room with n lights which are switched on initially and 4 buttons present on the wall. After performing exactly m unknown operations towards buttons, we need to return how many different kinds of status of the n lights could be. So consider n lights are labeled as number [1, 2, 3 ..., n], function of these 4 buttons are as follows −Flip all the lights.Flip lights with even numbers.Flip lights with odd numbers.Flip lights with (3k + 1) numbers, k = 0, 1, 2, ...Now if n = 3 and m = 1, then there will ... Read More

Beautiful Arrangement II in C++

Arnab Chakraborty
Updated on 04-May-2020 13:38:12

198 Views

Suppose we have two integers n and k, we need to construct a list that contains n different positive integers ranging from 1 to n and obeys the following rule −Consider the list is [a1, a2, a3, ... , an], then the list [|a1 - a2|, |a2 - a3|, |a3 - a4|, ... , |an-1 - an|] has exactly k unique integers. So if there are multiple answers, display any of them.If the input is like n = 3 and k = 2, then the result will be [1, 3, 2]. The [1, 3, 2] has three different positive integers ... Read More

Split Array into Consecutive Subsequences in C++

Arnab Chakraborty
Updated on 04-May-2020 13:34:44

469 Views

Suppose we have an array nums that is sorted in ascending order. We have to return true if and only if we can split it into 1 or more subsequences such that each subsequence consists of consecutive integers and whose length at least 3. So if the input is like [1, 2, 3, 3, 4, 4, 5, 5], then the output will be True, as we have two consecutive sequences. These are [1, 2, 3, 4, 5] and [3, 4, 5].To solve this, we will follow these steps −Make a map m and store the frequency of nums into m, ... Read More

Print Binary Tree in C++

Arnab Chakraborty
Updated on 04-May-2020 13:31:26

6K+ Views

Suppose we have to display a binary tree in an m*n 2D string array based on these rules −The row number m should be same as the height of the given binary tree.The column number n should be always an odd number.The value of the root node should be put in the exactly middle of the first row it can be put. The column and the row where the root node resides, will separate the rest space into two parts. These are left-bottom part and right-bottom part. We should print the left subtree in the left-bottom part and print the ... Read More

Find Duplicate Subtrees in C++

Arnab Chakraborty
Updated on 04-May-2020 13:25:29

140 Views

Suppose we have a binary tree. We have to find all duplicate subtrees. So for each kind of duplicate subtrees, we have to return the root node of any one of them. So suppose we have a tree like −The duplicate subtrees are −To solve this, we will follow these steps −Create an array ret, make a map mDefine a recursive method solve(). This will take node as input. This works as −if node is null, then return -1x := value of node as string, then concatenate “#” with it.left := solve(left of node), right := solve(right of node)x := ... Read More

Advertisements