Python Articles

Page 264 of 855

Program to find nCr values for r in range 0 to n, in an efficient way in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 665 Views

Computing binomial coefficients nCr for all values of r from 0 to n can be done efficiently using Pascal's triangle properties. Instead of calculating each nCr independently, we can use the recurrence relation: nCr = nC(r-1) × (n-r+1) / r. This approach builds the coefficients incrementally, making it much faster than computing each combination separately. Problem Statement Given n, find all nCr values where r ranges from 0 to n. If any result exceeds 10^9, return it modulo 10^9. For example, if n = 6, the output should be [1, 6, 15, 20, 15, 6, 1] ...

Read More

Program to find total area covered by two rectangles in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 427 Views

When two rectangles overlap in a 2D plane, we need to calculate their total covered area by finding the sum of both areas minus any overlapping region. Each rectangle is defined by its bottom-left corner (x1, y1) and top-right corner (x2, y2). Rectangle 1 (A, B) (C, D) Rectangle 2 (E, F) (G, H) Overlap Algorithm To ...

Read More

Program to find maximum size of any sequence of given array where every pair is nice in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 313 Views

Suppose we have a sequence nums of size n. We have to find the maximum size of subsequence of nums in which every pair (p, q) is a nice pair. A pair is said to be nice if it satisfies at least one of these conditions: The parity of the number of distinct prime divisors of p is equal to that of q The parity of the sum of all positive divisors of p is same as q For example, the value 18 has two distinct prime divisors: 2 and 3, so it has an even ...

Read More

Program to find modulus of a number by concatenating n times in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 268 Views

Suppose we have a number A. We have to generate a large number X by concatenating A n times in a row and find the value of X modulo m. So, if the input is like A = 15, n = 3, m = 8, then the output will be 3, because the number X will be 151515, and 151515 mod 8 = 3. Algorithm To solve this, we will follow these steps ? If A is same as 0, then return 0 Calculate the number of ...

Read More

Program to find number of ways we can select sequence from Ajob Sequence in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 285 Views

Suppose there is a strange language called Ajob language with infinite letters. We know n words in this language where the first word is one character long, second is two characters long, and so on. All letters in a word are unique. We need to select any word and form a subsequence from it where the subsequence length should be k less than the original word length. If a word length is L, then subsequence length should be (L - k). Words with length smaller than k cannot be chosen. Two subsequences are different if they have different ...

Read More

Program to find number of pairs between x, whose multiplication is x and they are coprime in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 244 Views

Suppose there is a function f(x) that counts the number of (p, q) pairs such that: 1 < p

Read More

Program to find out the position of a ball after n reversals in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 321 Views

Suppose there are n balls arranged in positions 1, 2, 3, 4, ..., n. These balls undergo a series of reversals where each reversal starts from a different position, moving one step to the right each time. We need to find the final position of a ball that was initially at a given index. Understanding the Problem Let's trace through an example with 5 balls and find where the ball at index 2 ends up ? Initial arrangement: 1, 2, 3, 4, 5 Reversal operations: Reverse from position 0 to 4: 5, 4, 3, 2, ...

Read More

Program to reset a polygon to its initial state in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 269 Views

A polygon can be transformed through rotations and flips along specific axes. This problem involves tracking these transformations and determining what operation is needed to reset the polygon to its initial state. Understanding Polygon Transformations For a polygon with n vertices, there are specific rules for transformation axes: If n is odd, each flipping axis passes through one vertex and the middle of the opposite side If n is even, half the axes pass through opposite vertices, half through opposite sides Adjacent axes have an angle of 360/(2n) degrees The transformation operations work as ...

Read More

Program to find out the cells containing maximum value in a matrix in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 344 Views

Suppose, there is a n x n matrix initialized with 0s. Now, a list is given and it contains some pairs that contain a particular row and a column position. For each item i in the list, the contents of the cells increase by 1 where the row number and the column number are less than the row value and column value of item i in the list. After all the list elements have been traversed, we have to find out the number of cells in the matrix that contains the maximum value. (row and column index start at 0) ...

Read More

Program to find out number of blocks that can be covered in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 387 Views

Suppose there are n blocks in a path, and a worker is putting colored tiles on the blocks. The worker puts tiles on blocks whose numbers are divisible by 4 or 2, but not by 42. We need to find how many blocks can be covered with k colored tiles. The key insight is that in every 42 consecutive blocks, exactly 20 blocks satisfy our condition (divisible by 2 or 4, but not by 42). Since each tile covers 2 blocks, we can cover 42 blocks with 20 tiles in each complete cycle. Algorithm To solve this ...

Read More
Showing 2631–2640 of 8,546 articles
« Prev 1 262 263 264 265 266 855 Next »
Advertisements