Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 137 of 377

Nth Magical Number in C++

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

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 More

Profitable Schemes in C++

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

Number of Segments in a String in C++

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

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 More

Super Egg Drop in C++

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

Sum of Subsequence Widths in C++

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

Number of Boomerangs in Python

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

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 More

Maximum Frequency Stack in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 484 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 266 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 215 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
Showing 1361–1370 of 3,768 articles
« Prev 1 135 136 137 138 139 377 Next »
Advertisements