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 299 of 2109
Program 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 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 More