Found 7347 Articles for C++

Find Permutation in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:58:36

253 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

Sequence Reconstruction in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:56:33

155 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

Design Phone Directory in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:52:43

481 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

Range Addition in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:50:25

143 Views

Suppose we have an array of size n and that is initialized with 0's and we also have a value k, we will perform k update operations. Each operation will be represented as triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc. We have to find the modified array after all k operations were executed.So, if the input is like length = 5, updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]], then the output will be [- 2, 0, 3, 5, 3]To solve this, we will follow ... Read More

Plus One Linked List in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:48:16

145 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

Find Leaves of Binary Tree in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:45:51

504 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

Design Hit Counter in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:39:49

515 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

Bomb Enemy in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:38:01

197 Views

Suppose we have a 2D grid, here each cell is either a wall 'W', an enemy 'E' or that is empty '0', We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as placing bomb at the green place, it will kill three enemies.To solve this, we will follow these steps −ret := 0n := row count ... Read More

Sort Transformed Array in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:35:05

184 Views

Suppose we have a sorted array of integer nums and integer values a, b and c. We have to apply a quadratic function of the form f(x) = ax^2 + bx + c to each element x in the array. And the final array must be in sorted order.So, if the input is like nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5, then the output will be [3, 9, 15, 33]To solve this, we will follow these steps −Define function f(), that takes x, a, b, c −return ax^2 + bx + cFrom ... Read More

Line Reflection in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:32:53

383 Views

Suppose we have n points on a 2D plane, we have to check whether there is any line parallel to y-axis that reflect the given points symmetrically, in other words, check whether there exists a line that after reflecting all points over the given line the set of the original points is the same that the reflected ones.So, if the input is like points = [[1, 1], [-1, 1]]then the output will be trueTo solve this, we will follow these steps −Define one set okn := size of pointsminVal := infmaxVal := -inffor initialize i := 0, when i < ... Read More

Advertisements