Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 246 of 377

Partition Labels in C++

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

Suppose we have a string S of lowercase letters is given. We will partition this string into as many parts as possible so that each letter appears in at most one part, finally return a list of integers representing the size of these parts. So if the string is like “ababcbacadefegdehijhklij”, output is [9, 7, 8], because the partitions are “ababcbaca”, “defegde”, “hijhklij”. So this is a partition so that each letter occurs in at most one part. A partition like "ababcbacadefegde", "hijhklij" is not correct, because it splits S into less parts.To solve this, we will follow these steps ...

Read More

Minimum Cost to Connect Sticks in C++

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

Suppose we have some sticks with positive integer lengths. We can connect any two sticks of lengths X and Y into one stick by paying a cost of X + Y. This will be performed until there is one stick remaining. We have to find the minimum cost of connecting all the given sticks into one stick in this way. So if the stack is [2, 4, 3], then the output will be 14.To solve this, we will follow these steps −Define a max heap priority queue pqinsert all elements of s into pqans := 0while pq has more than ...

Read More

Maximum Subarray Sum with One Deletion in C++

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

Suppose we have an array of integers; we have to find the maximum sum for a non-empty subarray (contiguous elements) with at most one element deletion. In other words, we can say that we want to choose a subarray and optionally delete one element from it so that there is still at least one element left and the sum of the remaining elements is maximum possible. We have to keep in mind that the subarray needs to be non-empty after deleting one element. So if the input is like [1, -2, 0, 3], then the output will be 4. So ...

Read More

Two Sum BSTs in C++

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

Suppose we have two binary search trees, we have to return True iff there is a node in the first tree and a node in the second tree and sum of these nodes is a given integer target. So if the tree is like −and target is 5, then the result is true.To solve this, we will follow these steps −Define a map sdefine a method called check(), this will take node, target and nodeNumber, this will work as follows −if node is valid, then return falsecurr := value of node, req := target – currif req is present in ...

Read More

Longest Arithmetic Subsequence of Given Difference in C++

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

Suppose we have an integer array arr and an integer difference, we have to find the length of the longest subsequence in arr which is an arithmetic sequence such that the difference between adjacent elements in the subsequence is same as the difference. So if the input is like [1, 5, 7, 8, 5, 3, 4, 2, 1] and difference is -2, then the output will be − 4, as the longest arithmetic sequence is [7, 5, 3, 1]To solve this, we will follow these steps −Define a map mn := size of array arr, set ans := 0for i ...

Read More

Binary Search Tree Iterator in C++

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

Suppose we want to make one iterator for binary tree. There will be two methods. The next() method to return the next element, and hasNext() method to return Boolean value, that will indicate that the next element is present or not. So if the tree is like −And the sequence of function calls are [next(), next(), hasNext(), next(), hasNext(), next(), hasNext(), next(), hasNext()]. The output will be [3, 7, true, 9, true, 15, true, 20, false]To solve this, we will follow these steps −There are two methods next and hasNext, The next() method will be like −curr := stack top ...

Read More

Design A Leaderboard in C++

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

Suppose we have to design a Leaderboard class, there are three different functions −addScore(playerId, score) − This will update the leaderboard by adding score to the given player's score. When there is no such player with given id in the leaderboard, add him to the leaderboard with the given score.top(K) − This will return the score sum of the top K players.reset(playerId) − This will reset the score of the player with the given id to 0. It is guaranteed that the player was added to the leaderboard before calling this function.Initially, the leaderboard should empty.If we perform the operations ...

Read More

Binary Tree Right Side View in C++

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

Suppose we have a binary tree, if we see the tree from right side, then we can see some elements of it. we have to display those elements. So if the tree is like −To solve this, we will follow these steps −We will create one helping method for dfs. This will take tree_node, an array to hold answers, and level. The level is initially 0. The dfs will work like below −if node is null, then returnif level = length of the answer array, then insert value of node into the ans arraydfs(right of the node, ans, level + ...

Read More

Tree Diameter in C++

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

Suppose we have an undirected tree; we have to find its diameter − the number of edges in the longest path in that tree is the diameter of that tree. Here tree is given as an edge list where edges[i] = [u, v] is a bidirectional edge between nodes u and v. Each node has labels in the set {0, 1, ..., edges.length}. So if the graph is like −The output will be 4.To solve this, we will follow these steps −Define a map ldefine a method called dfs(). this will take v, an array called visited, the graph and ...

Read More

Minimum Swaps to Make Strings Equal in C++

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

Suppose we have two strings s1 and s2 of equal length consisting of letters only "x" and "y". Our task is to make these two strings equal to each other. We can swap any two characters that belong to different strings, which means − swap s1[i] and s2[j]. We have to find the minimum number of swaps required to make s1 and s2 equal, or return -1 if it is impossible to do so. So if the strings are s1 = “xy” and s2 = “yx”, then the output will be 2. If we swap s1[0] and s2[0], s1 = ...

Read More
Showing 2451–2460 of 3,768 articles
« Prev 1 244 245 246 247 248 377 Next »
Advertisements