Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 172 of 377

Last Stone Weight II in C++

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

Suppose we have a collection of rocks, now each rock has a positive integer weight. In each turn, we choose any two rocks and smash them together. If the stones have weights x and y with x = 0, decrease j by 1dp[j] := false when (dp[j] and dp[j – stones[i]]) both are false, otherwise trueif dp[j] is true, then reach := max of reach and jreturn total – (2 * reach)Let us see the following implementation to get better understanding −Example#include using namespace std; class Solution {    public:    int lastStoneWeightII(vector& stones) {       int ...

Read More

Shortest Way to Form String in C++

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

Suppose we have a string, we can form a subsequence of that string by deleting some number of characters (possibly no deletions). So if there is two strings source and target, we have to find the minimum number of subsequences of source such that their concatenation equals target. If the task is impossible, then return -1. So if source is “abc” and target is “abcbc”, then the output will be 2.To solve this, we will follow these steps −Define a string called possible, this will take s and t as inputcreate a map mfor each character c in s mark ...

Read More

Unique Substrings in Wraparound String in C++

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

Suppose we have the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so the value s will look like this − "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".Now we have another string p. Our job is to find out how many unique non-empty substrings of p are present in s. In particular, our input is the string p and we need to output the number of different non-empty substrings of p in the string s.So if the input is like “zab” the output will be 6. There are 6 substrings “z”, “a”, “b”, “za”, “ab”, “zab” of the string “zab” in the string sTo ...

Read More

Minimize Rounding Error to Meet Target in C++

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

Suppose we have an array of prices P [p1, p2..., pn] and a target value, we have to round each price Pi to Roundi(Pi) so that the rounded array [Round1(P1), Round2(P2)..., Roundn(Pn)] sums to the given target value. Here each operation Roundi(pi) could be either Floor(Pi) or Ceil(Pi).We have to return the string "-1" if the rounded array is impossible to sum to target. Otherwise, return the smallest rounding error, which will be (as a string with three places after the decimal) defined as −$\displaystyle\sum\limits_{i-1}^n |Round_{i} (???? ) - ????$So if the input is like [“0.700”, “2.800”, “4.900”], and the ...

Read More

Exclusive Time of Functions in C++

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

Suppose on a single threaded CPU, we execute some functions. Now each function has a unique id between 0 and N-1. We will store logs in timestamp order that describe when a function is entered or exited.Here each log is a string written this format: "{function_id}:{"start" | "end"}:{timestamp}". For example, if the string is like "0:start:3" this means that the function with id 0 started at the beginning of timestamp 3. "1:end:2" means the function with id 1 ended at the end of timestamp 2. A function's exclusive time is the number of units of time spent in this function.So ...

Read More

Maximum Sum Circular Subarray in C++

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

Suppose we have a circular array C of integers represented by A, we have to find the maximum possible sum of a non-empty subarray of C. Also, a subarray may only include each element of the fixed buffer A at most once. If the array is like [1, -2, 3, -2], then the output will be 3. This is because subarray[3] has maximum sum 3.To solve this, we will follow these steps −n := size of vcreate arrays leftSum, leftSumMax, rightSum, rightSumMax all of size nleftSum[0] := v[0], leftSumMax[0] := maximum of 0 and v[0]for i in range 1 to ...

Read More

Validate IP Address in Python

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

Suppose we have a string; we have to check whether the given input is a valid IPv4 address or IPv6 address or neither.The IPv4 addresses are canonically represented in dotted-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), For example, 192.168.254.1; Besides, leading zeros in the IPv4 address is invalid. For example, the address 192.168.254.01 is invalid.The IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, suppose the address is 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid address. ...

Read More

Shopping Offers in C++

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

Suppose there is a store, there are some items to sell. Each item has some price. However, there are some special offers, and a special offer consists of one or more different kinds of items with a sale price. So we have the list of prices, a set of special offers, and the number we need to buy for each item. The task is to find the lowest price we have to pay for exactly certain items as given, where we could make optimal use of the special offers.Here each special offer is represented in the form of an array, ...

Read More

Validate Stack Sequences in C++

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

Suppose we have two sequences pushed and popped with distinct values, we have to find true if and only if this could have been the result of a sequence of the push and pop operations on an initially empty stack. So if the input is push = [1, 2, 3, 4, 5], and pop = [4, 5, 3, 2, 1], then the output will be true. We can use push(1), push(2), push(3), push(4), pop() : 4, push(5), pop() : 5, pop() : 3, pop() : 2, pop() : 1To solve this, we will follow these steps −Create one method called ...

Read More

Convex Polygon in C++

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

Suppose we have a list of points that form a polygon when joined sequentially, we have to find if this polygon is convex (Convex polygon definition). We have to keep in mind that there are at least 3 and at most 10, 000 points. And the coordinates are in the range -10, 000 to 10, 000.We can assume the polygon formed by given points is always a simple polygon, in other words, we ensure that exactly two edges intersect at each vertex and that edges otherwise don't intersect each other. So if the input is like: [[0, 0], [0, 1], ...

Read More
Showing 1711–1720 of 3,768 articles
« Prev 1 170 171 172 173 174 377 Next »
Advertisements