Found 26504 Articles for Server Side Programming

Number of Submatrices That Sum to Target in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:09:58

230 Views

Suppose we have a matrix, and a target value, we have to find the number of non-empty submatrices that sum is same as target. Here a submatrix [(x1, y1), (x2, y2)] is the set of all cells matrix[x][y] with x in range x1 and x2 and y in range y1 and y2. Two submatrices [(x1, y1), (x2, y2)] and [(x1', y1'), (x2', y2')] are different if they have some coordinate that is different: like, if x1 is not same as x1'.So, if the input is like010111010and target = 0, then the output will be 4, this is because four 1x1 ... Read More

Longest Duplicate Substring in C++

Arnab Chakraborty
Updated on 04-Jun-2020 11:06:27

660 Views

Suppose we have a string S, consider all duplicated contiguous substrings that occur 2 or more times. (The occurrences may overlap.), We have to find the duplicated substring that has the longest possible length. If there is no such substrings, then return a blank string. As the answer may very large, so return in mod 10^9 + 7.So, if the input is like "ababbaba", then the output will be "bab"To solve this, we will follow these steps −m := 1e9 + 7Define a function add(), this will take a, b, return ((a mod m) + (b mod m)) mod mDefine ... Read More

Escape a Large Maze Python

Arnab Chakraborty
Updated on 04-Jun-2020 11:00:48

434 Views

Suppose we have a grid, there are 1 million rows and 1 million columns, we also have one list of blocked cells. Now we will start at the source square and want to reach the target square. In each move, we can walk to a up, down, left, right adjacent square in the grid that isn't in the given list of blocked cells.We have to check whether it is possible to reach the target square through a sequence of moves or not.So, if the input is like blocked = [[0, 1], [1, 0]], source = [0, 0], target = [0, ... Read More

Stream of Characters in C++

Arnab Chakraborty
Updated on 04-Jun-2020 10:58:38

606 Views

Suppose we want to implement the StreamChecker class as follows −StreamChecker(words) − This is the constructor, this initializes the data structure with the given words.query(letter) − This returns true when for some k >= 1, the last k characters queried (in order from oldest to newest, including this letter just queried) spell one of the words in the given list.So, if the input is like word list = ["ce", "g", "lm"], then call query many times for [a, b, c, e, f, g, h, i, j, k, l, m], then the output will be true for e, g, m, and ... Read More

Numbers With Repeated Digits in C++

Arnab Chakraborty
Updated on 04-Jun-2020 10:53:46

685 Views

Suppose we have a positive integer N, we have to find the number of positive integers less than or equal to N that have at least 1 repeated digit .So, if the input is like 99, then the output will be 9, as we have numbers like 11, 22, 33, 44, 55, 66, 77, 88, 99.To solve this, we will follow these steps −Define a function A(), this will take m, n, ret := 1for initialize i := 0, when i < n, update (increase i by 1), do −ret := ret * m(decrease m by 1)return retFrom the main ... Read More

Grid Illumination in Python

Arnab Chakraborty
Updated on 04-Jun-2020 10:49:36

456 Views

Suppose we have a N x N grid of cells, in each cell (x, y) there is a lamp. Initially, some of the lamps are on. The lamps[i] is the location of the i-th lamp that is on. Each lamp that is on glows every square on its x-axis, y-axis, and both diagonals. Now for the i-th query i.e. queries[i] = (x, y), the answer to the query is 1 if the cell (x, y) is glowed, otherwise 0. After each query (x, y), we turn off any lamps that are at cell (x, y) or are adjacent 8-directionally. Return ... Read More

Minimum Cost to Merge Stones in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:52:30

637 Views

Suppose we have N piles of stones arranged in a row. Here the i-th pile has stones[i] number of stones. A move consists of merging K consecutive piles into one pile, now the cost of this move is equal to the total number of stones in these K number of piles. We have to find the minimum cost to merge all piles of stones into one pile. If there is no such solution then, return -1.So, if the input is like [3, 2, 4, 1] and K = 2, then the output will be 20, this is because, we will ... Read More

Number of Squareful Arrays in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:48:04

310 Views

Suppose we have an array A of positive integers, we can say that array is squareful if for every pair of adjacent elements, their sum is a perfect square. We have to find the number of permutations of A that are squareful. Two permutations A1 and A2 will not be same if and only if there is some index i such that A1[i] not same as A2[i].So, if the input is like [3, 30, 6], then the output will be 2, as we have two permutations like [3, 6, 30], [30, 6, 3].To solve this, we will follow these steps ... Read More

Minimum Number of K Consecutive Bit Flips in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:46:08

248 Views

Suppose we have an array A. This is containing only 0s and 1s, here a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously inverting the bits n the subarray. We have to find the minimum number of K-bit flips required so that there is no 0 in the array. If there is no such possibility, then return -1.So, if the input is like [0, 0, 0, 1, 0, 1, 1, 0] and K = 3, then the output will be 3, as we need to perform the operations three times, on the first try flip ... Read More

Subarrays with K Different Integers in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:44:29

256 Views

Suppose we have an array A of positive integers, we can call a good subarray (contiguous) of A, if the number of different integers in that subarray is exactly K. So, if the array is like [1, 2, 3, 1, 2] has 3 different integers: 1, 2, and 3. We have to find the number of good subarrays of A.So, if the input is like [1, 2, 3, 1, 4] and K = 3, then the output will be 4, as it can form three subarrays with exactly four distinct integers, these are [1, 2, 3], [1, 2, 3, 1], ... Read More

Advertisements