Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 143 of 377

Shortest Palindrome in C++

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

Suppose we have a string s. We can convert it to palindrome by adding characters in front of it. We have to find the shortest palindrome, that we can find performing this information. So if the string is like “abcc”, then the result will be − "ccbabcc".To solve this, we will follow these steps −n := size of s, s1 := s, s2 := sReverse the string s2s2 := s concatenate "#" concatenate s2Define an array lps of size same as s2j := 0, i := 1while i < size of s2, do −if s2[i] is same as s2[j], then, ...

Read More

Constrained Subsequence Sum in C++

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

Suppose we have an array called nums and an integer k, we have to find the maximum sum of a non-empty subsequence of that array such that for every two consecutive numbers in the subsequence, nums[i] and nums[j], where i < j, the condition j - i k and first element of dq is same as dp[i - k - 1], thendelete front element from dqdp[i] := maximum of dp[i] and (if dq is empty, then dp[i] + 0, otherwise first element of dp + dq[i])while (not dq is empty and last element of dq < dp[i]), do −delete ...

Read More

Number of Ways to Wear Different Hats to Each Other in C++

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

Suppose there are n people and 40 different types of hats those are labeled from 1 to 40. Now a 2D list is given called hats, where hats[i] is a list of all hats preferred by the i-th person. We have to find the number of ways that the n people wear different hats to each other. The answer may come very large, so return the answer modulo 10^9 + 7.So, if the input is like [[4, 6, 2], [4, 6]], then the output will be 4, as there are 4 different ways to choose, these are [4, 6], [6, ...

Read More

Number of Digit One in C++

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

Suppose we have a number n, We have to count the total number of digit 1 appearing in all non-negative numbers less than or equal to n. So if the input is 15, then the output will be 8, because the numbers containing 1 is [1,10,11,12,13,14,15], there are 8 1s.To solve this, we will follow these steps −ret := 0for initializing i := 1, when i

Read More

Find the Kth Smallest Sum of a Matrix With Sorted Rows in C++

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

Suppose we have one m * n matrix called mat, and an integer k, mat has its rows sorted in nondecreasing order. We can choose exactly one element from each row to form an array. We have to find the Kth smallest array sum among all possible arrays.So, if the input is like mat = [[1, 3, 11], [2, 4, 6]]1311246and k = 5, then the output will be 7, as when we choose one element from each row the first k smallest sums are [1, 2], [1, 4], [3, 2], [3, 4], [1, 6]. here the 5th sum is ...

Read More

Sliding Window Maximum in C++

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

Suppose we have an array called nums, there is a sliding window of size k which is moving from the left of the array to the right. We can only see the k numbers in the window. Each time the sliding window moves to the right side by one position. We have to find the max sliding window. So if the input is like −[1, 3, -1, -3, 5, 3, 6, 8] and k is 3, then the window will be like −Window PositionMax13-1-35368313-1-35368313-1-35368313-1-35368513-1-35368613-1-353688To solve this, we will follow these steps −Define an array ansDefine one double ended queue dqif ...

Read More

Distinct Subsequences in C++ Programming

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

Suppose we have strings S and T. We have to count number of distinct sequences of S which is equal to T.We know that a subsequence of a string is a new string which is formed from the original string by removing some (can be none) of the characters without disturbing the relative positions of the remaining characters. (Like, "ACE" is a subsequence of "ABCDE" while "AEC" is not).If the input strings are “baalllloonnn” and “balloon”, then there will be 36 different ways to select.To solve this, we will follow these steps −n := size of s, m := size ...

Read More

Expression Add Operators in C++

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

Suppose we have a string that holds only digits from 0 to 9. And one target value is given. We have to return all possibilities to add binary operators +, - and * into the digits to get the target values. So if the input is like “232” and the target is 8, then the answer will be [“2*3+2”, “2+3*2”]To solve this, we will follow these steps −Define a method called solve(), this will take index, s, curr, target, temp, mult −if idx >= size of s, then, if target is same as curr, then, insert temp at the end ...

Read More

Find Median from Data Stream in C++

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

Suppose we have a data stream, in that stream some data element may come and join, we have to make one system, that will help to find the median from the data. As we know that the median is the middle data of a sorted list, if it list length is odd, we can get the median directly, otherwise take middle two elements, then find the average. So there will be two methods, addNum() and findMedian(), these two methods will be used to add numbers into the stream, and find the median of all added numbersTo solve this, we will ...

Read More

Remove Invalid Parentheses in C++

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

Suppose we have a string of parentheses. We have to remove minimum number of invalid parentheses and return the well formed parentheses strings, So if the input is like “()(()()”, so the result will be [“()()()”, “()(())”]To solve this, we will follow these steps −Define a method called solve(), this will take pos, left, right, l, r, string array res and string temp.if pos is same as size of s, then, if left is same as 0 and right is same as 0, then, if (m[temp] is non-zero) is false, then, insert temp at the end of resm[temp] := 1returnif ...

Read More
Showing 1421–1430 of 3,768 articles
« Prev 1 141 142 143 144 145 377 Next »
Advertisements