Minimum Swaps to Make Sequences Increasing in C++

Arnab Chakraborty
Updated on 02-May-2020 09:14:29

370 Views

Suppose we have two integer sequences A and B of the same non-zero length. We can swap elements A[i] and B[i]. We have to keep in mind that both elements are in the same index position in their respective sequences. After completing some number of swaps, A and B are both strictly increasing. We have to find the minimum number of swaps to make both sequences strictly increasing.So if the input is like A = [1, 3, 5, 4] and B = [1, 2, 3, 7], then the answer will be 1, if we swap A[3] with B[3], then the ... Read More

All Paths from Source to Target in C++

Arnab Chakraborty
Updated on 02-May-2020 09:11:33

539 Views

Suppose we have a directed, acyclic graph with N nodes. We have to find all possible paths from node 0 to node N-1, and return them in any order. The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.So if the input is like [[1, 2], [3], [3], []], then the output will be [[0, 1, 3], [0, 2, 3]].To solve this, we will follow these steps −Make one 2d array called resDefine a method called solve, this will take ... Read More

Number of Subarrays with Bounded Maximum in C++

Arnab Chakraborty
Updated on 02-May-2020 09:09:25

239 Views

Suppose we have an array A of positive integers, and two positive integers L and R are also given. We have to find the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least L and at most R. So if A = [2, 1, 4, 3] and L = 2 and R = 3, then output will be 3 as there are three sub arrays that meet the requirements. So these are [2], [2, 1], [3].To solve this, we will follow these steps −ret := 0, dp := 0, ... Read More

Custom Sort String in C++

Arnab Chakraborty
Updated on 02-May-2020 09:07:20

577 Views

Suppose we have S and T two strings these are composed of lowercase letters. In S, no letter occurs more than once. S was sorted in some custom order previously. We have to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x will occur before y in the returned string.So if the S = “cba” and T = “abcd”, then the output will be “cbad”. Here "a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", ... Read More

Domino and Tromino Tiling in C++

Arnab Chakraborty
Updated on 02-May-2020 09:05:03

506 Views

Suppose we have two types of shapes, Domino and Tromino. They can be rotated like below −In a tiling, every square must be covered by a tile. Here two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.Given N, then we have to find in how many ways we can tile 2xN board? So if the input is 3, then the output will be 5. So the arrangements can be [XYZ XXZ XYY XXY XYY] and [XYZ YYZ XZZ ... Read More

Delete and Earn in C++

Arnab Chakraborty
Updated on 02-May-2020 09:02:50

282 Views

Suppose we have an array nums of integers, we can perform some operations on the array. Here in each operation, we pick any nums[i] and delete it to earn nums[i] amount of points. We must delete every element equal to nums[i] - 1 or nums[i] + 1. Initially the point is 0. We have to find the maximum number of points we can earn by applying such operations. So if the input is like [3, 4, 2], then the output will be 6. So this is because, if we delete 4, we will get 4 points, consequently 3 will also ... Read More

Asteroid Collision in C++

Arnab Chakraborty
Updated on 02-May-2020 09:00:25

1K+ Views

Suppose we have an array asteroids of integers representing asteroids in a row. Now for each asteroid, the absolute value represents its size, and the sign represents its direction that can be positive or negative for the right and left respectively. Each asteroid moves at the same speed.We have to find the state of the asteroids after all collisions. When two asteroids meet, the smaller one will explode. If both are the same size, both will explode. Two asteroids moving in the same direction will never meet.So if the input is like [5, 10, -5], then output will be [5, ... Read More

Maximum Length of Pair Chain in C++

Arnab Chakraborty
Updated on 02-May-2020 08:57:28

190 Views

Suppose in the world of Dota2, there are two parties − The Radiant and also the Dire. The Dota2 senate consists of senators coming from two parties. Now the senate wants to form a choice a few change within the Dota2 game. The voting for this alteration may be a round-based procedure. In each round, each senator can exercise one among the two rights −Ban one senator's right − A senator can make another senator lose all his rights during this and every one the subsequent rounds.Announce the victory − If this senator found the senators who still have rights ... Read More

Shopping Offers in C++

Arnab Chakraborty
Updated on 02-May-2020 08:53:56

306 Views

Suppose there is a store, there are some items to sell. Each item has some price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. So we have the list of prices, a set of special offers, and the number we need to buy for each item. The task is to find the lowest price we have to pay for exactly certain items as given, where we could make optimal use of the special offers.Here each special offer is represented in the form of an array, ... Read More

Exclusive Time of Functions in C++

Arnab Chakraborty
Updated on 02-May-2020 08:48:11

331 Views

Suppose on a single threaded CPU, we execute some functions. Now each function has a unique id between 0 and N-1. We will store logs in timestamp order that describe when a function is entered or exited.Here each log is a string written this format: "{function_id}:{"start" | "end"}:{timestamp}". For example, if the string is like "0:start:3" this means that the function with id 0 started at the beginning of timestamp 3. "1:end:2" means the function with id 1 ended at the end of timestamp 2. A function's exclusive time is the number of units of time spent in this function.So ... Read More

Advertisements