Python Articles

Page 117 of 855

Python Program to find out the determinant of a given special matrix

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

Determinants of special matrices formed from tree structures have important applications in graph theory and computational mathematics. In this problem, we construct an n×n matrix A where each element A(x, y) equals the weight of the least common ancestor (LCA) of vertices x and y in a tree. Problem Understanding Given a tree with n vertices labeled 1 to n, where vertex 1 is the root and each vertex has weight wi, we need to find the determinant of matrix A where A(x, y) = weight_of_LCA(x, y). Example Tree Structure For input edges [[1, 2], [1, ...

Read More

Python Program to find out the number of rooms in which a prize can be hidden

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

Suppose in a game show there are 2n rooms arranged in a circle. One room contains a prize that participants must find. The rooms are numbered 1, 2, 3, ..., n, -n, -(n-1), ..., -1 in clockwise order. Each room has a door with a marking x that indicates the distance to the next room. If x is positive, the door opens to the xth room clockwise; if negative, it opens to the xth room counter-clockwise. We need to find how many rooms can hide the prize such that participants cannot find it by following the door sequence. ...

Read More

Python Program to find out how many cubes are cut

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

Suppose we have several cubes of dimensions a, b, and c, and using them we create a new box of dimension a×b×c. The values a, b, and c are pairwise co-prime, meaning gcd(a, b) = gcd(b, c) = gcd(a, c) = 1. We need to cut the box into two pieces with a single slice and determine how many individual cubes are cut into two pieces. The cut is made through a plane that passes through specific vertices, creating a diagonal slice through the 3D box structure. Problem Statement Given an array containing multiple sets ...

Read More

Python Program to find out how many times the balls will collide in a circular tube

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

Suppose there are n balls in a circular tube that is 100 meters long. Initially, each ball is positioned at a specific distance from a starting point. The balls travel in different directions at a speed of 0.1 meters per second. When two balls meet, they collide and change their direction of travel. We need to find the total number of collisions that occur over 10^9 + 6 seconds. Problem Analysis The key insight is that balls moving in opposite directions will collide multiple times as they circle the tube. The collision count depends on: Number ...

Read More

Python Program to find the number of operations to pack several metal bars in a container

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

Suppose we are given the task to transport several metal bars of different sizes. The transportation container has limited length and can only contain bars of length 1. We are provided n number of bars with their lengths given in a list. To fit all bars in the container, we must cut and divide them into unit sizes, then pack them (which costs one operation per bar). The key insight is that cutting a bar optimally requires finding its prime factorization and cutting along those factors to minimize operations. Problem Analysis For input input_arr = [6, 3, ...

Read More

Python Program to find out the price of a product after a number of days

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Sometimes we need to calculate the price of a product after exponential price increases over multiple days. This problem involves computing modular exponentiation to handle very large numbers efficiently. Problem Understanding Given an initial product price x and number of days y, the price after y days becomes x^y. Since this can result in extremely large numbers, we return the result modulo 10^9 + 7. Algorithm Steps The solution involves the following steps − Iterate through each price-days pair in the input list Extract the initial ...

Read More

Python Program to find out the number of matches in an array containing pairs of (base, number)

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

We need to find pairs in the format (base, number) that represent the same decimal value. For example, (10, 15) means 15 in base 10, and (8, 17) means 17 in base 8. Both equal 15 in decimal, so they match. So, if the input is like num_inputs = 2, input_arr = [(10, 15), (8, 17)], then the output will be 1. The variable num_inputs specifies the number of inputs, and the array input_arr lists the number pairs. Here if we look at the two pairs: 15 in base 10 (decimal) is the same as 17 in base ...

Read More

Python Program to find out the size of the bus that can contain all the friends in the group

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

Suppose there are n number of student groups waiting to get back from their college to their home via the college bus. In each student group, there are m number of students. The student groups want to travel by bus without getting separated. They board the bus if and only if all the members of their group can get on the bus. Also, a group does not board the bus if their previous group has not boarded the bus or has already reached their destination. Given the number of groups and the number of students in each group, we ...

Read More

Program to find wealth of richest customer in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Suppose we have a matrix of order m x n called accounts where accounts[i][j] is the amount of money of ith customer present in jth bank. We have to find the wealth that the richest customer has. A customer is richest when he/she has maximum amount considering all banks. Example Input So, if the input is like ? 10 20 15 30 5 20 10 5 12 15 12 3 Then the output will be 55 as the money of second person is 30+5+20 = 55, ...

Read More

Program to find maximum k-repeating substring from sequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Given a sequence of characters s, we say a string w is k-repeating if w concatenated k times is a substring of the sequence. The maximum k-repeating value of w is the highest value of k where w is k-repeating in the sequence. If w is not a substring of s, its maximum k-repeating value is 0. Problem Statement Given a sequence s and a string w, we need to find the maximum k-repeating value of w in the sequence. For example, if s = "papaya" and w = "pa", the output will be 2 because "pa" ...

Read More
Showing 1161–1170 of 8,549 articles
« Prev 1 115 116 117 118 119 855 Next »
Advertisements