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

Arnab Chakraborty
Updated on 25-Mar-2026 12:45:05

612 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
Updated on 25-Mar-2026 12:44:49

319 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
Updated on 25-Mar-2026 12:44:25

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
Updated on 25-Mar-2026 12:44:01

388 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
Updated on 25-Mar-2026 12:43:42

407 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
Updated on 25-Mar-2026 12:43:26

476 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

Program to find minimum number of movie theatres required to show all movies in python

Arnab Chakraborty
Updated on 25-Mar-2026 12:43:07

384 Views

Suppose we have a list of intervals for different movie showings (they can be overlapped), we have to find the minimum number of theatres required to be able to show all of the movies. So, if the input is like intervals = [[20, 65], [0, 40], [50, 140]], then the output will be 2, as [20, 65] and [0, 40] are overlapping. [20, 65] and [50, 140] are also overlapping but [0, 40] and [50, 140] are not. So we need 2 theaters. Algorithm To solve this, we will follow these steps ? Create a ... Read More

Program to find most occurring number after k increments in python

Arnab Chakraborty
Updated on 25-Mar-2026 12:42:46

246 Views

Suppose we have a list of numbers called nums and another value k. We can perform an operation where we increase some element by one, at most k times. We have to find the value of the most frequently occurring number we can obtain after these increments. If there are multiple solutions, we select the smallest possible number. So, if the input is like nums = [1, 0, 0, 0, 8, 8, 8, 8] and k = 8, then the output will be 8. We can increase 1 seven times to get 8, and increase any 0 to 1, ... Read More

Program to find sum of minimum trees from the list of leaves in python

Arnab Chakraborty
Updated on 25-Mar-2026 12:42:21

301 Views

Suppose we have a list of numbers called nums. This list represents the leaf nodes in inorder traversal of a binary tree. The internal nodes have exactly 2 children and their value equals the product of the largest leaf value of its left subtree and the largest leaf value of its right subtree. We need to find the sum of all values in the tree that produces the minimum total sum. So, if the input is like nums = [3, 5, 10], then the output will be 83. ... Read More

Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python

Arnab Chakraborty
Updated on 25-Mar-2026 12:41:52

1K+ Views

Suppose we have a list of numbers called nums, we have to find a minimum sum subsequence from the given list such that at least one number for all groups of three consecutive numbers is selected. If the length of given list is less than 3, a number should still be selected. So, if the input is like nums = [2, 3, 4, 5, 6, 7], then the output will be 7, as we can select 2 and 5. Algorithm To solve this problem, we will use dynamic programming with the following approach ? ... Read More

Advertisements