Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 142 of 377
Find all rectangles filled with 0 in Python
Finding all rectangles filled with 0s in a binary 2D matrix is a common problem in computer vision and image processing. We need to identify the starting and ending coordinates of each rectangular region containing only zeros. The problem requires us to find rectangles that are separated (don't touch each other) but can touch array boundaries. Each rectangle is represented by four coordinates: [start_row, start_col, end_row, end_col]. Input Example Consider this binary matrix where 1 represents filled cells and 0 represents empty cells ? 1011101 1101111 1011001 1011001 1011011 1010000 1110001 1011101 Algorithm ...
Read MoreFind all palindromic sub-strings of a given string - Set 2 in Python
A palindrome is a string that reads the same forwards and backwards. Finding all palindromic sub-strings involves checking every possible substring. This implementation uses the expand around centers approach with fractional positions to handle both odd and even-length palindromes efficiently. How the Algorithm Works The algorithm uses fractional positions (0.0, 0.5, 1.0, 1.5...) to represent centers: Integer positions (0, 1, 2...) represent centers of odd-length palindromes Half positions (0.5, 1.5, 2.5...) represent centers of even-length palindromes For each center, expand outward while characters match Implementation Here's the complete solution that finds all palindromic ...
Read MoreFind all good indices in the given Array in Pytho
Given an array of numbers, we need to find all indices where removing that element results in a good array. A good array is one where at least one element equals the sum of all other elements in the array. The problem uses 1-based indexing, meaning the first element is at index 1, not 0. Understanding the Problem For the array [10, 4, 6, 2]: Removing index 1 (element 10): [4, 6, 2] → 6 = 4 + 2 ✓ Removing index 2 (element 4): [10, 6, 2] → 10 ≠ 6 + 2 ✗ ...
Read MoreFind all distinct palindromic sub-strings of a given String in Python
Finding all distinct palindromic substrings is a common string processing problem. A palindrome is a string that reads the same forwards and backwards. This solution uses Manacher's algorithm to efficiently find all palindromic substrings. So, if the input is like "bddaaa", then the output will be [a, aa, aaa, b, d, dd] Understanding the Algorithm The algorithm follows these key steps: Create a matrix to store palindrome lengths at each position Process the string with padding characters to handle edge cases Use Manacher's algorithm to find all palindromes efficiently Extract distinct palindromic substrings using a ...
Read MoreFind a string such that every character is lexicographically greater than its immediate next character in Python
Sometimes we need to create a string where each character is lexicographically greater than its immediate next character. This creates a strictly decreasing sequence of characters from left to right. Given a number n, we need to generate a lowercase string of length n+1 where each character is lexicographically larger than the character that follows it. Example If the input is n = 15, the output will be "ponmlkjihgfedcba" (16 characters total). Algorithm To solve this problem, we follow these steps ? Calculate extra = n % 26 to find remaining characters after ...
Read MoreFind a sorted subsequence of size 3 in linear time in Pytho
Given an array of N numbers, we need to find three elements such that A[i] < A[j] < A[k] where i < j < k (an increasing subsequence of size 3) in linear O(n) time. If multiple triplets exist, we return any one of them. For example, if the input is [13, 12, 11, 6, 7, 3, 31], the output will be (6, 7, 31). Algorithm Approach We use two auxiliary arrays to track potential elements ? smaller[] − For each position i, stores the index of the smallest element to the left that is ...
Read MoreFind a positive number M such that gcd(N^M,N&M) is maximum in Python
Given a number N, we need to find a positive number M such that gcd(N^M, N&M) is maximized, where ^ is XOR and & is AND operation. The problem asks us to find both M and the maximum GCD value. The key insight is that we need to analyze the binary representation of N to find the optimal M that maximizes the GCD between N XOR M and N AND M. Algorithm The solution follows these steps − If N has no set bits at even positions (all bits at positions 0, 2, 4... are ...
Read MoreFinal state of the string after modification in Python
This problem simulates boxes being pushed in different directions until they reach a stable final state. Given a string where 'R' represents a box pushed right, 'L' represents a box pushed left, and '.' represents empty space, we need to find the final configuration after all movements stop. Problem Understanding Each box marked 'R' pushes subsequent boxes to the right, while boxes marked 'L' push preceding boxes to the left. The simulation continues until no more movement is possible ? Algorithm Approach We use a two-pass algorithm to calculate the net force on each position ? ...
Read MoreLFU Cache in Python
A Least Frequently Used (LFU) cache is a data structure that removes the least frequently accessed item when the cache reaches its maximum capacity. Python's collections.OrderedDict helps track both frequency and insertion order for efficient implementation. LFU Cache Operations The LFU cache supports two main operations: get(key) – Returns the value if the key exists, otherwise returns -1 put(key, value) – Inserts or updates a key-value pair When capacity is reached, the cache removes the least frequently used element before inserting a new one. Implementation Strategy The implementation uses two data structures: ...
Read MoreLongest Increasing Path in a Matrix in Python
Given a matrix, we need to find the length of the longest increasing path. From each cell, we can move in four directions − left, right, up, or down. We cannot move diagonally or outside the matrix boundaries. This problem can be solved efficiently using dynamic programming with memoization and depth-first search (DFS). Problem Example Consider this matrix ? 9 9 4 6 6 8 2 1 1 The longest increasing path is 1 → 6 → 8 → 9 with length 4. Algorithm Approach ...
Read More