Found 7197 Articles for C++

Tallest Billboard in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:31:45

153 Views

Suppose we are installing a billboard and we want it to have the largest height. The billboard will have two steel supports on both sides. Each support must be of equal height. We also have a collection of rods which can be welded together. So, if we have rods of lengths 1, 2, and 3, we can weld them together to make a support of length 6. We have to find the largest possible height of our billboard installation. If we cannot support the billboard, return 0.So, if the input is like [1, 2, 2, 3, 3, 3, 4], then ... Read More

Largest Component Size by Common Factor in C++

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

176 Views

Suppose we have an array A of unique positive integers, now consider the following graph −There are length of A number of nodes, these are labelled A[0] to A[size of A - 1]; There is an edge between A[i] and A[j] when A[i] and A[j] share a common factor greater than 1. We have to find the size of the largest connected component in the graph.So, if the input is like [4, 6, 15, 35], then the output will be 4To solve this, we will follow these steps −Define an array parentDefine an array rankDefine an array rankif parent[x] is ... Read More

Find the Shortest Superstring in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:21:39

388 Views

Suppose we have an array A of strings, we have to find any smallest string that contains each string in A as a substring. We can also assume that no string in A is substring of another string in A.So, if the input is like ["dbsh", "dsbbhs", "hdsb", "ssdb", "bshdbsd"], then the output will be "hdsbbhssdbshdbsd"To solve this, we will follow these steps −Define a function calc(), this will take a, b, for initialize i := 0, when i < size of a, update (increase i by 1), do −if substring of a from index i to end is at ... Read More

Distinct Subsequences II in C++

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

155 Views

Suppose we have a string S, we have to count the number of distinct subsequences of S. The result can be large, so we will return the answer modulo 10^9 + 7.So, if the input is like "bab", then the output will be 6, as there are 6 different sequences, these are "a", "b, "ba", "ab", "bb", "abb".To solve this, we will follow these steps −Define a function add(), this will take a, b, return ((a mod MOD) + (b mod MOD)) mod MODDefine a function sub(), this will take a, b, return (((a mod MOD) - (b mod MOD)) ... Read More

Three Equal Parts in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:13:31

187 Views

Suppose we have one array A of 0s and 1s, we have to divide the array into 3 non-empty parts such that all of these parts represent the same binary value. If that is possible, return any [i, j] with i+1 < j, such that −A[0], A[1], ..., A[i] is the first part;A[i+1], A[i+2], ..., A[j-1] is the second part, andA[j], A[j+1], ..., A[A.length - 1] is the third part.All three parts have equal binary value. If that is not possible, return [-1, -1].So, if the input is like [0, 1, 0, 1, 1], then the output will be [0, ... Read More

Number of Music Playlists in C++

Arnab Chakraborty
Updated on 04-Jun-2020 09:07:53

509 Views

Suppose we have a music player, that contains N different songs and we want to listen to L songs during our trip. So we have to make a playlist so that it meets these conditions −Every song is played at least onceA song can only be played again only if K other songs have been played.We have to find the number of possible playlists. The answer can be very large, so we will return it modulo 10^9 + 7.So, if the input is like N = 2, L = 3, K = 0, then the output will be 6, as ... Read More

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

771 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

181 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

Advertisements