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
Articles by Arnab Chakraborty
Page 79 of 377
Program to count number of walls required to partition top-left and bottom-right cells in Python
In this problem, we need to find the minimum number of walls required to block all paths between the top-left cell (0, 0) and bottom-right cell (R-1, C-1) in a 2D binary matrix. The matrix contains 0s (empty cells) and 1s (existing walls), and we can only move in four directions (up, down, left, right). Problem Understanding Given a matrix where 0 represents empty cells and 1 represents walls, we need to determine the minimum number of additional walls to place so that there's no path from top-left to bottom-right. We cannot place walls on the start and ...
Read MoreProgram to count number of distinct characters of every substring of a string in Python
Given a lowercase string, we need to find the sum of distinct character counts across all possible substrings. A character is considered distinct in a substring if it appears exactly once. The result should be returned modulo 10^9 + 7 for large numbers. Problem Understanding For string s = "xxy", let's examine all substrings ? "x" (index 0): 1 distinct character "x" (index 1): 1 distinct character "y" (index 2): 1 distinct character "xx" (indices 0-1): 0 distinct characters (x appears twice) "xy" (indices 1-2): 2 distinct characters "xxy" (indices 0-2): 1 distinct character (only y ...
Read MoreProgram to count number of trailing zeros of minimum number x which is divisible by all values from 1 to k in Python
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 MoreProgram to Find Out the Maximum Final Power of a List in Python
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 MoreProgram to Find Out the Minimum Cost Possible from Weighted Graph in Python
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 MoreProgram to Find Out the Number of Corrections to be Done to Fix an Equation in Python
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 MoreProgram to Find Out the Points Achievable in a Contest in Python
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 MoreProgram to Find Out the Minimum Cost to Purchase All in Python
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 MoreProgram to Find Out the Strings of the Same Size in Python
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 MoreProgram to Find Out the Number of Squares in a Grid in Python
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