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
Python Articles
Page 338 of 855
Program to partitioning into minimum number of Deci- Binary numbers in Python
Given a decimal number as a string, we need to find the minimum number of deci-binary numbers whose sum equals the given number. A deci-binary number contains only digits 0 and 1. For example, if the input is "132", the output will be 3 because 132 can be expressed as the sum of three deci-binary numbers: 100 + 11 + 21 = 132. Algorithm The key insight is that the minimum number of deci-binary numbers needed equals the maximum digit in the input number. This is because: Each deci-binary number can contribute at most 1 ...
Read MoreProgram to find winner of stone game in Python
Suppose Amal and Bimal are playing a stone game where Amal goes first. The game works as follows − There are n stones in a pile. Each player can take a stone and receive points based on their individual valuation of that stone. We have two arrays A_Values and B_Values where A_Values[i] and B_Values[i] represent how Amal and Bimal value the ith stone respectively. The player with the highest total score wins. If scores are equal, it's a draw. Both players play optimally and know each other's valuations. Return 1 if Amal wins, -1 if Bimal wins, and ...
Read MoreProgram to find maximum width ramp in Python
A ramp in an array is a pair of indices (i, j) where i < j and nums[i] ≤ nums[j]. The width of a ramp is the difference (j - i). We need to find the maximum width ramp in the given array. For example, in the array [6, 0, 8, 2, 1, 5], the maximum width ramp is at indices (1, 5) where nums[1] = 0 and nums[5] = 5, giving us a width of 4. Approach The solution uses a value-to-indices mapping approach ? Group indices by their values in a dictionary Process ...
Read MoreProgram to find sum of absolute differences in a sorted array in Python
Given a sorted array in non-decreasing order, we need to create a result array where each element contains the sum of absolute differences between that element and all other elements in the array. For example, with nums = [5, 7, 12], the result will be [9, 7, 12] because: |5−5| + |5−7| + |5−12| = 0+2+7 = 9 |7−5| + |7−7| + |7−12| = 2+0+5 = 7 |12−5| + |12−7| + |12−12| = 7+5+0 = 12 Naive Approach The straightforward approach calculates absolute differences for each element ? def solve_naive(nums): ...
Read MoreProgram to find equal sum arrays with minimum number of operations in Python
We are given two arrays nums1 and nums2 with values between 1 and 6 (inclusive). In one operation, we can update any value in either array to any value between 1 and 6. Our goal is to find the minimum number of operations needed to make the sum of values in nums1 equal to the sum of values in nums2. So, if the input is like nums1 = [1, 5, 6], nums2 = [4, 1, 1], then the output will be 2 because we can change nums2 from [4, 1, 1] to [4, 1, 6] in first operation, and ...
Read MoreProgram to find concatenation of consecutive binary numbers in Python
When working with binary representations, we often need to concatenate binary strings of consecutive numbers. This problem asks us to find the decimal value of a binary string formed by concatenating binary representations of numbers from 1 to n. So, if the input is like n = 4, then the output will be 220 because concatenating binary representations from 1 to 4 gives us "1" + "10" + "11" + "100" = "1101111100", which equals 220 in decimal. Algorithm Steps To solve this problem, we follow these steps − Initialize ans := 1 (binary representation ...
Read MoreProgram to find closest dessert cost in Python
In Python, we can solve the closest dessert cost problem by systematically exploring all possible combinations of bases and toppings. Given arrays of baseCosts and toppingCosts with a target value, we need to find the dessert cost closest to the target. Problem Rules There must be exactly one base We can add zero, one, or more toppings At most two of each type of topping can be used If multiple costs are equally close to target, return the lower one Algorithm Approach We use a bitmask approach to represent topping combinations. Each position in ...
Read MoreProgram to find max number of K-sum pairs in Python
Suppose we have an array called nums and another value k. In one operation, we can select two elements from nums whose sum equals k and remove them from the array. We have to find the maximum number of operations we can perform on the array. So, if the input is like nums = [8, 3, 6, 1, 5] and k = 9, then the output will be 2 as we can delete [3, 6] whose sum is 9, then remove [8, 1] whose sum is also 9. Algorithm To solve this, we will follow these steps: ...
Read MoreProgram to check whether string Is transformable with substring sort operations in Python
When working with string transformations, we sometimes need to check if one numeric string can be transformed into another using substring sort operations. This problem involves selecting any substring and sorting it in ascending order to eventually match a target string. The key insight is that when we sort a substring, smaller digits can move left past larger digits, but larger digits cannot move left past smaller digits that come after them in the original string. Algorithm Approach We use a greedy approach with position tracking ? Store positions of each digit in reverse order ...
Read MoreProgram to find out if the graph is traversable by everybody in Python
In graph theory, we sometimes need to determine if a graph can be traversed by multiple entities with different traversal capabilities. This problem involves finding the minimum number of edges to remove so that both Jack and Casey can traverse the entire graph. Jack can traverse edges with weight 1, Casey can traverse edges with weight 2, and both can traverse edges with weight 3. We need to ensure both people can reach all vertices by removing the minimum number of edges. Problem Analysis The solution uses Union-Find (Disjoint Set Union) data structure to track connected components. ...
Read More