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 133 of 377
Find the smallest window in a string containing all characters of another string in Python
Finding the smallest window in a string that contains all characters of another string is a classic sliding window problem. This technique uses two pointers to efficiently find the minimum substring. Problem Statement Given two strings s1 and s2, we need to find the smallest substring in s1 that contains all characters of s2. For example, if s1 = "I am a student" and s2 = "mdn", the output should be "m a studen". Algorithm Steps The sliding window approach works as follows: Create frequency arrays for both pattern and current window characters ...
Read MoreFind the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python
In some programming problems, we need to find the smallest positive integer that cannot be represented as the sum of any subset from a given sorted array. This is a classic greedy algorithm problem that can be solved efficiently in O(n) time complexity. So, if the input is like A = [1, 4, 8, 12, 13, 17], then the output will be 2, because we can represent 1 using [1], but we cannot represent 2 using any subset combination. Algorithm Approach The key insight is that if we can represent all numbers from 1 to answer-1, and the current element A[i]
Read MoreFind the probability of a state at a given time in a Markov chain - Set 1 in Python
A Markov chain is a random process where the probability of moving to the next state depends only on the current state. We can represent it as a directed graph where nodes are states and edges have transition probabilities. To find the probability of reaching state F at time T starting from state S, we use dynamic programming. Problem Statement Given a Markov chain with N states, we need to find the probability of reaching state F at time T if we start from state S at time 0. Each transition takes one unit of time, and the ...
Read MoreFind the position of box which occupies the given ball in Python
Given two arrays A and B, where A represents the number of boxes in each row and B contains ball numbers, we need to find the row and column position for each ball. Ball i with value B[i] will be placed in the B[i]-th box when counting from the beginning. Problem Understanding Array A defines the grid structure where A[i] is the number of boxes in row i. Array B contains ball numbers, and each ball B[i] goes to the B[i]-th position when boxes are numbered sequentially from 1. Example For A = [3, 4, 5, ...
Read MoreFind the player who rearranges the characters to get a palindrome string first in Python
Suppose we have a string S with lowercase letters, and two players are playing a game. The rules are as follows: A player wins if they can shuffle the characters of the string to form a palindrome string A player cannot win by removing any character from the string Both players play optimally and Player 1 starts the game. We need to determine the winner. So, if the input is like "pqpppq", then the output will be Player 1 as Player 1 in the first step arranges the characters to get "ppqqpp" and wins the ...
Read MoreFind the original matrix when largest element in a row and a column are given in Python
This problem involves reconstructing an original matrix when you know the largest element in each row and column, plus a binary matrix indicating where positive values were located. The key insight is that at any position where the binary matrix has a 1, the original value must be the minimum of the row maximum and column maximum. Problem Understanding Given: Array A of size N (maximum elements for each row) Array B of size M (maximum elements for each column) Binary matrix of size N×M where 1 indicates a positive value existed, 0 indicates the position was ...
Read MoreFind the ordering of tasks from given dependencies in C++
Finding the ordering of tasks from given dependencies is a classic topological sorting problem. Given n tasks labeled from 0 to n-1 with prerequisite relationships, we need to determine a valid execution order. This problem uses Depth-First Search (DFS) with cycle detection to ensure all dependencies are satisfied. Problem Understanding If we have a prerequisite pair [2, 1], it means task 1 must be completed before task 2. We need to find an order where all prerequisites are satisfied, or return an empty array if it's impossible due to circular dependencies. Algorithm Approach The solution uses ...
Read MoreFind the number of sub arrays in the permutation of first N natural numbers such that their median is M in Python
Finding the number of sub-arrays in a permutation where the median equals a specific value M is a classic problem that can be solved efficiently using prefix sums and hash maps. The key insight is that for a sub-array to have median M, the number of elements less than M should be at most equal to the number of elements greater than M. We track the balance using a running sum where we subtract 1 for elements less than M and add 1 for elements greater than M. Algorithm Approach We maintain a balance counter and use ...
Read MoreFind the number of spectators standing in the stadium at time t in Python
This problem simulates spectators standing and sitting in a stadium over time. There are n spectators labeled 1 to n, where at most k spectators can stand simultaneously. We need to find how many spectators are standing at time t. Problem Understanding The pattern works as follows: At time t1, the first spectator stands. At time t2, the second spectator stands. ... At time tk, the k-th spectator stands. At time tk + 1, the (k + 1)-th spectator stands and ...
Read MoreFind the number of rectangles of size 2x1 which can be placed inside a rectangle of size n x m in Python
Finding the number of 2x1 rectangles that can be placed inside an n x m rectangle is a classic combinatorial problem. We need to consider different cases based on whether the dimensions are even or odd. Problem Understanding Given a rectangle of size n x m, we want to find the maximum number of non-overlapping 2x1 rectangles that can be placed inside it. The constraints are ? No two small rectangles can overlap Every small rectangle must lie completely inside the larger one Small rectangles can touch the edges of the larger rectangle ...
Read More