Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 232 of 377
Sum of Subsequence Widths in C++
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 MoreMaximum Frequency Stack in C++
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 MoreOrderly Queue in C++
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 MoreMinimum Moves to Equal Array Elements in C++
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 MoreNumbers At Most N Given Digit Set in C++
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 MoreAssign Cookies in C++
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 MoreValid Permutations for DI Sequence in C++
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 MoreSuper Palindromes in C++
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 MoreNumber of Music Playlists in C++
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 MoreConstruct the Rectangle in C++
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