Found 7197 Articles for C++

K-Concatenation Maximum Sum in C++

Arnab Chakraborty
Updated on 02-May-2020 09:28:30

307 Views

Suppose we have an integer array arr and one integer k, we have to change the array by repeating it k times. So if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].Now we have to find the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0. As the answer may be very large, find the answer modulo 10^9 + 7.So if the input is like [1, -2, 1] and k = 5, ... Read More

Invalid Transactions in C++

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

227 Views

Suppose there are some transactions. A transaction is possibly invalid if −The amount exceeds $1000, or;If it occurs within (and including) 60 minutes of another transaction with the same name in a different city.Here each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction. We have a list of transactions, find a list of transactions that are possibly invalid. So if the input is like ["alice, 20, 800, mtv", "bob, 50, 1200, mtv"], then the answer will be ["bob, 50, 1200, mtv"].To solve this, we will follow these steps ... Read More

As Far from Land as Possible in C++

Arnab Chakraborty
Updated on 02-May-2020 09:21:47

332 Views

Suppose we have one N x N grid containing only values like 0 and 1, where 0 represents water and 1 represents the land, we have to find a water cell such that its distance to the nearest land cell is maximized and return the distance. Here we will use the Manhattan distance − the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|. If no land or water is present in the grid, then return -1.101000101Then the output will be 2, as the cell (1, 1) is as far as possible ... Read More

Camelcase Matching in C++

Arnab Chakraborty
Updated on 02-May-2020 09:17:22

787 Views

Suppose we have a list of queries, and a pattern, we have to return an answer that will be list of booleans, where answer[i] is true if and only if queries[i] matches the pattern. A query word matches a given pattern when we can insert lowercase letters to the pattern word so that it equals the query.So if the input is like ["FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"] and pattern = "FB", then the results will be [true, false, true, false, false].To solve this, we will follow these steps −Define a method called insertNode(), this takes head and string scurr := ... Read More

Minimum Swaps To Make Sequences Increasing in C++

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

337 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

514 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

214 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

545 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

463 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

257 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

Advertisements