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
Server Side Programming Articles
Page 537 of 2109
Minimum Cost to cut a board into squares in Python
Suppose we have a board of length p and width q; we have to break this board into p*q number of squares such that cost of breaking is as minimum as possible. Cutting cost for each edge will be given. The key insight is to use a greedy approach: always make the most expensive cut first, because expensive cuts will affect more pieces if made later. Problem Understanding Given: X_slice: costs of horizontal cuts Y_slice: costs of vertical cuts We need to find the minimum total cost to cut the board into unit squares. ...
Read MoreFind the time which is palindromic and comes after the given time in Python
A palindromic time is a time string that reads the same forwards and backwards. Given a time in 24-hour format (HH:MM), we need to find the next closest palindromic time. For example, "12:21" is palindromic, but "12:34" is not. The algorithm checks if we can form a palindrome with the current hour by reversing it to get the minutes. If not, we increment the hour and try again. Algorithm Steps The solution follows these steps − Extract the hour and minute from the input time string Reverse the hour digits to get potential palindromic minutes ...
Read MoreFind the Surface area of a 3D figure in Python
Finding the surface area of a 3D figure represented by a matrix involves calculating the exposed surfaces of each building block. Each cell A[i][j] represents the height of a building at position (i, j). Algorithm The surface area calculation considers ? Top and bottom surfaces ? Each cell contributes 2 units (top and bottom) Side surfaces ? Calculate height differences between adjacent cells Border surfaces ? Add full height for cells at matrix edges Step-by-Step Approach Initialize result = 0 For each cell (i, j), calculate height differences with top and left ...
Read MoreFind the sums for which an array can be divided into subarrays of equal sum in Python
Given an array of integers, we need to find all possible sum values for which the array can be divided into contiguous subarrays where each subarray has the same sum. If no such division is possible, we return -1. For example, if the input array is [2, 4, 2, 2, 2, 4, 2, 6], the output will be [6, 8, 12] because the array can be divided into subarrays with these equal sums: Sum 6: [{2, 4}, {2, 2, 2}, {4, 2}, {6}] Sum 8: [{2, 4, 2}, {2, 2, 4}, {2, 6}] Sum 12: [{2, 4, ...
Read MoreMinimize (max(A[i], B[j], C[k]) – min(A[i], B[j], C[k])) of three different sorted arrays in Python
Given three sorted arrays A, B, and C of different sizes, we need to find the minimum absolute difference between the maximum and minimum values of any triplet (A[i], B[j], C[k]) where each element comes from a different array. The key insight is to use a greedy approach: start from the largest elements of each array and progressively reduce the maximum element to minimize the difference. Algorithm Steps The algorithm works as follows ? Start with pointers at the end of each array (largest elements) Calculate the current difference between max and min of the ...
Read MoreFind the sum of maximum difference possible from all subset of a given array in Python
Given an array of n values, we need to find the sum of maximum differences possible from all subsets. For each subset, we calculate max(subset) - min(subset), then sum these differences across all possible subsets. For example, if we have array [1, 3, 4], the subsets and their max-min differences are: {1}: max=1, min=1, difference=0 {3}: max=3, min=3, difference=0 {4}: max=4, min=4, difference=0 {1, 3}: max=3, min=1, difference=2 {1, 4}: max=4, min=1, difference=3 {3, 4}: max=4, min=3, difference=1 {1, 3, 4}: max=4, min=1, difference=3 Total sum = 0+0+0+2+3+1+3 = 9 Algorithm Approach Instead ...
Read MoreFind the sum of all Truncatable primes below N in Python
A truncatable prime is a prime number that remains prime when digits are successively removed from either the left (left-truncatable) or right (right-truncatable) side. A number must be both left-truncatable and right-truncatable to be considered truncatable. For example, 3797 is truncatable because 3797, 797, 97, 7 (left truncation) and 3797, 379, 37, 3 (right truncation) are all prime numbers. Algorithm We use the Sieve of Eratosthenes to precompute all primes, then check each number for truncatability ? Generate all primes up to a reasonable limit using the sieve For each prime number, check if it's left-truncatable ...
Read MoreFind the smallest window in a string containing all characters of another string in Python
Finding the smallest window in a string that contains all characters of another string is a classic sliding window problem. This technique uses two pointers to efficiently find the minimum substring. Problem Statement Given two strings s1 and s2, we need to find the smallest substring in s1 that contains all characters of s2. For example, if s1 = "I am a student" and s2 = "mdn", the output should be "m a studen". Algorithm Steps The sliding window approach works as follows: Create frequency arrays for both pattern and current window characters ...
Read MoreFind the smallest positive integer value that cannot be represented as sum of any subset of a given array in Python
In some programming problems, we need to find the smallest positive integer that cannot be represented as the sum of any subset from a given sorted array. This is a classic greedy algorithm problem that can be solved efficiently in O(n) time complexity. So, if the input is like A = [1, 4, 8, 12, 13, 17], then the output will be 2, because we can represent 1 using [1], but we cannot represent 2 using any subset combination. Algorithm Approach The key insight is that if we can represent all numbers from 1 to answer-1, and the current element A[i]
Read MoreFind the probability of a state at a given time in a Markov chain - Set 1 in Python
A Markov chain is a random process where the probability of moving to the next state depends only on the current state. We can represent it as a directed graph where nodes are states and edges have transition probabilities. To find the probability of reaching state F at time T starting from state S, we use dynamic programming. Problem Statement Given a Markov chain with N states, we need to find the probability of reaching state F at time T if we start from state S at time 0. Each transition takes one unit of time, and the ...
Read More