Found 26504 Articles for Server Side Programming

Circular Permutation in Binary Representation in C++

Arnab Chakraborty
Updated on 02-May-2020 11:32:16

241 Views

Suppose we have 2 integers n and start. Our task is return any permutation p of (0, 1, 2....., 2^n -1) as follows −p[0] = startp[i] and p[i+1] differ by only one bit in their binary representation.p[0] and p[2^n -1] must also differ by only one bit in their binary representation.So if the input is like n = 2 and start = 3, then the returned array will be [3, 2, 0, 1], these are [11, 10, 00, 01]To solve this, we will follow these steps −ans is an arrayfor i in range 0 to 2^ninsert start XOR i XOR ... Read More

Replace the Substring for Balanced String in C++

Arnab Chakraborty
Updated on 02-May-2020 11:27:44

482 Views

Suppose we have a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string will be balanced if each of its characters appears n/4 times where n is the length of the string. Find the minimum length of the substring that can be replaced with any other string of the same length to make the original string is balanced. So if s = “QQWE”, then output will be 1. This is because we can replace Q to R, so that “RQWE”, that is balanced.Return 0 if the string is already balanced.To solve this, we will follow ... Read More

K-Concatenation Maximum Sum in C++

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

308 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

228 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

334 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

788 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

339 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

515 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

Advertisements