Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 149 of 377

Letter Tile Possibilities in Python

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

Given a set of letter tiles, we need to find the number of possible non-empty sequences we can make using these tiles. For example, with tiles "AAB", we can form 8 different sequences: "A", "B", "AA", "AB", "BA", "AAB", "ABA", "BAA". This problem uses backtracking with frequency counting to generate all possible permutations while avoiding duplicates. Algorithm Approach We use a depth-first search (DFS) approach with the following steps: Count the frequency of each letter in the input tiles For each recursive call, try using each available letter Backtrack by restoring the letter count after ...

Read More

Flip Columns For Maximum Number of Equal Rows in Python

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

Given a binary matrix of 0s and 1s, we can flip any columns to maximize the number of rows with equal values. Flipping a column changes all 0s to 1s and all 1s to 0s in that column. The goal is to find the maximum number of rows that can have identical values after optimal column flips. Original Matrix: 0 0 0 0 ...

Read More

Distant Barcodes in Python

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

In a warehouse with a row of barcodes, we need to rearrange them so that no two adjacent barcodes are the same. For example, if we have [1, 1, 1, 2, 2, 2], we want to output [2, 1, 2, 1, 2, 1]. The strategy is to place the most frequent barcode first at even positions (0, 2, 4...), then fill odd positions with remaining barcodes. Algorithm Steps Count frequency of each barcode Sort barcodes by frequency (ascending order) Place the most frequent barcode at even positions (0, 2, 4...) Place remaining barcodes at odd positions ...

Read More

Previous Permutation With One Swap in Python

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

Finding the previous permutation with one swap means finding the lexicographically largest permutation that is smaller than the given array by swapping exactly two elements. If no such permutation exists, we return the original array. For example, given [3, 2, 1], we can swap positions 1 and 2 to get [3, 1, 2], which is the largest permutation smaller than the original. Algorithm The algorithm follows these steps ? Find the rightmost position where A[left] > A[left + 1] If no such position exists, return the original array Find the largest element to the right ...

Read More

Partition Array for Maximum Sum in Python

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

The Partition Array for Maximum Sum problem involves partitioning an array into contiguous subarrays of length at most K, where each subarray's values are replaced with the maximum value in that subarray. The goal is to maximize the total sum after partitioning. For example, with array [1, 15, 7, 9, 2, 5, 10] and K=3, the optimal partitioning creates subarrays [1, 15, 7], [9], and [2, 5, 10], which become [15, 15, 15], [9], and [10, 10, 10], giving sum 84. Algorithm Approach We use dynamic programming where dp[i] represents the maximum sum for the subarray ending ...

Read More

Smallest Integer Divisible by K in Python

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

Given a positive integer K, we need to find the smallest positive integer N such that N is divisible by K and N contains only the digit 1 (like 1, 11, 111, 1111, etc.). We return the length of such number N, or -1 if no such number exists. For example, if K = 3, the smallest number containing only 1s that is divisible by 3 is 111, so we return 3 (the length). Algorithm To solve this problem, we follow these steps: If K is even or divisible by 5, return -1 (no solution ...

Read More

Clumsy Factorial in Python

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

The clumsy factorial is a variation of the traditional factorial where we use a rotating pattern of operations: multiply (*), divide (/), add (+), and subtract (-) instead of just multiplication. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. The operations follow normal arithmetic precedence rules: multiplication and division are performed before addition and subtraction, and operations of equal precedence are evaluated left to right. We use floor division to ensure integer results. Understanding the Pattern Let's trace through clumsy(10) step by ...

Read More

Minimum Add to Make Parentheses Valid in Python

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

When we have a string of parentheses, we need to find the minimum number of parentheses to add to make it valid. A valid parentheses string follows these rules ? It is the empty string It can be written as XY (X concatenated with Y), where X and Y are valid strings It can be written as (A), where A is a valid string For example, if the string is "()))((", we need to add 4 more parentheses to make it valid. Algorithm We use a stack-based approach to track unmatched parentheses ? ...

Read More

Find and Replace Pattern in Python

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

Finding and replacing patterns in Python involves identifying words that match a given character pattern structure. A word matches a pattern if there exists a character mapping where each unique character in the pattern maps consistently to characters in the word. For example, with pattern "abb" and words ["abc", "deq", "mee", "aqq", "dkd", "ccc"], the words "mee" and "aqq" match because they follow the same structure: first character is unique, second and third characters are the same. Algorithm Approach The solution converts each word and the pattern into a normalized format representing the character structure. Characters are ...

Read More

Construct Binary Tree from Preorder and Postorder Traversal in Python

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

Binary tree construction from preorder and postorder traversals is a fundamental algorithm problem. Given two traversal sequences, we can reconstruct the original binary tree using a stack-based approach. Understanding the Problem Given preorder traversal [1, 2, 4, 5, 3, 6, 7] and postorder traversal [4, 5, 2, 6, 7, 3, 1], we need to construct the binary tree. The preorder visits root first, while postorder visits root last. 1 2 3 ...

Read More
Showing 1481–1490 of 3,768 articles
« Prev 1 147 148 149 150 151 377 Next »
Advertisements