Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 171 of 377

UTF-8 Validation in C++

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

Suppose we have a list integers representing the data. We have to check whether it is valid UTF-8 encoding or not. One UTF-8 character can be 1 to 4-byte long. There are some properties −For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the first n-bits are all 1s, the n+1 bit is 0, followed by n-1 bytes with most significant 2 bits being 10.So the encoding technique is as follows −Character Number RangeUTF-8 octet sequence0000 0000 0000 007F0xxxxxxx0000 0080 0000 07FF110xxxxx 10xxxxxx0000 0800 0000 FFFF1110xxxx 10xxxxxx 10xxxxxx0001 0000 0010 FFFF11110xxx 10xxxxxx 10xxxxxx ...

Read More

Maximum Binary Tree in C++

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

Suppose we have an integer array. All elements in that array is unique. A maximum tree building on this array is defined as follow −The root will hold the maximum number in the array.The left subtree is the maximum tree constructed from left side of the subarray divided by the maximum number.The right subtree is the maximum tree constructed from right side of subarray divided by the maximum number.We have to construct the maximum binary tree. So if the input is like: [3, 2, 1, 6, 0, 5], then the output will be −To solve this, we will follow these ...

Read More

Ternary Expression Parser in C++

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

Suppose we have a string representing arbitrarily nested ternary expressions, we have to calculate the result of the expression. we can always assume that the given expression is valid and only consists of digits 0-9, ?, :, T and F these few characters. (Here T and F represent True and False respectively). There are some properties −The length of the given string must be less than or equal to 10000.Each number will contain only one digit.The conditional expressions group right-to-left.The condition will always be either T or F. So the condition will never be a digit.The result of the expression ...

Read More

Robot Bounded In Circle C++

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

Suppose we have an infinite plane, a robot initially stands at position (0, 0) and faces north. The robot can receive one of three instructions −G − go straight 1 unit;L − turn 90 degrees to the left direction;R − turn 90 degrees to the right direction.The robot performs the instructions given in order, Instructions are repeated forever. We have to check whether there exists a circle in the plane such that the robot never leaves the circle. So if the input is like [GGLLGG], then the answer will be true. from (0, 0) to (0, 2), it will loop ...

Read More

Rotate Function in C++

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

Suppose we have Given an array of integers A and let n is the length of array A. Now assume Bk to be an array obtained by rotating the array A, k positions clock-wise. Here the rotation can be defined as −F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].Now find the maximum value of F(0), F(1), ..., F(n-1).So if the input is like A = [4, 3, 2, 6], then −F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 ...

Read More

Maximum Width of Binary Tree in C++

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

Suppose we have a binary tree, we have to define a function to get the maximum width of the given tree. Here the width of a tree is the maximum width among all levels. We will consider the binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level is actually the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted for the length calculation). So if the tree is like −Then the maximum width ...

Read More

Integer Replacement in C++

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

Suppose we have a positive integer n and we can do these operations as follow −If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.We have to find the minimum number of replacements needed for n to become 1?So if the number is 7, then the answer will be 4, as 7 → 8 → 4 → 2 → 1 or 7 → 6 → 3 → 2 → 1To solve this, we will follow these steps −ret := 0, n := xwhile n > 1if ...

Read More

Binary Tree Pruning in C++

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

Suppose we have the head node root of a binary tree, where additionally every node's value is either a 0 or a 1. We have to find the same tree where every subtree not containing a 1 has been deleted. So if the tree is like −To solve this, we will follow these steps −Define a recursive method solve(), this will take the node. the method will be like −If node is null, then return nullleft of node := solve(left of node)right of node := solve(right of node)if left of node is null and right of node is also null ...

Read More

Longest String Chain in C++

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

Suppose we have a list of words, here each word consists of lowercase letters. So one word word1 is a predecessor of another word word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2. For the example of the predecessor is like, "abc" is a predecessor of "abac". Now a word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. We have to find the longest possible length of ...

Read More

Random Pick Index in C++

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

Suppose we have an array of integers with possible duplicates, we have to pick the index randomly of a given target number. We can assume that the given target number must exist in the array. So if the array is like [1, 2, 3, 3, 3], then pick(3), may return 2, 3, 4 randomly.To solve this, we will follow these steps −ret := - 1, cnt := 1for i in range 0 to size of vif v[i] = target, thenif random number mod cnt = 0, then ret = icnt := cnt + 1return retExample (C++)Let us see the following ...

Read More
Showing 1701–1710 of 3,768 articles
« Prev 1 169 170 171 172 173 377 Next »
Advertisements