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
Python Articles
Page 334 of 855
Program to check existence of edge length limited paths in Python
Given an undirected weighted graph with n nodes and an edgeList where each edge is represented as (u, v, w) indicating a path from node u to node v with distance w. We also have queries in the format (p, q, lim) asking whether there exists a path from node p to node q with total distance less than lim. This problem can be efficiently solved using the Union-Find (Disjoint Set Union) data structure combined with sorting techniques. Algorithm Overview The approach involves: Sort edges by weight in ascending order Sort queries by their limit ...
Read MoreProgram to minimize deviation in array in Python
Suppose we have an array nums. We can perform two types of operations on any element of the array any number of times ? For even elements, divide it by 2 For odd elements, multiply it by 2. The deviation of the array is the maximum difference between any two elements in the array. We have to find the minimum deviation the array can have after performing some number of operations. So, if the input is like nums = [6, 3, 7, 22, 5], then the output will be 5 because we can make our ...
Read MoreProgram to distribute repeating integers in Python
When distributing items to customers, we need to ensure each customer gets exactly the quantity they ordered, with all items being identical. This problem involves checking if we can satisfy all customer demands using available items. The approach uses backtracking with frequency counting. We count how many times each frequency appears, then try to allocate items to customers in descending order of demand. Algorithm Steps Here's how the solution works ? Count frequency of each unique number in the array Count frequency of frequencies (how many numbers ...
Read MoreProgram to find number of ways to form a target string given a dictionary in Python
Suppose we have a list of strings called words, where all elements are of the same length. We also have a string called target. We have to generate target using the given words under the following rules: We should generate target from left to right. To get the ith character (0-indexed) of target, we can select the kth character of the jth string in words when target[i] is same as words[j][k]. Once we use the kth character of the jth string of words, we cannot use the xth character of any string in words where x
Read MoreProgram to find minimum one bit operations to make integers zero in Python
We need to transform a number n into 0 using specific bit operations. The operations allowed are: Select the rightmost bit in the binary representation of n. Change the ith bit in the binary representation of n when the (i-1)th bit is set to 1 and the (i-2)th through 0th bits are set to 0. We need to find the minimum number of operations required to transform n into 0. Understanding the Problem For example, if n = 6, the binary representation is "110". The transformation steps are ? Start: "110" (6 ...
Read MoreProgram to find out how many transfer requests can be satisfied in Python
Suppose there are n hostel rooms numbered from 0 to n-1. Students want to transfer to different rooms and place transfer requests. Since no room can remain vacant, a transfer is only satisfied if another student takes the transferring student's place. We need to find the maximum number of requests that can be satisfied simultaneously. Problem Understanding For a set of transfer requests to be valid, the number of students leaving each room must equal the number of students entering that room. This creates a balanced system where no room becomes empty or overcrowded. Example If ...
Read MoreProgram to find out the shortest path to reach the goal in Python
Finding the shortest path in a grid is a classic problem that can be solved using Breadth-First Search (BFS). In this problem, we need to navigate through a grid where different symbols represent different cell types and find the minimum moves to reach the goal. Grid Symbol Meanings '#' is the goal cell that we want to reach 'O' is a free cell via which we can travel to the goal cell '*' is our current position in the grid 'X' is a blocked cell, via which we cannot travel Problem Example Given the ...
Read MoreProgram to find largest submatrix with rearrangements in Python
Given an m x n binary matrix, we can rearrange columns to find the largest submatrix containing only 1s. This problem combines dynamic programming with greedy optimization to maximize the area. Problem Understanding Consider this input matrix ? 0 0 1 1 1 1 1 0 1 After rearranging columns, we can get ? 1 1 0 1 1 1 0 1 1 The highlighted 2x2 submatrix has area 4, which is the maximum possible. Algorithm Steps ...
Read MoreProgram to find tuple with same product in Python
Given an array nums with unique positive values, we need to find the number of tuples (a, b, c, d) such that a*b = c*d where a, b, c, and d are elements of nums, and all elements are distinct. For example, if the input is nums = [2, 3, 4, 6], then the output will be 8 because we can get tuples like (2, 6, 3, 4), (2, 6, 4, 3), (6, 2, 3, 4), (6, 2, 4, 3), (3, 4, 2, 6), (4, 3, 2, 6), (3, 4, 6, 2), (4, 3, 6, 2). Algorithm ...
Read MorePython program to find list of triplets for which i+j+k is not same as n
Sometimes we need to find all possible combinations of three numbers that don't sum to a specific value. This problem involves generating all triplets (i, j, k) where each value is within given ranges and their sum is not equal to a target number n. We can solve this efficiently using list comprehension with a filtering condition to exclude triplets that sum to n. Problem Statement Given three upper bounds i, j, and k, and a target sum n, find all triplets [x, y, z] where: x ranges from 0 to i (inclusive) y ranges ...
Read More