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 141 of 377
Find Shortest distance from a guard in a Bankin Python
Suppose we have a matrix filled with three letters 'O', 'G', and 'W' where 'O' represents open space, 'G' represents guards, and 'W' represents walls in a bank. We need to replace all O's with their shortest distance from the nearest guard, without going through walls. In the output matrix, guards become 0 and walls become -1. Problem Example If the input matrix is: OOOOG OOOWO OWOOO GWWWO OOOOG The output will be: 33210 233-11 1-1432 0-1-1-11 12210 Algorithm Approach We use multi-source BFS (Breadth-First Search) to solve this ...
Read MoreFind same contacts in a list of contacts in Python
Finding duplicate contacts in a list is a common problem where we need to group contacts that belong to the same person. Two contacts are considered the same if they share any common field: username, email, or phone number. This problem can be solved using graph theory with Depth First Search (DFS). We create an adjacency matrix where contacts are nodes, and edges connect contacts that share any field. Problem Statement Given a list of contacts with three fields each (username, email, phone), we need to ? A contact can store username, email and phone ...
Read MoreFind multiplication of sums of data of leaves at same levels in Python
Binary trees often require level-by-level processing to solve complex problems. In this problem, we need to find the sum of leaf nodes at each level and multiply all these sums together. Problem Statement Given a binary tree, we need to perform the following operations ? For each level, find sum of all leaves if there are leaves at this level. Otherwise ignore it. Find multiplication of all sums and return it. For example, if we have the following tree structure ? ...
Read MoreFind pairs with given sum such that pair elements lie in different BSTs in Python
Finding pairs with a given sum from two different Binary Search Trees is a classic problem that combines BST traversal with the two-pointer technique. The key insight is to use the sorted property of BST in-order traversals. Problem Statement Given two Binary Search Trees and a target sum, find all pairs where one element comes from the first BST and another from the second BST, and their sum equals the target. For example, with sum = 12: BST 1 ...
Read MoreFind pairs with given sum such that elements of pair are in different rows in Python
Finding pairs with a given sum from different rows in a matrix is a common problem in data structures. We need to find all pairs where one element comes from one row and the other from a different row, and their sum equals the target value. So, if the input matrix is ? 2 4 3 5 6 9 8 7 10 11 14 12 13 1 15 16 And sum = 13, then the output will be [(4, 9), (5, 8), (2, 11), (3, ...
Read MoreFind bitonic point in given bitonic sequence in Python
A bitonic sequence is a sequence of numbers that first increases strictly, reaches a peak (the bitonic point), then decreases strictly. We need to find this peak element efficiently using binary search. The bitonic point is the maximum element where the left neighbor is smaller and the right neighbor is also smaller. Algorithm We use binary search to find the bitonic point in O(log n) time ? Compare the middle element with its neighbors If array[mid-1] < array[mid] > array[mid+1], we found the bitonic point If array[mid] < array[mid+1], the peak is in the right ...
Read MoreFind array with k number of merge sort calls in Python
Sometimes we need to construct an array that requires exactly k recursive calls when sorted using merge sort. This problem involves understanding how merge sort's recursive structure works and manipulating an array to achieve the desired call count. Given two numbers a and b, we need to find an array containing values in range [1, a] that requires exactly b number of recursive merge sort calls. Understanding the Problem Merge sort makes recursive calls by dividing the array into halves. The total number of calls follows a pattern based on the array structure and element positions. ...
Read MoreFind an element which divides the array in two subarrays with equal product in Python
Given an array of integers, we need to find an element that divides the array into two subarrays with equal product. If no such element exists, return -1. For example, in the array [2, 5, 3, 2, 5], the element 3 at index 2 divides it into subarrays [2, 5] and [2, 5], both having product 10. Algorithm We use prefix and suffix product arrays to efficiently calculate products of left and right subarrays ? Create a prefix product array storing cumulative products from left Create a suffix product array storing cumulative products from right ...
Read MoreFind an element in an array such that elements form a strictly decreasing and increasing sequence in Python
In Python, we can find an element in an array where elements form a strictly decreasing sequence followed by a strictly increasing sequence. The element we seek is the transition point between these two sequences. Problem Requirements The solution must satisfy these conditions: Both decreasing and increasing sequences must have minimum length 2 The last value of the decreasing sequence is the first value of the increasing sequence No duplicate elements are allowed (strictly decreasing/increasing) For example, in array [5, 4, 3, 4], the sequence [5, 4, 3] is strictly decreasing and [3, 4] ...
Read MoreFind all strings formed from characters mapped to digits of a number in Python
When working with digit-to-character mappings, we often need to generate all possible string combinations from a given number. This is similar to how old mobile phone keypads worked, where each digit mapped to multiple letters. Problem Understanding Given a mapping where each digit (1-9) corresponds to a list of characters, we need to find all possible strings that can be formed from a number. The key constraint is that we must use the same character for every occurrence of a digit in the number. 1 → ['A', 'B', 'C'] 2 → ['D', 'E', 'F'] ...
Read More