Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 93 of 377

Program to reverse words separated by set of delimiters in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 436 Views

Sometimes we need to reverse words in a string while keeping delimiters in their original positions. This is useful when processing formatted text where the structure must be preserved but the word order needs to be reversed. So, if the input is like s = "Computer/Network:Internet|tutorialspoint" with delimiters ["/", ":", "|"], then the output will be tutorialspoint/Internet:Network|Computer. Algorithm To solve this problem, we follow these steps ? Extract all words (non-delimiter characters) and store them in a list Iterate through the original string character by character If the character is a delimiter, add it directly ...

Read More

Program to reverse inner nodes of a linked list in python

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

Given a linked list and two positions i and j, we need to reverse the nodes from position i to position j (0-indexed). The rest of the nodes remain in their original positions. For example, if we have a linked list [1, 2, 3, 4, 5, 6, 7, 8, 9] with i = 2 and j = 6, the output will be [1, 2, 7, 6, 5, 4, 3, 8, 9]. Algorithm To solve this problem, we follow these steps: Create a dummy head node pointing to the original head Traverse to find the node ...

Read More

Program to evaluate ternary expression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 374 Views

Looking at this article, I can see it's about C++ but labeled as Python. I'll improve the structure, fix the HTML issues, and make it clearer while keeping the C++ content intact. A ternary expression uses the format condition ? value1 : value2. Given a string representing nested ternary expressions with 'T' (True) and 'F' (False), we need to evaluate the final result. Problem Constraints String length ≤ 10000 Conditional expressions group right-to-left Conditions are always 'T' or 'F', never digits Final result is always 'T' or 'F' For example, "T?T?F:T:T" evaluates to "F". ...

Read More

Program to check we can update a list index by its current sum to reach target or not in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 175 Views

Suppose we have a list of numbers called target. We start with a list X of the same length filled with 1s. We can perform the following operation repeatedly: select any index i in X and set X[i] to the current sum of all elements in X. The goal is to check whether X can be transformed into the target list. For example, if target = [5, 9, 3], we start with X = [1, 1, 1] (sum = 3). Update index 2: X = [1, 1, 3] (sum = 5). Update index 0: X = [5, 1, 3] ...

Read More

Program to convert gray code for a given number in python

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

Gray code is a binary numeral system where consecutive numbers differ by exactly one bit. For example, the gray code sequence starts as: 000, 001, 011, 010, 110, 111, 101, 100. Given a number n, we need to find the nth gray code in decimal representation. The key insight is that gray codes follow a recursive pattern where we can find the nth gray code by determining the highest power of 2 less than or equal to n, then recursively solving for the remaining part. Algorithm Steps To convert a number to its corresponding gray code position: ...

Read More

Program to recover shuffled queue of people in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 215 Views

Suppose we have a 2D matrix where each row contains two values [height, count] that indicates a person has given height and there are 'count' number of people in front of them that are at least as tall as them. When this queue is shuffled, we need to recover the original ordering. Problem Understanding Given a shuffled queue representation, we need to reconstruct the original queue order. Each person is represented as [height, count] where count is the number of people in front who are taller or equal in height. Example Input and Output Input ...

Read More

Program to find number of distinct quadruple that forms target sum in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 441 Views

Finding distinct quadruples from four arrays that sum to a target value is a common algorithmic problem. We can solve this efficiently using a hash map to store intermediate sums and reduce the time complexity from O(n⁴) to O(n²). Problem Understanding Given four arrays A, B, C, D and a target sum, we need to find the count of distinct quadruples (i, j, k, l) where A[i] + B[j] + C[k] + D[l] equals the target. For example, with A = [5, 4, 3], B = [8, 4], C = [6, 2], D = [4, 10], target ...

Read More

Program to find state of prison cells after k days in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 424 Views

We need to simulate the state of prison cells after k days. Each cell becomes occupied (1) if both adjacent cells have the same state, otherwise it becomes empty (0). The first and last cells are always empty since they don't have two neighbors. Understanding the Problem Given a binary list representing prison cells and a number k, we need to find the state after k days. The transformation rule is: If a cell has two neighbors with the same state (both 0 or both 1), it becomes occupied (1) Otherwise, it becomes empty (0) First ...

Read More

Program to find number of sublists we can partition so given list is sorted finally in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 230 Views

Suppose we have a list of numbers called nums. We can partition the list into some individual sublists then sort each piece. We have to find the maximum number of sublists we can partition so that nums as a whole is sorted afterwards. So, if the input is like nums = [4, 3, 2, 1, 7, 5], then the output will be 2, as we can sort the sublists like [4, 3, 2, 1] and [7, 5]. Algorithm To solve this, we will follow these steps − count := 0 ...

Read More

Program to find number of ways we can split a palindrome in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 412 Views

Suppose we have a string s, we have to find the number of ways we can partition the string such that each part is a palindrome. So, if the input is like s = "xyyx", then the output will be 3, as we have splits like: ["x", "yy", "x"], ["x", "y", "y", "x"], ["xyyx"]. Approach To solve this, we will use dynamic programming with the following steps − Create a table of size n + 1 initialized with 0, except table[0] = 1 For each position i, check all possible substrings ending at i If ...

Read More
Showing 921–930 of 3,768 articles
« Prev 1 91 92 93 94 95 377 Next »
Advertisements