Programming Articles

Page 496 of 2547

Program to find number of sublists we can partition so given list is sorted finally in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 238 Views

Suppose we have a list of numbers called nums. We can partition the list into some individual sublists then sort each piece. We have to find the maximum number of sublists we can partition so that nums as a whole is sorted afterwards. So, if the input is like nums = [4, 3, 2, 1, 7, 5], then the output will be 2, as we can sort the sublists like [4, 3, 2, 1] and [7, 5]. Algorithm To solve this, we will follow these steps − count := 0 ...

Read More

Program to find number of ways we can split a palindrome in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 417 Views

Suppose we have a string s, we have to find the number of ways we can partition the string such that each part is a palindrome. So, if the input is like s = "xyyx", then the output will be 3, as we have splits like: ["x", "yy", "x"], ["x", "y", "y", "x"], ["xyyx"]. Approach To solve this, we will use dynamic programming with the following steps − Create a table of size n + 1 initialized with 0, except table[0] = 1 For each position i, check all possible substrings ending at i If ...

Read More

Program to generate all possible strings by taking the choices in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 519 Views

When working with strings that contain choices in square brackets, we need to generate all possible combinations. For example, [d|t|l]im[e|s] represents multiple possible strings where we can choose from the options within brackets. The pattern [a|b|c] means we can choose either a, b, or c. Our task is to find all possible strings by making these choices. Example For the input s = "[d|t|l]im[e|s]", the output will be ['dime', 'dims', 'lime', 'lims', 'time', 'tims']. Algorithm We'll use a recursive approach with backtracking ? If the string is empty, return ...

Read More

Program to find minimum possible maximum value after k operations in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 1K+ Views

Sometimes we need to minimize the maximum value in a list by performing a limited number of decrease operations. This problem requires finding the minimum possible maximum value after k operations where each operation subtracts 1 from any element. Problem Understanding Given a list of numbers and k operations, we can subtract 1 from any element k times. The goal is to minimize the maximum value in the resulting list. Example If we have nums = [3, 4, 6, 5] and k = 6 operations ? Decrease 6 three times: [3, 4, 3, 5] ...

Read More

Program to find number of sublists whose sum is given target in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 593 Views

Suppose we have a list of numbers called nums and another value target, we have to find the number of sublists whose sum is same as target. So, if the input is like nums = [3, 0, 3] and target = 3, then the output will be 4, as we have these sublists whose sum is 3: [3], [3, 0], [0, 3], [3]. Algorithm Approach To solve this, we will follow these steps − temp := an empty map temp[0] := 1 s := 0 ans := 0 for i in range 0 to size ...

Read More

Program to find number of K-Length sublists whose average is greater or same as target in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 308 Views

Suppose we have a list nums, and two additional values k and target, we have to find the number of sublists whose size is k and its average value ≥ target. So, if the input is like nums = [1, 10, 5, 6, 7] k = 3 target = 6, then the output will be 2, as the sublist [1, 10, 5] has average value of 5.33 (less than 6), [10, 5, 6] has average of 7, and [5, 6, 7] has average of 6. Algorithm To solve this efficiently using a sliding window approach, we will ...

Read More

Program to count number of fraction pairs whose sum is 1 in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 2K+ Views

Suppose we have a list of fractions where each fraction is represented as [numerator, denominator]. We need to find the number of pairs of fractions whose sum equals 1. So, if the input is like fractions = [[2, 7], [3, 12], [4, 14], [5, 7], [3, 4], [1, 4]], then the output will be 4, as (2/7 + 5/7), (3/12 + 3/4), (3/4 + 1/4), (4/14 + 5/7) are the four pairs which sum to 1. Algorithm To solve this, we follow these steps ? Create a dictionary to store fraction frequencies in reduced form ...

Read More

Program to find number not greater than n where all digits are non-decreasing in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 376 Views

Given a number n, we need to find the largest number smaller or equal to n where all digits are in non-decreasing order (each digit is greater than or equal to the previous digit). For example, if the input is n = 221, the output will be 199 because 199 has digits in non-decreasing order (1 ≤ 9 ≤ 9) and is the largest such number ≤ 221. Algorithm The approach works by scanning digits from right to left and fixing any decreasing sequences ? Convert the number into a list of digits Scan from ...

Read More

Program to generate first n lexicographic numbers in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 399 Views

Lexicographic ordering sorts numbers as strings rather than numeric values. For example, "10" comes before "2" because the first character '1' comes before '2' in dictionary order. So, if the input is like n = 15, then the output will be [1, 10, 11, 12, 13, 14, 15, 2, 3, 4, 5, 6, 7, 8, 9] Algorithm Steps To solve this, we will follow these steps ? count := 1 ans := a list with single element count while size of ans < ...

Read More

Program to find number of distinct combinations that sum up to k in python

Arnab Chakraborty
Arnab Chakraborty
Updated on 25-Mar-2026 464 Views

Suppose we have a list of distinct numbers called nums and another number k, we have to find the number of distinct combinations that sum up to k. You can reuse numbers when creating combinations. So, if the input is like nums = [2, 4, 5] and k = 4, then the output will be 2, as we can make two such groups like [2, 2] and [4]. Algorithm This problem uses dynamic programming approach. We create a table where table[i] represents the number of ways to sum up to i. To solve this, we will ...

Read More
Showing 4951–4960 of 25,466 articles
« Prev 1 494 495 496 497 498 2547 Next »
Advertisements