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 483 of 2547
Program to find length of longest arithmetic subsequence of a given list in Python
An arithmetic subsequence is a sequence where the difference between consecutive elements is constant. Given a list of numbers, we need to find the length of the longest arithmetic subsequence that can be formed. For example, if we have nums = [1, 4, 7, 10, 13, 20, 16], the longest arithmetic subsequence is [1, 4, 7, 10, 13, 16] with length 6, where each consecutive difference is 3. Algorithm Approach We use dynamic programming with a dictionary to store subsequence lengths. For each pair of elements, we calculate their difference and track the longest subsequence ending at ...
Read MoreProgram to check whether we can split list into consecutive increasing sublists or not in Python
Suppose we have a list of numbers called nums that is sorted in non-decreasing order. We need to check whether it can be split into subsequences where each subsequence has a minimum length of 3 and contains consecutively increasing numbers. So, if the input is like nums = [2, 3, 4, 4, 5, 6, 7], then the output will be True, as we can split the list into [2, 3, 4] and [4, 5, 6, 7]. Algorithm Approach To solve this problem, we will follow these steps − counts − A ...
Read MoreProgram to split lists into strictly increasing sublists of size greater than k in Python
Sometimes we need to split a list into sublists where each sublist is strictly increasing and has a minimum size. This problem asks us to determine if such a split is possible for a given list and minimum size k. The key insight is that duplicate elements cannot be in the same strictly increasing sublist. If we have too many duplicates, we might not be able to form enough sublists of the required minimum size. Algorithm To solve this problem, we follow these steps ? Count the frequency of each element in the list Find ...
Read MoreProgram to find largest sum of 3 non-overlapping sublists of same sum in Python
Finding the largest sum of three non-overlapping sublists of the same size is a dynamic programming problem. We need to efficiently calculate sublist sums and track the maximum possible combinations. So, if the input is like nums = [2, 2, 2, -6, 4, 4, 4, -8, 3, 3, 3] k = 3, then the output will be 27, as we can select the sublists [2, 2, 2], [4, 4, 4], and [3, 3, 3], total sum is 27. Algorithm Steps To solve this, we will follow these steps − Create prefix ...
Read MoreProgram to find largest binary search subtree from a given tree in Python
A binary tree may contain subtrees that are valid binary search trees (BST). We need to find the largest subtree (with maximum number of nodes) that forms a valid BST from the given binary tree. So, if the input is like ? 12 3 5 4 6 ...
Read MoreProgram to find number of ways we can reach from top left point to bottom right point in Python
Suppose we have an N x M binary matrix where 0 represents an empty cell and 1 represents a blocked cell. Starting from the top-left corner, we need to find the number of ways to reach the bottom-right corner. We can only move right or down. So, if the input matrix is ? 001 000 110 Then the output will be 2, as there are two valid paths: [Right, Down, Right, Down] and [Down, Right, Right, Down]. Algorithm We use dynamic programming to solve this problem. The approach involves ? ...
Read MoreProgram to count number of sublists with exactly k unique elements in Python
Sometimes we need to count sublists that contain exactly k unique elements from a given list. This problem can be solved using a sliding window approach with the key insight that sublists with exactly k unique elements equals sublists with "at most k" minus sublists with "at most k-1" unique elements. So, if the input is like nums = [2, 2, 3, 4] and k = 2, then the output will be 3, as we have the sublists like: [2, 2, 3], [2, 3], [3, 4]. Algorithm Approach To solve this, we will follow these steps ? ...
Read MoreProgram to find lexicographically smallest subsequence of size k in Python
Given a list of numbers and a value k, we need to find the lexicographically smallest subsequence of size k. A subsequence maintains the relative order of elements from the original list. Problem Understanding For example, if we have nums = [2, 3, 1, 10, 3, 4] and k = 3, we need to select 3 numbers that form the smallest possible sequence when compared lexicographically. The answer would be [1, 3, 4]. Algorithm Approach To solve this problem, we use a greedy approach ? For each position in our result, find the smallest ...
Read MoreProgram to get maximum profit by scheduling jobs in Python
Suppose we have a list of intervals where each interval contains three values [start, end, profit]. We can perform only one task at a time, and we have to find the maximum amount of profit we can get by scheduling non-overlapping jobs. So, if the input is like intervals = [[1, 2, 100], [3, 5, 40], [6, 19, 150], [2, 100, 250]], then the output will be 350, as we can take these two intervals [1, 2, 100] and [2, 100, 250]. Algorithm Overview To solve this problem, we will use dynamic programming with the following approach: ...
Read MoreProgram to find number of increasing subsequences of size k in Python
Given a list of numbers and a value k, we need to find the number of strictly increasing subsequences of size k. A subsequence maintains the relative order of elements but doesn't need to be contiguous. So, if the input is like nums = [2, 3, 4, 1] and k = 2, then the output will be 3, as we have the subsequences of size 2: [2, 3], [3, 4], [2, 4]. Approach Using Dynamic Programming We use dynamic programming where dp[i] represents the number of increasing subsequences of current length ending at position i ? ...
Read More