Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 232 of 377

Sum of Subsequence Widths in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 233 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

Maximum Frequency Stack in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 494 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

Orderly Queue in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 273 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

Minimum Moves to Equal Array Elements in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have an array of size n, we have to find the minimum number of moves required to make all array elements the same, where a move means incrementing n - 1 elements by 1.So, if the input is like [3, 2, 3, 4], then the output will be 4.To solve this, we will follow these steps −n := size of numsif n is same as 0, then −return 0sort the array numsans := 0for initialize i := 0, when i < n, update (increase i by 1), do −ans := ans + nums[i] - nums[0]return ansExample Let us see ...

Read More

Numbers At Most N Given Digit Set in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 235 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

Assign Cookies in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 803 Views

Suppose we are trying to distribute some cookies to children. But, we should give each child at most one cookie. Now each child i has a greed factor gi, which is the minimum size of a cookie that the child will be content with; and each cookie j has a size sj. When sj >= gi, we can assign the cookie j to the child i, and the child i will be content. Our goal is to maximize the number of content children and output the maximum number.So, if the input is like [1, 2], [1, 2, 3], then the ...

Read More

Valid Permutations for DI Sequence in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 827 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

Super Palindromes in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 583 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

Number of Music Playlists in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 584 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

Construct the Rectangle in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 403 Views

Suppose we have a specific rectangular web page area, our job is to design a rectangular web page, whose length L and width W that satisfies the following requirements −The area of the web page must equal to the given target area.The width W should not be larger than the length L, and L >= W.The difference between L and W should be as small as possible.So, if the input is like 4, then the output will be [2, 2], as the target area is 4, and all the possible ways to construct it are [1, 4], [2, 2], [4, ...

Read More
Showing 2311–2320 of 3,768 articles
« Prev 1 230 231 232 233 234 377 Next »
Advertisements