Programming Articles

Page 145 of 2547

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 26-Mar-2026 336 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 381 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

Program to apply Russian Peasant Multiplication in Python

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

The Russian Peasant Multiplication method is an ancient algorithm for performing exponentiation of complex numbers of the form (p + q·i)^r using recursive multiplication. This method efficiently computes the result by breaking down the exponent using binary representation. Given four integers p, q, r, and k, we need to calculate (p + q·i)^r = x + y·i and return (x mod k, y mod k). Algorithm Steps The Russian Peasant method follows these recursive steps: If r = 0, return 1 (any number to power 0 is 1) If r = 1, return (p mod ...

Read More

Program to find out the number of integral coordinates on a straight line between two points in Python

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

When we have two points (p1, q1) and (p2, q2), we can find the number of integral coordinates (points where both x and y are integers) that lie on the straight line between them. This problem uses the mathematical concept that the number of lattice points on a line segment equals the GCD of the coordinate differences. So, if the input is like p1 = 3, q1 = 3, p2 = 6, q2 = 6, then the output will be 2. The integral points (4, 4) and (5, 5) lie on the straight line between (3, 3) and (6, ...

Read More

Program to find number of possible moves to start the game to win by the starter in Python

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

Suppose Amal and Bimal are playing a game with n containers, each containing chocolates. The containers are numbered from 1 to N, where the ith container has count[i] chocolates. Players take turns selecting a non-empty container and removing one or more chocolates from it. The player who cannot make a move loses. We need to find how many winning first moves Amal has. Game Rules The game follows these rules: Players alternate turns, starting with Amal On each turn, a player must select a non-empty container The player removes one or more chocolates from the selected ...

Read More

Program to find winner of a rower breaking game in Python

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

Suppose we have an array of tower heights. There are n different towers with different heights. Amal and Bimal are playing a tower breaking game with specific rules. Game Rules Amal always plays first During each move, the current player selects a tower of height X and breaks it down into Y different towers of height Z each, where Y*Z = X and both X and Y > 1 The player who cannot make a move loses the game Algorithm Approach This is a classic game theory problem that can be solved using the ...

Read More

Program to find winner of number reducing game in Python

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

Suppose Amal and Bimal are playing a number reducing game. They have a number n and follow these rules: If n is a power of 2, divide it by 2. Otherwise, reduce it by the next lower number which is also a power of 2. Whoever reduces the number to 1 wins the game. Amal always starts first. For example, if n = 19: Amal reduces 19 to 16 (19 - 16 = 16, where 16 is the largest power of 2 less than 19), then Bimal divides 16 by 2 to get 8, Amal divides 8 by 2 ...

Read More

Program to find number of magic sets from a permutation of first n natural numbers in Python

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

Suppose we have an array A with first n natural numbers, and one permutation P{p1, p2, ... pn} of array A. We have to check how many magic sets are there. A permutation is said to be magic set, if this satisfies these few rules − If we have k, then the elements in positions a[1], a[2], ... a[k] are less than their adjacent elements [P[a[i] - 1] > P[a[i]] < P[a[i] + 1]] If we have l, then the elements in positions b[1], b[2], ... b[l] are greater than their adjacent ...

Read More

Program to find number of sequences after adjacent k swaps and at most k swaps in Python

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

We need to find how many sequences can be generated from an array of first n natural numbers using exactly k adjacent swaps (S1) and at most k adjacent swaps (S2). Adjacent swaps means swapping elements at positions i and i+1. Problem Understanding Given an array A with first n natural numbers [1, 2, 3, ..., n], we calculate ? S1: Number of sequences after exactly k adjacent swaps S2: Number of sequences after at most k adjacent swaps Example Walkthrough For n = 3, k = 2 with original array [1, 2, ...

Read More

Program to find number of strictly increasing colorful candle sequences are there in Python

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

This problem asks us to find the number of strictly increasing colorful candle sequences. A sequence is strictly increasing based on candle heights, and colorful if it contains at least one candle of each color from 1 to k. The solution uses the inclusion-exclusion principle combined with a Binary Indexed Tree (BIT) to efficiently count valid subsequences. For each subset of colors, we count increasing subsequences, then apply inclusion-exclusion to ensure all k colors are present. Algorithm Explanation The approach works as follows − Use bit manipulation to represent subsets of colors (2^k possibilities) For ...

Read More
Showing 1441–1450 of 25,469 articles
« Prev 1 143 144 145 146 147 2547 Next »
Advertisements