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 543 of 2109
Find the minimum sum of distance to A and B from any integer point in a ring of size N in Python
We need to find the minimum sum of distances from any integer point X to two given points A and B on a ring of size N. The goal is to position X such that the total distance (distance from X to A + distance from X to B) is minimized, where X cannot be the same as A or B. In a ring structure, we can move in two directions: clockwise and counter-clockwise. The optimal position for X will depend on which path gives us the minimum total distance. Example Walkthrough If N = 30, A ...
Read MoreFind the minimum positive integer such that it is divisible by A and sum of its digits is equal to B in Python
Given two numbers A and B, we need to find the minimum positive integer M such that M is divisible by A and the sum of its digits equals B. If no such number exists, return -1. For example, if A = 50 and B = 2, the output will be 200 because 200 is divisible by 50 and the sum of its digits (2 + 0 + 0 = 2) equals B. Approach We'll use a breadth-first search (BFS) approach with the following strategy ? Use a queue to explore numbers digit by digit ...
Read MoreFind the minimum of maximum length of a jump required to reach the last island in exactly k jumps in Python
Suppose we have an array A of numbers, where the i-th number represents the position of an island. Given another integer k (1 ≤ k < N), a person standing on the 0-th island must reach the last island in exactly k jumps. We need to find the minimum of the maximum jump length required for this journey. All island positions are given in ascending order. For example, if A = [7, 20, 41, 48] and k = 2, the output will be 28. There are two possible paths: Path 1: 7 → 20 → 48 (jumps: ...
Read MoreFind the minimum number of preprocess moves required to make two strings equal in Python
Given two strings P and Q of equal length containing only lowercase letters, we need to find the minimum number of preprocessing moves required to make them equal. A preprocessing move allows changing any character in string P to any other lowercase letter. After preprocessing, we can apply these operations to make the strings equal: Select any index i and swap characters P[i] and Q[i] Select any index i and swap characters P[i] and P[n − i − 1] Select any index i and swap characters Q[i] and Q[n − i − 1] Note: The ...
Read MoreFind the minimum number of moves needed to move from one cell of matrix to another in Python
Finding the minimum number of moves to navigate from one cell to another in a matrix is a classic pathfinding problem. We can solve this using Breadth-First Search (BFS) algorithm by treating the matrix as a graph where each cell is a node. Problem Understanding In this problem, we have an N × N matrix with specific cell values ? Cell with value 1 indicates Source Cell with value 2 indicates Destination Cell with value 3 indicates Blank cell (walkable) Cell with value 0 indicates Wall (blocked) We can move in four directions: up, ...
Read MoreFind the Minimum length Unsorted Subarray, sorting which makes the complete array sorted in Python
When working with unsorted arrays, we often need to find the minimum length subarray that, when sorted, makes the entire array sorted. For example, in the array [2, 6, 4, 8, 10, 9, 15], sorting the subarray [6, 4, 8, 10, 9] at positions 1-5 would make the entire array sorted. Algorithm Approach The solution compares the original array with its sorted version to identify mismatched positions ? Create a sorted copy of the input array Compare each element with its sorted counterpart Track all positions where elements differ Return the distance between first and last ...
Read MoreFind the minimum difference between Shifted tables of two numbers in Python
When we have two numbers p and q, we can create their multiplication tables and then shift them by values r and s respectively. The problem asks us to find the minimum difference between any terms in these shifted tables. A shifted table means we add the shift value to each element in the multiplication table. For example, if we have the table of 7: [7, 14, 21, 28, ...] and shift it by 6, we get [13, 20, 27, 34, ...]. Understanding the Problem Let's understand with an example where p = 7, q = 17, ...
Read MoreFind the minimum and maximum amount to buy all N candies in Python
Suppose there is a candy store where N different types of candies are available and the prices of all N different types of candies are given. The store also provides an attractive offer: when you buy a single candy, you can get a maximum of K different types of other candies for free. We need to find both the minimum and maximum amount of money required to buy all N different types of candies while utilizing the offer optimally. The key insight is: For minimum cost: Buy the cheapest candies and get expensive ones for free For ...
Read MoreFind the maximum repeating number in O(n) time and O(1) extra space in Python
Finding the maximum repeating number in an array with constraints of O(n) time and O(1) extra space requires a clever approach. Given an array of size n with elements in range 0 to k-1 (where k ≤ n), we can modify the original array to store frequency information. The key insight is to use the array indices as counters by adding k to the element at index A[i] % k for each element A[i]. Algorithm Steps For each element in the array, increment the value at index A[i] % k by k After processing, the element ...
Read MoreFind the maximum number of composite summands of a number in Python
Given a number N (where 1 ≤ N ≤ 10^9), we need to represent N as a sum of the maximum possible number of composite numbers. A composite number is a positive integer greater than 1 that has at least one positive divisor other than 1 and itself (like 4, 6, 8, 9, 10, etc.). For example, if N = 16, we can write it as 4 + 4 + 4 + 4 (4 summands) or 8 + 8 (2 summands). The maximum number of summands is 4. Approach We use dynamic programming with the smallest composite ...
Read More