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
Programming Articles
Page 256 of 2547
Program to find length of smallest sublist that can be deleted to make sum divisible by k in Python
When working with arrays and modular arithmetic, sometimes we need to find the shortest subarray to remove so that the remaining elements' sum is divisible by a given number k. This problem uses prefix sums and hash maps to efficiently find the optimal subarray to delete. Problem Understanding Given a list of positive integers and a positive number k, we need to find the length of the shortest subarray that can be deleted to make the remaining sum divisible by k. We cannot delete the entire array. Example For nums = [5, 8, 6, 3] and ...
Read MoreProgram to find minimum deletions to make strings strings in Python
Sometimes we need to find the minimum number of deletions required to make two strings equal. This is a classic dynamic programming problem that can be solved by finding the Longest Common Subsequence (LCS) and subtracting it from the total length of both strings. Given two lowercase strings s and t, we need to delete characters from either string to make them identical. The key insight is that we want to keep the maximum number of common characters and delete the rest. Problem Example If we have s = "pipe" and t = "ripe", we can delete ...
Read MoreProgram to find maximum profit after cutting rods and selling same length rods in Python
Suppose we have a list of rod lengths called rodLen. We also have two integers called profit and cost, representing profit per length and cost per cut. We can gain profit per unit length of a rod but we can only sell rods that are all of the same length. We can also cut a rod into two pieces such that their lengths are integers, but we have to pay cost amount for each cut. We can cut a rod as many times as we want. We have to find the maximum profit that we can make. So, if ...
Read MoreProgram to find maximum length of k ribbons of same length in Python
Suppose we have a list of positive numbers representing ribbon lengths and a value k. We can cut the ribbons as many times as we want, and we need to find the largest length r such that we can have k ribbons of length r. If no such solution exists, return -1. So, if the input is like ribbons = [1, 2, 5, 7, 15] and k = 5, then the output will be 5. We can cut the ribbon of size 15 into 3 pieces of length 5 each, cut the ribbon of size 7 into pieces of ...
Read MoreProgram to count substrings with all 1s in binary string in Python
Suppose we have a binary string s. We have to find the number of substrings that contain only "1"s. If the answer is too large, mod the result by 10^9+7. So, if the input is like s = "100111", then the output will be 7, because the substrings containing only "1"s are ["1", "1", "1", "1", "11", "11" and "111"]. Algorithm To solve this, we will follow these steps − Initialize a counter a to 0 for tracking consecutive 1s Initialize count to 0 for total substrings ...
Read MoreProgram to count number of square submatrices in given binary matrix in Python
A square submatrix is a subset of elements from a matrix arranged in a square pattern. In a binary matrix, we want to count square submatrices where all elements are 1. This problem uses dynamic programming to efficiently count all possible squares. Problem Understanding Given a binary matrix, we need to find the total number of square submatrices containing only 1s. For example: 0 1 1 0 1 1 This matrix has 5 square submatrices: one (2×2) square and four (1×1) squares. Algorithm Approach We use dynamic programming ...
Read MoreProgram to count number of intervals that is totally contained inside other intervals in Python
Suppose we have a list of intervals where each interval[i] has [start, end] values. We need to find the number of intervals that are completely contained inside other intervals. If an interval is contained by multiple other intervals, it should only be counted once. An interval [s0, e0] is inside another interval [s1, e1] when s1 ≤ s0 and e0 ≤ e1. So, if the input is like intervals = [[2, 6], [3, 4], [4, 7], [5, 5]], then the output will be 2, because [3, 4] is inside [2, 6] and [5, 5] is inside [4, 7]. ...
Read MorePython Pandas - Extract the hour from the DateTimeIndex with specific time series frequency
To extract hour from the DateTimeIndex with specific time series frequency, use the DateTimeIndex.hour property. This is particularly useful when working with time series data that has hourly frequency patterns. Creating DateTimeIndex with Hourly Frequency First, let's create a DateTimeIndex with hourly frequency using pd.date_range() ? import pandas as pd # DatetimeIndex with period 6 and frequency as H i.e. hour # The timezone is Australia/Sydney datetimeindex = pd.date_range('2021-10-20 02:35:55', periods=6, tz='Australia/Sydney', freq='H') # Display DateTimeIndex print("DateTimeIndex...") print(datetimeindex) DateTimeIndex... DatetimeIndex(['2021-10-20 02:35:55+11:00', '2021-10-20 03:35:55+11:00', ...
Read MoreProgram to find all contiguously increasing numbers in start end range in Python
Suppose we have two numbers start and end, we have to find a sorted list of integers such that every number e in range [start, end] both inclusive and the digits of e are contiguously increasing. An example of continuously increasing number is 5678, but 169 is not. So, if the input is like start = 10 end = 150, then the output will be [12, 23, 34, 45, 56, 67, 78, 89, 123] Algorithm To solve this, we will follow these steps − s := all 9 digits as a ...
Read MoreProgram to count number of ways to win at most k consecutive games in Python
We need to find the number of ways to play n games such that we never win more than k consecutive games. Each game can result in a win (W) or loss (L), and we must ensure no sequence of k+1 consecutive wins occurs. For example, if n = 3 and k = 2, the valid sequences are: "LLL", "WLL", "LWL", "LLW", "WWL", "LWW", "WLW" (7 ways total). Approach We'll use dynamic programming with memoization. The key insight is to track the current position in the game sequence and the number of consecutive wins so far ? ...
Read More