Programming Articles

Page 549 of 2547

Find the character in first string that is present at minimum index in second string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 406 Views

Suppose we have a string str and another string patt, we need to find the character in patt that is present at the minimum index in str. If no character from patt is present in str, then return -1. For example, if str = "helloworld" and patt = "wor", the output will be 'o' because 'o' appears at index 4, which is the smallest index among all characters from patt that exist in str. Algorithm Steps To solve this problem, we follow these steps − Initialize minimum_index to a large value ...

Read More

Find sum of all elements in a matrix except the elements in row and-or column of given cell in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 977 Views

We have a 2D matrix and a set of cell indices represented as (i, j) where i is the row and j is the column. For each given cell index, we need to find the sum of all matrix elements excluding the elements in the ith row and jth column. For example, if we have the matrix: 2 2 3 4 5 7 6 4 3 And cell indices = [(0, 0), (1, 1), (0, 1)], the output will be [19, 14, 20]. Algorithm To ...

Read More

Find substrings that contain all vowels in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 937 Views

Sometimes we need to find all substrings within a string that contain all vowels (a, e, i, o, u) at least once and contain no consonants. This is useful for text processing and pattern matching tasks. Problem Statement Given a string in lowercase alphabets, we need to find all substrings that: Contain all five vowels at least once Contain no consonants For example, if the input is "helloworldaeiouaieuonicestring", the output will be substrings like 'aeiou', 'aeioua', 'aeiouai', etc. Algorithm We'll use a nested loop approach with these steps: For each ...

Read More

Find subsequences with maximum Bitwise AND and Bitwise OR in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 829 Views

When working with arrays and bitwise operations, we often need to find subsequences that maximize the sum of bitwise AND and OR operations. The key insight is that the maximum AND value comes from the largest single element, while the maximum OR value comes from combining all elements. Problem Understanding Given an array of n elements, we need to find two subsequences (possibly different) such that the sum of the bitwise AND of the first subsequence and bitwise OR of the second subsequence is maximized. For example, with array A = [4, 6, 7, 2], the maximum ...

Read More

Find sub-arrays from given two arrays such that they have equal sum in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 440 Views

When working with two arrays, you may need to find sub-arrays that have equal sums. This problem involves finding contiguous elements from each array where the sum of elements in one sub-array equals the sum in another sub-array. Given two arrays P and Q of size N containing numbers 1 to N, we need to find sub-arrays with equal sums and return their indices. If no solution exists, return -1. Example For arrays P = [2, 3, 4, 5, 6] and Q = [9, 3, 2, 6, 5], the solution is: P[0..2] = 2 + 3 ...

Read More

Find Shortest distance from a guard in a Bankin Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 249 Views

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 More

Find same contacts in a list of contacts in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 332 Views

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 More

Find multiplication of sums of data of leaves at same levels in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 189 Views

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 More

Find pairs with given sum such that pair elements lie in different BSTs in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 194 Views

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 More

Find pairs with given sum such that elements of pair are in different rows in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 258 Views

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 More
Showing 5481–5490 of 25,466 articles
« Prev 1 547 548 549 550 551 2547 Next »
Advertisements