Found 26504 Articles for Server Side Programming

Super Palindromes in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:03:27

525 Views

Suppose we have a positive integer N, that is said to be a superpalindrome if it is a palindrome, and it is also the square of a palindrome. Now consider we have two positive integers L and R we have to find the number of superpalindromes in the inclusive range of [L, R].So, if the input is like L = 5 and R = 500, then the output will be 3, the superpalindromes are 9, 121, 484.To solve this, we will follow these steps −Define a function helper(), this will take x, m, M, lb, ub, if x > ub, ... Read More

Valid Permutations for DI Sequence in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:00:57

773 Views

Suppose we have a string S. This is a string of characters from the set {'D', 'I'}. (D means "decreasing" and I means "increasing")Now consider a valid permutation is a permutation P[0], P[1], ..., P[n] of integers {0 to n}, such that for all i, it meets these rules:If S[i] == 'D', then P[i] > P[i+1];Otherwise when S[i] == 'I', then P[i] < P[i+1].We have to find how many valid permutations are there? The answer may be very large, so we will return using mod 10^9 + 7.So, if the input is like "IDD", then the output will be 3, ... Read More

Numbers At Most N Given Digit Set in C++

Arnab Chakraborty
Updated on 04-Jun-2020 08:58:56

182 Views

Suppose we have one sorted set of digits D, a non-empty subset of {'1', '2', '3', '4', '5', '6', '7', '8', '9'} except 0. Now, we will write some numbers using these digits, using each digit as many times as we want. So, if D = {'2', '3', '7'}, we may write numbers such as '23', '771', '2372327'.Now we have to find the number of positive integers that can be written that are less than or equal to N.So, if the input is like D = [2, 3, 4, 7], N = 100, then the output will be 20, as ... Read More

Orderly Queue in C++

Arnab Chakraborty
Updated on 04-Jun-2020 08:56:21

229 Views

Suppose there is a string S. All letters in S are in lowercase. Then, we may make any number of moves.Here, in each move, we choose one of the first K letters, and remove it, and place it at the end of the string. We have to find the lexicographically smallest string we could have after any number of moves.So, if the input is like "cabaa" and K = 3, then the output will be "aaabc"To solve this, we will follow these steps −if K > 1, then −sort the array Sreturn Sret := Sn := size of Sfor initialize ... Read More

Maximum Frequency Stack in C++

Arnab Chakraborty
Updated on 04-Jun-2020 08:53:44

420 Views

Suppose we want to implement one stack called FreqStack, Our FreqStack has two functions −push(x), This will push an integer x onto the stack.pop(), This will remove and returns the most frequent element in the stack. If there are more than one elements with same frequency, then the element closest to the top of the stack is removed and returned.So, if the input is like push some elements like 7, 9, 7, 9, 6, 7, then perform the pop operations four times, then the output will be 7, 9, 7, 6 respectively.To solve this, we will follow these steps −Define ... Read More

Sum of Subsequence Widths in C++

Arnab Chakraborty
Updated on 04-Jun-2020 08:50:02

184 Views

Suppose we have an array A of integers, consider all non-empty subsequences of A. For any sequence S, consider the width of S be the difference between the maximum and minimum element of S. We have to find the sum of the widths of all subsequences of A. The answer may be very large, so return the answer modulo 10^9 + 7.So, if the input is like [3, 1, 2], then the output will be 6, this is because the subsequences are like [1], [2], [3], [2, 1], [2, 3], [1, 3], [2, 1, 3] and the widths are 0, ... Read More

Super Egg Drop in C++

Arnab Chakraborty
Updated on 04-Jun-2020 08:47:29

478 Views

Suppose we have given K eggs, and we have a building with N floors from 1 to N. Now each egg is identical in function, and if an egg breaks, we cannot drop it again.There exists a floor F with between 0 and N such that any egg dropped at a floor higher than F will break, and any egg dropped at or below floor F will not break. In each move, we may take an egg and drop it from any floor X. The X is in range 1 to N.Our goal is to know with certainty what the ... Read More

Profitable Schemes in C++

Arnab Chakraborty
Updated on 04-Jun-2020 12:38:44

225 Views

Suppose there is a gang with G people and a list of various crimes they could commit. The i-th crime generates a profit value profit[i] and requires group[i] gang members to participate.If a gang member is participating in one crime, that he can't participate in another crime. Now let us define profitable scheme any subset of these crimes that generates at least P profit, and total number of members participating in that subset of crimes is at most G.We have to find how many schemes can be chosen? The answer may be very large, So return it modulo 10^9 + ... Read More

Minimum Number of Refueling Stops in C++

Arnab Chakraborty
Updated on 04-Jun-2020 08:36:52

557 Views

Suppose there is a car, that travels from a starting position to a destination which is t miles east of the starting position.Now along the way, there are many gas stations. So each station[i] represents a gas station that is station[i][0] miles east of the starting position, and that station has station[i][1] liters of gas.If the car starts with an infinite size of gas tank, which initially has startFuel liters of fuel in it. It uses 1 liter of gas per 1 mile that it drives.When the car reaches one gas station, it may stop and refuel, so now it ... Read More

Shortest Subarray with Sum at Least K in C++

Arnab Chakraborty
Updated on 04-Jun-2020 08:33:51

443 Views

Suppose we have an array A. We have to find the length of the shortest, non-empty, contiguous subarray of A whose sum is at least K. If there is no such subarray, then return -1.So, if the input is like [5, 3, -2, 2, 1] and k = 6, then the output will be 2, as we can see (5+3) >= 6To solve this, we will follow these steps −n := size of Aans := n + 1, j := 0, sum := 0Define one deque dqfor initialize i := 0, when i < n, update (increase i by 1), ... Read More

Advertisements