Partition a List into K Equal Sum Parts in C++

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

180 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

Count Sublists with Exactly K Unique Elements in Python

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

199 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

Find Sum of K Non-Overlapping Sublists with Maximum Sum in C++

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

225 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

Find Lexicographically Smallest Subsequence of Size K in Python

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

349 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

Justify Words into Same Length Lines in C++

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

104 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

Find Minimum Sum of Difficulties to Complete Jobs Over K Days in C++

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

201 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

Get Maximum Profit by Scheduling Jobs in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:53:00

838 Views

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, we have to find the most amount of profit we can get.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]To solve this, we will follow these stepsd := an empty map that contains lists as valuesn := 0for each (start, end, profit) in intervals, ... Read More

Find All Possible IP Addresses After Restoration in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:49:18

356 Views

Suppose we have a string with only digits, we have to restore it by forming all possible valid IP address combinations. We know that a valid IP address consists of exactly four integers (each integer is in range 0 to 255) separated by single period symbol.So, if the input is like ip = "25525511136", then the output will be ["254.25.40.123", "254.254.0.123"]To solve this, we will follow these steps −Define a function convertToNum(), this will take s, start, end, num := 0for initialize i := start, when i 255, then −return 10000return numDefine a function addDots(), this will take positions, ... Read More

Find Number of Increasing Subsequences of Size K in Python

Arnab Chakraborty
Updated on 12-Dec-2020 09:44:40

489 Views

Suppose we have a list of numbers called nums and also another value k, we have to find the number of subsequences of size k that are strictly increasing. If the answer is very large, mod it by 10^9 + 7.So, if the input is like nums = [2, 3, 4, 1] k = 2, then the output will be 3, as we have the subsequences of size 2: [2, 3], [3, 4], [2, 4].To solve this, we will follow these steps −m := 10^9 + 7dp := a list of size same as nums and fill with 1iterate the ... Read More

Find Cost to Reach Final Index of Two Lists in Python

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

174 Views

Suppose we have two lists of numbers nums0 and nums1 of the same length and two other values d as distance and c as cost. If we start from index 0 at either nums0 or nums1 and want to end up at the final index of either list. Now, in each round, we can select to switch to the other list for cost of cost. Then we can jump forward at most d distance away where the c cost of landing at an index is the value at that point. So we have to find the minimum total cost possible ... Read More

Advertisements