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 on Trending Technologies
Technical articles with clear explanations and examples
Program 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 MoreProgram to find longest awesome substring in Python
An awesome substring is a non-empty substring where we can rearrange characters through swaps to form a palindrome. To create a palindrome, at most one character can have an odd frequency. We need to find the length of the longest awesome substring in a numeric string. For example, in string "4353526", the substring "35352" (length 5) is awesome because we can rearrange it to form the palindrome "35253". Algorithm Overview We use a bitmask approach where each bit represents whether a digit (0-9) has appeared an odd number of times. Two positions with the same bitmask or ...
Read MoreProgram to find the maximum score from all possible valid paths in Python
Suppose we have two arrays nums1 and nums2. A valid path is defined as follows − Select nums1 or nums2 to traverse (from index-0). Traverse the array from left to right. Now, if we are moving through any value that is present in nums1 and nums2 we can change the path to the other array. Here the score is the sum of unique values in a valid path. We have to find the maximum score we can get of all possible valid paths. If the answer is too large then ...
Read MoreProgram to find min length of run-length encoding after removing at most k characters in Python
Run-length encoding is a string compression method that replaces consecutive identical characters with the character followed by its count. For example, "xxyzzz" becomes "x2yz3". In this problem, we need to find the minimum length of the run-length encoded string after removing at most k characters. Problem Understanding Given a string s and integer k, we can delete at most k characters to minimize the run-length encoded length. The key insight is that we want to create longer consecutive sequences by strategically removing characters. Example For s = "xxxyzzzw" and k = 2: Original: "xxxyzzzw" → ...
Read MoreProgram to find maximum score from performing multiplication operations in Python
Suppose we have two arrays nums and multipliers of size n and m respectively (n >= m). We want to perform exactly m operations to achieve the maximum score. Our initial score is 0. In each operation, we can: Select one value x from either the start or the end of the nums array Add multipliers[i] * x to the score Remove x from the array nums We need to find the maximum score after performing m operations. Example If the input is nums = [5, 10, ...
Read MoreProgram to find minimum number of increments on subarrays to form a target array in Python
Suppose we have an array called target with positive values. Now consider an array initial of same size with all zeros. We have to find the minimum number of operations required to generate a target array from the initial if we do this operation: Select any subarray from initial and increment each value by one. So, if the input is like target = [2, 3, 4, 3, 2], then the output will be 4. How It Works The key insight is that we only need to increment when moving to a higher value. When the array decreases, ...
Read MoreProgram to find minimum number of operations to move all balls to each box in Python
Suppose we have a binary string called boxes, where boxes[i] is '0' indicates the ith box is empty, and '1' indicates it contains one ball. In one operation, we can move one ball from a box to an adjacent box. We need to find an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box. Problem Understanding If the input is boxes = "1101", then the output will be [4, 3, 4, 5] ? To put all balls in first box: take from ...
Read MoreProgram to find maximum number of non-overlapping substrings in Python
Given a string with lowercase letters, we need to find the maximum number of non-overlapping substrings where each substring contains all occurrences of every character it includes. Problem Understanding The algorithm must satisfy two conditions ? Substrings are non-overlapping A substring containing character 'ch' must include all occurrences of 'ch' in the string For example, if we have "pqstpqqprrr", the valid substrings are ["pqstpqqprrr", "pqstpqqp", "st", "s", "t", "rrr"]. We want the maximum count, so we choose ["s", "t", "rrr"]. Algorithm Approach The solution uses a greedy approach ? Find ...
Read MoreProgram to find best position for a service center in Python
Suppose we have a list of positions containing coordinate points where houses are located. We want to find the optimal location (xc, yc) for a service center such that the sum of Euclidean distances from all houses to the service center is minimized. This is known as the geometric median problem. So, if the input is like positions = [(10, 11), (11, 10), (11, 12), (12, 11)], then the output will be 4.0 ...
Read More