Found 26504 Articles for Server Side Programming

Sequential Digits in C++

Arnab Chakraborty
Updated on 02-May-2020 12:32:10

443 Views

Suppose we have an integer, that has sequential digits if and only if each digit in the number is one more than the previous digit. We have to find a sorted list of all the integers in the range [low, high] inclusive that have sequential digits. So if the low = 100 and high = 300, then the output will be [123,234]To solve this, we will follow these steps −create one array resfor i in range 1 to nfor j := 1, until j + i – 1

Find the Smallest Divisor Given a Threshold in C++

Arnab Chakraborty
Updated on 02-May-2020 12:29:56

729 Views

Suppose we have an array of integers called nums and an integer k, that is threshold value, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. We have to find the smallest divisor such that the result mentioned above is less than or equal to threshold value k. For example − if nums = [1, 2, 5, 9] and k = 6, then the output will be 5. We can get the sum as (1+2+5+9) = 17 when the divisor is 1. If the divisor is 4, then ... Read More

Group the People Given the Group Size They Belong To in C++

Arnab Chakraborty
Updated on 02-May-2020 12:27:05

390 Views

Suppose there are n people whose IDs are in range 0 to n - 1 and each person belongs exactly to one group. We have the array groupSizes of length n. This array is indicating that the group size each person belongs to, we have to find the groups there are and the people's IDs each group includes.Suppose the input is like − [3, 3, 3, 3, 3, 1, 3], then the output is [[5], [0, 1, 2], [3, 4, 6]], Other possible solutions can be [[2, 1, 6], [5], [0, 4, 3]] or [[5], [0, 6, 2], [4, 3, ... Read More

Count Square Submatrices with All Ones in C++

Arnab Chakraborty
Updated on 02-May-2020 12:23:44

188 Views

Suppose we a binary matrix, of size m x n. We have to count number of square submatrices, with all 1s. So if the matrix is like −011111110111So there will be 15 squares. 10 squares of single ones, 4 squares of four ones, and 1 square with nine ones.To solve this, we will follow these steps −set ans := 0, n := row count and m := column countfor i in range 0 to m – 1ans := ans + matrix[n – 1, i]for i in range 0 to n – 1ans := ans + matrix[i, m – 1]ans := ... Read More

Number of Burgers with No Waste of Ingredients in C++

Arnab Chakraborty
Updated on 02-May-2020 12:03:33

309 Views

Suppose we have two integers tomatoSlices and cheeseSlices. These are the ingredients of different burgers −Jumbo Burger: 4 tomato slices and 1 cheese slice.Small Burger: 2 Tomato slices and 1 cheese slice.We have to find [total_jumbo, total_small] so that the number of tomatoSlices that are left is equal to 0 and the number of cheeseSlices that are left is also 0. If it is not possible to make the remaining tomatoSlices and cheeseSlices equal to 0 return []. So if the input is tomatoSlices = 16 and chesseSlices = 7, then the output will be [1, 6]. So this indicates, ... Read More

Greatest Sum Divisible by Three in C++

Arnab Chakraborty
Updated on 02-May-2020 12:01:29

456 Views

Suppose we have an array nums of integers, we need to find the maximum possible sum of elements of the given array such that it is divisible by three. So if the input is like [3, 6, 5, 1, 8], then the output will be 18, as the subarray is [3, 6, 1, 8], and the sum is 18, that is divisible by 3.To solve this, we will follow these steps −n := size of nums arraycreate one 2d array dp of size (n + 1) x 3set dp[0, 0] := 0, dp[0, 1] := -inf, dp[0, 2] := inffor ... Read More

Smallest Common Region in C++

Arnab Chakraborty
Updated on 02-May-2020 11:59:13

188 Views

Suppose we have some lists of regions where the first region of each list includes all other regions in that list. basically, if a region X contains another region Y then, X is larger than Y. Also by definition a region X contains itself. So if we have two regions r1 and r2, we have to find the smallest region that contains both of them. So if we have r1, r2 and r3 such that r1 includes r3, it is guaranteed there is no r2 such that r2 includes r3. So if the input is like [["Earth", "North America", "South ... Read More

Encode Number in C++

Arnab Chakraborty
Updated on 02-May-2020 11:55:40

577 Views

Suppose we have a non-negative integer n, and we have to find the encoded form of it. The encoding strategy will be as follows −NumberEncoded number0“”1“0”2“1”3”00”4”01”5”10”6”11”7”000”So if the number is 23, then result will be 1000, if the number is 54, then it will be 10111To solve this, we will follow these steps −Create one method called bin, this will take n and k, this method will act like belowres := empty stringwhile n > 0res := res + the digit of n mod 2n := n /2reverse the number reswhile x > length of resres := prepend 0 with ... Read More

Reconstruct a 2-Row Binary Matrix in C++

Arnab Chakraborty
Updated on 02-May-2020 11:51:17

227 Views

Suppose we have the following details of a matrix with n columns and 2 rows −Matrix elements will be either 0 or 1Sum of elements of the 0-th(upper) row is given as upper.Sum of elements of the 1-st(lower) row is given as lower.Sum of elements in the i-th column(0-indexed) is colsum[i], where colsum is given as an integer array with length n.The task is to reconstruct the matrix with upper, lower and colsum. We have to find it as a 2D integer array. If there are more than one valid solution, any of them will be accepted. If there is ... Read More

Maximum Length of a Concatenated String with Unique Characters in C++

Arnab Chakraborty
Updated on 02-May-2020 11:36:35

426 Views

Suppose we have an array of strings arr. The string s is a concatenation of a sub-sequence of arr which have unique characters. Find the maximum possible length of s. If the input is like [“cha”, “r”, “act”, “ers”], then the output will be 6, possible solutions are “chaers” and “acters”.To solve this, we will follow these steps −make one method called ok() and this will take strings s and t. This will act like belowmake one map xfor i in range 0 to size of sincrease x[s[i]] by 1if x[s[i]] > 1, then return falsefor i in range 0 ... Read More

Advertisements