Programming Articles

Page 477 of 2547

Program to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python

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

When we need to find the smallest positive integer x that is divisible by all numbers from 1 to k, we're looking for the Least Common Multiple (LCM) of numbers 1 through k. The trailing zeros in this LCM depend on how many times 10 divides it, which is determined by the minimum of powers of 2 and 5 in the prime factorization. For example, if k = 6, the LCM of {1, 2, 3, 4, 5, 6} is 60. The number 60 has one trailing zero because it contains one factor of 10 (2 × 5). Algorithm ...

Read More

Program to Find Out the Maximum Final Power of a List in Python

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

The power of a list is defined as the sum of (index + 1) * value_at_index over all indices. We can represent this mathematically as: $$\displaystyle\sum\limits_{i=0}^{n-1} (i+1)\times list[i]$$ Given a list of N positive integers, we can select any single value and move it to any position (beginning, end, or leave it unchanged). Our goal is to find the maximum possible final power of the list, with the result modded by 10^9 + 7. Understanding the Problem Let's first understand what power means with a simple example: def calculate_power(nums): power ...

Read More

Program to Find Out the Minimum Cost Possible from Weighted Graph in Python

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

Finding the minimum cost path in a weighted graph where cost is defined as the product of path length and maximum edge weight requires a specialized approach. This problem combines shortest path algorithms with weight constraints to optimize the overall cost function. Problem Understanding Given an undirected weighted graph represented as edges [u, v, w], we need to find the minimum cost path from node 0 to node n-1. The cost formula is: Cost = Number of edges × Maximum weight in the path Algorithm Approach The solution uses a binary search-like approach on edge ...

Read More

Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python

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

Given a string representing an equation in the form x+y=z, we need to find the minimum number of digits to insert to make the equation mathematically correct. This problem uses dynamic programming to explore all possible digit insertions. For example, with the equation "2+6=7", we can insert digits to make it "21+6=27", requiring 2 insertions. Approach The solution uses dynamic programming with the following strategy ? Split the equation into three parts: left operand (A), right operand (B), and result (C) Process digits from right to left (like manual addition) At each position, try different ...

Read More

Program to Find Out the Points Achievable in a Contest in Python

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

In a programming contest where we can attempt multiple problems but the contest ends when we solve one, we need to find the optimal strategy to maximize expected points. Given two lists points and chances of equal length, where chances[i] represents the percentage chance of solving problem i for points[i] points, and k representing the maximum problems we can attempt. The expected value of attempting the ith problem is points[i] * chances[i] / 100.0. We need to find the optimal strategy that maximizes expected points. Algorithm Approach We use dynamic programming with the following strategy: ...

Read More

Program to Find Out the Minimum Cost to Purchase All in Python

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

We need to find the minimum cost to purchase exactly one of every item from 0 to N-1. We have sets that contain ranges of items with purchase costs, and removals that let us discard extra items for a fee. This problem can be solved using Dijkstra's shortest path algorithm by modeling it as a graph where each node represents the number of items we need to collect. Understanding the Problem Given: sets: Each set [start, end, cost] lets us buy items from start to end (inclusive) for the given cost removals: Cost to remove one ...

Read More

Program to Find Out the Strings of the Same Size in Python

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

In Python, we can solve the problem of finding strings of the same size with specific constraints using dynamic programming with memoization. Given a string and a maximum allowed consecutive character count, we need to find how many strings are lexicographically smaller or equal to the input string while satisfying the consecutive character limit. Problem Understanding Given a string s of lowercase letters and an integer k, we need to count strings that: Have the same length as s Are lexicographically smaller than or equal to s Have no more than k consecutive equal characters ...

Read More

Program to Find Out the Number of Squares in a Grid in Python

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

Suppose we have two values p and q, we have to find the number of unique squares that can be generated from a grid with p rows and q columns in which the points are placed evenly. If the answer is very large return result mod 10^9 + 7. In this problem, a square is a set of 4 points that form the four vertices of a square. The sides of the square must have the same length, and it does not always have to be aligned with the axes of the grid. So, if the input is ...

Read More

Program to Find Out the Special Nodes in a Tree in Python

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

A tree has special nodes where every node in its subtree has a unique color. Given an n-ary tree as an adjacency list and node colors, we need to count how many nodes are special. For each node i: tree[i] contains its children and parent color[i] represents its color value A node is "special" if all nodes in its subtree (including itself) have distinct colors ? Example Consider this tree structure: .node-circle { fill: #e3f2fd; stroke: #1976d2; ...

Read More

Program to Find Out if There is a Short Circuit in Input Words in Python

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

Given a list of words, we need to check if they can be chained to form a circle. A word A can be placed before word B if the last character of A matches the first character of B. Each word must be used exactly once. For example, with words = ["ant", "dog", "tamarind", "nausea", "gun"], we can form: ant → tamarind → dog → gun → nausea → ant (circle completed). Algorithm This problem is solved using graph theory concepts ? Build a directed graph where each character is a node Add edges from ...

Read More
Showing 4761–4770 of 25,466 articles
« Prev 1 475 476 477 478 479 2547 Next »
Advertisements