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
Articles by Arnab Chakraborty
Page 137 of 377
Nth Magical Number in C++
A number is said to be a magical number, if that number is divisible by A or B. We have to find the Nth magical number. As the answer may very large, we will return it modulo 10^9 + 7.So, if the input is like N = 4, A = 4, B = 3, then the output will be 8To solve this, we will follow these steps −Define a function cnt(), this will take x, A, B,return (x / A) + (x / B) - (x / lcm of A and B)From the main method, do the following −l := 2, r := 1^14, ret := 0while l
Read MoreProfitable Schemes in C++
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 MoreNumber of Segments in a String in C++
Suppose we have a string s. We have to count the number of segments in a string, where a segment is defined to be a contiguous sequence of characters (no whitespace).So, if the input is like "Hello, I love programming", then the output will be 4, as there are 4 segments.To solve this, we will follow these steps −n := 0for initialize i := 0, when i < size of s, update (increase i by 1), do −if s[i] is not equal to white space, then −(increase n by 1)while (i < size of s and s[i] is not equal ...
Read MoreSuper Egg Drop in C++
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 MoreSum 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 MoreNumber of Boomerangs in Python
Suppose we have n points in the plane that are all pairwise distinct. Now a "boomerang" is a tuple of points like (i, j, k) such that the distance between i and j is the same as the distance between i and k. We have to find the number of boomerangs.So, if the input is like [[0, 0], [1, 0], [2, 0]], then the output will be 2, as two boomerangs are [[1, 0], [0, 0], [2, 0]] and [[1, 0], [2, 0], [0, 0]].To solve this, we will follow these steps −counter_of_boomerangs := 0for each point_1 in points array, ...
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 More