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 136 of 377
Find maximum distance between any city and station in Python
Sometimes we need to find the maximum distance between any city and its nearest station. Given N cities numbered from 0 to N-1 and a list of cities with stations, we need to calculate the furthest any city is from a station. So, if the input is like N = 6 and stations = [2, 4], then the output will be 2, because city 0 is distance 2 from the nearest station at city 2. Algorithm Overview To solve this problem, we will follow these steps − Create a boolean array to mark which cities ...
Read MoreFind maximum difference between nearest left and right smaller elements in Python
Given an array of integers, we need to find the maximum absolute difference between the nearest left and right smaller elements for each position. If no smaller element exists on either side, we consider it as 0. For example, with array [3, 5, 9, 8, 8, 10, 4], the left smaller elements are [0, 3, 5, 5, 5, 8, 3] and right smaller elements are [0, 4, 8, 4, 4, 4, 0]. The maximum absolute difference is |8 - 4| = 4. Algorithm Steps We use a stack-based approach to find nearest smaller elements efficiently ? ...
Read MoreFind lost element from a duplicated array in Python
Finding a lost element from a duplicated array is a common problem where two arrays are identical except one element is missing from one of them. We need to identify which element is missing efficiently. So, if the input is like A = [2, 5, 6, 8, 10], B = [5, 6, 8, 10], then the output will be 2 as 2 is missing from second array. Using Binary Search Approach The most efficient approach uses binary search to find the missing element in O(log n) time complexity ? def solve(A, B, N): ...
Read MoreFind longest palindrome formed by removing or shuffling chars from string in Python
Suppose we have a string; we have to find the longest palindrome that can be generated by deleting or shuffling the characters from the string. If there are more than one palindrome, then return only one. So, if the input is like pqqprrs, then the output will be pqrsrqp. Approach To solve this problem, we will follow these steps ? Count the frequency of each character in the string For each character, use pairs to form the left and right sides of the palindrome ...
Read MoreFind longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python
A bitonic sequence is a sequence that first increases and then decreases. In this problem, we need to find the longest bitonic sequence where the increasing part comes from the first array and the decreasing part comes from the second array, both as subsequences. Given two arrays A and B, we find the longest increasing subsequence from A, then the longest increasing subsequence from the reverse of B (which gives us a decreasing sequence), and combine them. Algorithm Overview The solution uses dynamic programming with binary search optimization: Find the longest increasing subsequence (LIS) from ...
Read MoreFind largest subtree having identical left and right subtrees in Python
Finding the largest subtree with identical left and right subtrees involves comparing the structure and values of subtrees. We'll use a recursive approach with tree encoding to efficiently identify matching subtrees. 55 15 70 10 25 ...
Read MoreFind k-th character of decrypted string - Set – 2 in Python
Sometimes we need to decode an encoded string where repetitions of substrings are represented as substring followed by count of substrings. For example, if the string is "pq2rs2" and we want the 5th character, the output will be 'r', because the decrypted string is "pqpqrsrs" and the 5th character is 'r'. We have to keep in mind that the frequency of encrypted substring can be of more than one digit. So, if the input is like string = "pq4r2ts3" and k = 11, then the output will be 't', as the decoded string is "pqpqpqpqrrtststs". Algorithm To ...
Read MoreFind Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in Python
In binary arrays, we often need to find the optimal position to flip a 0 to 1 to create the longest possible sequence of consecutive 1s. This problem requires tracking sequences on both sides of each 0 to determine which replacement yields maximum benefit. Given a binary array like [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1], we need to find which 0 should be replaced to get the longest continuous sequence of 1s. The answer is index 10, creating [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1]. ...
Read MoreFind index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python
Given two strings S1 and S2 of the same length, we need to find an index i such that the prefix S1[0...i] and suffix S2[i+1...n-1] form a palindrome when concatenated. If no such index exists, return -1. For example, with S1 = "pqrsu" and S2 = "wxyqp", we find that S1[0..1] = "pq" and S2[2..4] = "yqp" combine to form "pqyqp", which is a palindrome at index 1. Algorithm To solve this problem, we follow these steps − Initialize n as the length of both strings For each index i from 0 to n-1: ...
Read MoreFind if there is a path of more than k length from a source in Python
Finding a path longer than a given length in a graph is a classic graph traversal problem. We need to determine if there exists a simple path (without cycles) from a source vertex to any other vertex with total edge weights exceeding a threshold k. 0 1 2 ...
Read More