Found 33676 Articles for Programming

Program to find largest sum of 3 non-overlapping sublists of same sum in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:26:38

155 Views

Suppose we have a list of numbers called nums and another value k, we have to find the largest sum of three non-overlapping sublists of the given list of size k.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.To solve this, we will follow these steps −P := [0]for each x in A, doinsert P[-1] + x at the end of ... Read More

Program to find maximum sum of two sets where sums are equal in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:23:56

165 Views

Suppose we have a list of numbers called nums, now find two sets as their sums are same and maximum, then find sum value.So, if the input is like nums = [2, 5, 4, 6], then the output will be 6, as the sets are [2, 4] and [6].To solve this, we will follow these steps −sum := 0for each number i in nums, dosum := sum + in := size of numsDefine one 2D array dp of size (n + 1) x (2 * sum + 5) and fill with -1dp[0, sum] := 0for initialize i := 1, when ... Read More

Program to find largest binary search subtree from a given tree in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:19:16

160 Views

Suppose we have a binary tree, we have to find the largest subtree (with maximum number of nodes) as binary search tree.So, if the input is likethen the output will beTo solve this, we will follow these steps −max_size := [0]max_node := [null]Define a function traverse() . This will take nodeif node is null, thenreturn nullleft := traverse(left of node)right := traverse(right of node)lst := left + [value of node] + rightif lst is sorted, thenif max_size[0] < size of lst, thenmax_size[0] := size of lstmax_node[0] := nodereturn lsttraverse(root)From the main method return max_node[0]Example (Python)Let us see the following implementation ... Read More

Program to find number of ways we can reach from top left point to bottom right point in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:16:40

392 Views

Suppose we have one N x M binary matrix. Where 0 means empty cell and 1 means blocked cell. Now starting from the top left corner, we have to find the number of ways to reach the bottom right corner. If the answer is very large, mod it by 10^9 + 7.So, if the input is like001000110then the output will be 2, as There are two ways to get to the bottom right: [Right, Down, Right, Down] and [Down, Right, Right, Down].To solve this, we will follow these steps −dp := a matrix of same size of given matrix and ... Read More

Program to check whether we can partition a list with k-partitions of equal sum in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:12:50

171 Views

Suppose we have a list of numbers called nums and another value k, we have to check whether it is possible to partition nums into k different subsets where the sum of each subset are same.So, if the input is like nums = [4, 2, 6, 5, 1, 6, 3] k = 3, then the output will be True, as we can partition them like: [6, 3], [6, 2, 1], and [4, 5].To solve this, we will follow these steps −Define a function check(), this will take an array v, for initialize i := 1, when i < size of ... Read More

Program to count number of sublists with exactly k unique elements in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:08:47

193 Views

Suppose we have a list of numbers called nums and another value k, we have to find the number of sublists required that there are exactly k unique numbers in the sublist.So, if the input is like nums = [2, 2, 3, 4] k = 2, then the output will be 3, as we have the sublists like: [2, 2, 3], [2, 3], [3, 4].To solve this, we will follow these steps −Define a function count() . This will take Kslot := an empty map, by default all values are 0i := res := 0for each index j, and value ... Read More

Program to find sum of k non-overlapping sublists whose sum is maximum in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:07:09

215 Views

Suppose we have a list of numbers called nums, and another value k, we have to find the k non-overlapping, non-empty sublists such that the sum of their sums is maximum. We can consider k is less than or equal to the size of nums.So, if the input is like nums = [11, -1, 2, 1, 6, -24, 11, -9, 6] k = 3, then the output will be 36, as we can select the sublists [11, -1, 2, 1, 6], [11], and [6] to get sums of [19, 11, 6] = 36.To solve this, we will follow these steps ... Read More

Program to find lexicographically smallest subsequence of size k in Python

Arnab Chakraborty
Updated on 12-Dec-2020 10:04:26

334 Views

Suppose we have a list of numbers called nums and another value k, we have to find the lexicographically smallest subsequence of size k.So, if the input is like nums = [2, 3, 1, 10, 3, 4] k = 3, then the output will be [1, 3, 4]To solve this, we will follow these steps −l := size of nums, r := k - 1out := a new listfor j in range 0 to k, domn := nums[complement of r]for i in range r to l, doif mn >= nums[complement of i], thenmn := nums[complement of i]l := ir := ... Read More

Program to justify a set of words by converting them into same length lines in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:00:33

102 Views

Suppose we have an list of words and a width k, we have to arrange the text such that each line has exactly k number of characters and the text is fully justified. Here we shall pack our words as many words as we can insert in each line. And we shall pad extra spaces ' ' when necessary so that each line has exactly k characters.Here extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, empty slots on the left will be assigned ... Read More

Program to find minimum sum of difficulties to complete jobs over k days in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:55:42

189 Views

Suppose we have a list of numbers called jobs and another value k. Now we want to finish all jobs in k different days. The jobs must be performed in the given order and in each day we have to complete one task. The difficulty of job i is stored at jobs[i] and the difficulty of completing a list of jobs on a day will be the maximum difficulty job performed on that day. So we have to find the minimum sum of the difficulties to perform the jobs over k different days.So, if the input is like jobs = ... Read More

Advertisements