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 43 of 377
Program 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 MoreProgram to detect cycles in 2D grid in Python
Cycle detection in a 2D grid is a common graph problem where we need to find if there exists a path that starts and ends at the same cell. A cycle must have at least 4 cells and we can only move to adjacent cells with the same character value. Problem Understanding Given a 2D grid of characters, we need to detect if there's a cycle. The conditions are ? A cycle has length 4 or more We can move in four directions (up, down, left, right) Adjacent cells must have the same character Cannot revisit ...
Read MoreProgram to find minimum number of days to eat N oranges in Python
Suppose we have n oranges in the kitchen. We can eat some oranges every day following these rules: Eat 1 orange If n is even, eat n/2 oranges If n is divisible by 3, eat 2*(n/3) oranges We can select only one option each day. We need to find the minimum number of days to eat all n oranges. Example Walkthrough If the input is n = 10, the output will be 4 days ? Day 1: Eat 1 orange, 10 − 1 = 9 Day 2: Eat 6 oranges (2*(9/3)), 9 ...
Read MoreProgram to find minimum cost to cut a stick in Python
Given a wooden stick of length n and an array of cut positions, we need to find the minimum cost to make all cuts. The cost of each cut equals the length of the current stick segment being cut. This is a classic dynamic programming problem that can be solved optimally by choosing the right order of cuts. Problem Understanding Consider a stick of length 7 with cuts at positions [5, 1, 4, 3]. The total cost depends on the order we make the cuts ? Each cut costs the length of the current stick segment ...
Read More