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
Server Side Programming Articles
Page 545 of 2109
Find the distance covered to collect items at equal distances in Python
In this problem, we need to calculate the total distance covered by a participant who collects stones placed at equal intervals and returns each stone to a starting bucket. The setup is as follows: A bucket is placed at the starting point The first stone is 6 units away from the bucket Each subsequent stone is 4 units apart from the previous one The participant must collect stones one by one, returning to the bucket after each collection Bucket ...
Read MoreFind the count of sub-strings whose characters can be rearranged to form the given word in Python
Given a string S (all letters are lowercase), we need to find the count of all substrings of length four whose characters can be rearranged to form the word "bird". For example, if the input is "birdb", the output will be 2 because there are two 4-character substrings that contain exactly one 'b', one 'i', one 'r', and one 'd'. Approach To solve this problem, we follow these steps − Initialize a counter to 0 Iterate through all possible 4-character substrings For each substring, count occurrences of 'b', 'i', 'r', 'd' If each character appears ...
Read MoreFind the cordinates of the fourth vertex of a rectangle with given 3 vertices in Python
A rectangle has four vertices, and if we know three of them, we can find the fourth by using the property that opposite vertices share the same coordinates. In a grid representation with asterisks ('*') marking three vertices, we need to find the missing fourth vertex coordinates. Problem Understanding Given a grid where exactly three asterisks ('*') represent three vertices of a rectangle, we need to find the coordinates of the fourth vertex. The key insight is that in a rectangle, each row and column should contain exactly two vertices, except for the missing vertex's row and column ...
Read MoreFind the character in first string that is present at minimum index in second string in Python
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 MoreFind sum of all elements in a matrix except the elements in row and-or column of given cell in Python
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 MoreFind substrings that contain all vowels in Python
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 MoreFind subsequences with maximum Bitwise AND and Bitwise OR in Python
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 MoreFind sub-arrays from given two arrays such that they have equal sum in Python
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 MoreFind 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 More