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 143 of 377
Optimize Water Distribution in a Village in Python
The water distribution optimization problem is a classic application of graph theory and the Minimum Spanning Tree (MST) algorithm. Given n houses, we can either build wells directly in houses or connect them via pipes. The goal is to minimize the total cost to supply water to all houses. The problem can be modeled as finding the MST of a graph where: Each house is a node Building a well is represented as connecting to a virtual node (node 0) Pipes between houses are edges with their respective costs Algorithm Approach We use Kruskal's algorithm ...
Read MoreString Transforms Into Another String in Python
Sometimes we need to check if we can transform one string into another by converting all occurrences of specific characters. This problem involves character mapping and transformation rules to determine if str1 can be converted to str2. In one conversion, we can convert all occurrences of one character in str1 to any other lowercase English character. The key constraint is that we need at least one "free" character (not used in str2) to perform intermediate conversions. Problem Example Given str1 = "aabcc" and str2 = "ccdee", we can transform str1 to str2 ? Convert 'c' ...
Read MoreParallel Courses in Python
The parallel courses problem involves finding the minimum number of semesters needed to complete N courses with prerequisite dependencies. This is essentially a topological sorting problem that can be solved using BFS (Breadth-First Search). Problem Understanding Given N courses labeled 1 to N and a relations array where relations[i] = [X, Y] means course X must be completed before course Y. We need to find the minimum semesters required to complete all courses, or return -1 if impossible due to circular dependencies. Algorithm Approach We use topological sorting with BFS to process courses level by level ...
Read MoreDivide Array Into Increasing Sequences in Python
Suppose we have a non-decreasing array of positive integers called nums and an integer K. We need to determine if this array can be divided into one or more disjoint increasing subsequences, each of length at least K. For example, if the input is nums = [1, 2, 2, 3, 3, 4, 4] and K = 3, the output will be True because this array can be divided into two subsequences: [1, 2, 3, 4] and [2, 3, 4], both with lengths at least 3. Algorithm Approach The key insight is that we need to find the ...
Read MoreBinary Prefix Divisible By 5 in Python
Given an array A of 0s and 1s, we need to determine which binary prefixes are divisible by 5. For each index i, we interpret the subarray from A[0] to A[i] as a binary number and check if it's divisible by 5. For example, if the input is [0, 1, 1, 1, 1, 1], the binary prefixes are: 0 (binary) = 0 (decimal) → divisible by 5 01 (binary) = 1 (decimal) → not divisible by 5 011 (binary) = 3 (decimal) → not divisible by 5 0111 (binary) = 7 (decimal) → not divisible by 5 ...
Read MorePowerful Integers in Python
A powerful integer is defined as a number that can be expressed in the form xi + yj where x and y are positive integers, and i, j are non-negative integers. Given two positive integers x and y along with a bound, we need to find all powerful integers less than or equal to the bound. Understanding Powerful Integers For example, with x = 2, y = 3, and bound = 10: 2 = 20 + 30 = 1 + 1 3 = 21 + 30 = 2 + 1 4 = 20 + 31 = ...
Read MoreDelete Columns to Make Sorted in Python
Given an array of N lowercase letter strings of equal length, we need to find the minimum number of column deletions required to make all remaining columns sorted in non-decreasing order. When we delete columns at specific indices, the remaining characters form new columns. Each remaining column must be sorted for the solution to be valid. Problem Understanding Consider the array ["abcdef", "uvwxyz"] with deletion indices {0, 2, 3}: After deletions: ["bef", "vyz"] Remaining columns: ["b", "v"], ["e", "y"], ["f", "z"] All columns are sorted in non-decreasing order Algorithm The approach involves ...
Read MoreDI String Match in Python
The DI String Match problem involves creating a permutation based on a pattern string containing "I" (increase) and "D" (decrease). Given a string S of length N, we need to return a permutation of numbers [0, 1, ..., N] that satisfies the increase/decrease pattern. Problem Understanding For a string S containing only "I" and "D" characters: If S[i] is "I", then A[i] < A[i+1] (increasing) If S[i] is "D", then A[i] > A[i+1] (decreasing) For example, with input "IDID", a valid output is [0, 4, 1, 3, 2] because: 0 < 4 (I), ...
Read MoreValid Mountain Array in Python
A valid mountain array is an array that rises to a peak and then falls, resembling a mountain shape. To be valid, it must have at least 3 elements, strictly increase to a peak, then strictly decrease. Mountain Array Requirements An array is a valid mountain if it satisfies these conditions: Array size >= 3 There exists a peak index i where: Elements strictly increase: A[0] < A[1] < ... < A[i] Elements strictly decrease: A[i] > A[i+1] > ... > A[length-1] Algorithm Approach We use a two-phase traversal approach: Ascending ...
Read MoreReorder Data in Log Files in Python
Suppose we have an array of logs. In that array each entry is a space delimited string of words. The first word in each log is an alphanumeric identifier. Then, there are different types of strings like below − Each word after the id will consist only of lowercase letters; Each word after the id will consist only of digits. We will call these two types of logs as letter-logs and digit-logs respectively. It is guaranteed that each log has at least one word after its id. ...
Read More