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 count minimum k length sublist that can be flipped to make all items of list to 0 in Python
Suppose we have a list of numbers called nums that contains 0s and 1s, and another value k representing the sublist length. We can perform an operation where we flip a sublist of length k such that all 1s become 0s and all 0s become 1s. We need to find the minimum number of operations required to change all numbers in nums to 0s. If it's impossible, we return -1. So, if the input is like nums = [1, 1, 1, 0, 0, 1, 1, 1], k = 3, then the output will be 2, as we can ...
Read MoreProgram to find minimum radius to light up all houses on a street in Python
Suppose we have a list of numbers called nums that represent positions of houses on a 1-dimensional line. We have 3 street lights that can be placed anywhere, and a light at position x with radius r lights up all houses in range [x - r, x + r], inclusive. We need to find the smallest radius r required to light up all houses. For example, if the input is nums = [4, 5, 6, 7], then the output will be 0.5. We can place the lamps at positions 4.5, 5.5, and 6.5 with radius r = 0.5 to ...
Read MoreProgram to find minimum digits sum of deleted digits in Python
When working with two digit strings, we sometimes need to remove characters to make them identical while minimizing the sum of deleted digits. This problem can be solved using dynamic programming with a variation of the Longest Common Subsequence (LCS) algorithm. Given two strings s and t containing only digits, we need to remove digits from both strings so that the remaining strings are identical, and the sum of deleted digits is minimized. Example If the input is s = "41272" and t = "172", we can remove "4" and "2" from the first string to get ...
Read MoreProgram to find minimum string size that contains given substring in Python
Given two strings s and t, we need to find the length of the minimum substring in s that contains all characters of t. If no such substring exists, we return -1. This is known as the Minimum Window Substring problem. For example, if s = "thegrumpywizardmakes" and t = "wake", the shortest substring containing all characters of "wake" is "wizardmake" with length 10. Algorithm We use the sliding window technique with two pointers ? Create a frequency counter for characters in string t Use two pointers (start and end) to maintain a sliding window ...
Read MoreProgram to count number of minimum swaps required to make it palindrome in Python
A palindrome is a string that reads the same forwards and backwards. To transform a string into a palindrome using minimum adjacent swaps, we need to check if it's possible and then calculate the swaps required. For a string to form a palindrome, at most one character can have an odd frequency (which would be the middle character in odd-length palindromes). Algorithm The solution involves two steps: Check if palindrome formation is possible by counting character frequencies Use two pointers to match characters from both ends, swapping adjacent elements when needed Example ...
Read MoreProgram to find minimum possible difference of indices of adjacent elements in Python
Given a list of numbers, two numbers nums[i] and nums[j] are considered adjacent when there is no number with a value between them in the original list. We need to find the minimum possible difference of indices |j - i| such that nums[j] and nums[i] are adjacent. For example, if the input is nums = [1, -9, 6, -6, 2], the output will be 2, as we can see that 2 and 6 are adjacent elements (no value between 2 and 6 exists in the list) and they are 2 indices apart. Algorithm Approach To solve this ...
Read MoreProgram to count number of words we can generate from matrix of letters in Python
Suppose we have a 4 x 4 board of letters and a list of words, we have to find the largest number of words that can be generated in the board by a sequence of adjacent letters, using one cell at most once per word (but we can reuse cells for other words). We can go up, down, left, right, or diagonal direction. Problem Example If the input is like ? ...
Read MoreProgram to find intervals by merging target interval in Python
Suppose we have a list of non-overlapping intervals sorted by end time. We need to merge a target interval into this list while maintaining the non-overlapping and sorted properties. For example, if we have intervals = [[1, 15], [25, 35], [75, 90]] and target = [10, 30], the output will be [[1, 35], [75, 90]] because the target interval [10, 30] overlaps with both [1, 15] and [25, 35], merging them into a single interval [1, 35]. Algorithm To solve this problem, we follow these steps: Add the target interval to the list of intervals ...
Read MoreProgram to find list of elements which are less than limit and XOR is maximum in Python
Suppose we have a list of numbers nums and a list of queries where each query contains [x, limit]. We have to find a list such that for each query [x, limit], we find an element e in nums such that e ≤ limit and e XOR x is maximized. If there is no such element, return -1. Problem Understanding For example, if nums = [3, 5, 9] and queries = [[4, 6], [2, 0]], then the output will be [3, -1]. For the first query, we can use 3 or 5 from nums (both ≤ 6). 3 ...
Read MoreProgram to find length of subsequence that can be removed still t is subsequence of s in Python
Suppose we have a string s and another string t, where t is a subsequence of s. We need to find the maximum length of a substring that can be removed from s such that t remains a subsequence of the remaining string. So, if the input is like s = "xyzxyxz" and t = "yz", then the output will be 4, as we can remove a substring of length 4 while keeping "yz" as a subsequence. Approach The algorithm works by considering three scenarios ? Remove a suffix after matching all characters of t ...
Read More