Find Number of Sublists to Partition a List in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:42:41

201 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 to such 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]To solve this, we will follow these steps:count := 0main_sum := 0, sorted_sum := 0for each element x from nums and y from sorted ... Read More

Find Number of Ways to Split a Palindrome in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:40:20

385 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"].To solve this, we will follow these steps:n := size of stable := a list of size n + 1 and fill it with 0table[0] := 1for i in range 0 to n, dofor j in range 0 to i - 1, dosub := s[from index j to ... Read More

Generate All Possible Strings by Taking Choices in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:36:28

467 Views

Suppose we have a string s of lowercase alphabet characters, other characters like "[", "|", and "]". Here "[a|b|c]" indicates either "a", "b", or "c" can be chosen as a possibility. We have to find a list of strings containing all possible values that s can represent. Here "[]" cannot be nested and may have any number of choices.So, if the input is like s = "[d|t|l]im[e|s]", then the output will be ['dime', 'dims', 'lime', 'lims', 'time', 'tims']To solve this, we will follow these steps:if s is empty, thenreturn a list with blank stringn := size of sseq := a ... Read More

Find Minimum Possible Maximum Value After K Operations in Python

Arnab Chakraborty
Updated on 26-Nov-2020 06:30:47

1K+ Views

Suppose we have a list of numbers called nums and another value k. Now let us consider an operation where we can subtract 1 from any element in the list. We can perform this operation k times. We have to find the minimum possible maximum value in the list after k such operations.So, if the input is like nums = [3, 4, 6, 5] k = 6, then the output will be 3, as we can decrease 4 once, 6 thrice and 5 twice to get [3, 3, 3, 3].To solve this, we will follow these steps:sort numbers in reverse ... Read More

Find Number of Only Child in a Binary Tree in Python

Arnab Chakraborty
Updated on 25-Nov-2020 13:05:33

567 Views

Suppose we have a binary tree; we have to find the number of nodes that are an only child. As we know a node x is called an only child node when its parent has exactly one child that is x.So, if the input is likethen the output will be 2 as 8 and 6 are the only children.To solve this, we will follow these steps:if root is null, thenreturn 0d := a double ended queueinsert root at the end of dcount := 0while d is not empty, docurrent := left element of d and delete left elementif left of ... Read More

Find Number of Swaps Required to Sort Sequence in Python

Arnab Chakraborty
Updated on 25-Nov-2020 13:01:41

2K+ Views

Suppose we have a list of distinct numbers; we have to find the minimum number of swaps required to sort the list in increasing order.So, if the input is like nums = [3, 1, 7, 5], then the output will be 2, as we can swap 3 and 1, then 5 and 7.To solve this, we will follow these steps:sort_seq := sort the list numstable := a new mapfor each index i and value n in nums, dotable[n] := iswaps := 0for i in range 0 to size of nums, don := nums[i]s_n := sort_seq[i]s_i := table[s_n]if s_n is not ... Read More

Find Number of Sublists Whose Sum is Given Target in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:59:39

549 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] target = 3, then the output will be 4, as we have these sublists whose sum is 3: [3], [3, 0], [0, 3], [3].To solve this, we will follow these steps:temp := an empty maptemp[0] := 1s := 0ans := 0for i in range 0 to size of nums, dos := s + nums[i]comp := s - targetif comp is in temp, thenans ... Read More

Find Number of K-Length Sublists with Average Greater or Equal to Target in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:57:25

274 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, 7] has average value of 6 and [10, 5, 6] has average of 7.To solve this, we will follow these steps:target := target * ksum := 0, ans := 0for each index i and number n in nums, doif i ... Read More

Count Fraction Pairs Whose Sum is 1 in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:55:17

2K+ Views

Suppose we have a list of fractions where each fraction is individual lists [numerator, denominator] which represents the number (numerator / denominator). We have to find the number of pairs of fractions whose sum is 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.To solve this, we will follow these steps:d := a new mapans := 0for each fraction i in fractions, ... Read More

Find Number Not Greater Than N with Non-Decreasing Digits in Python

Arnab Chakraborty
Updated on 25-Nov-2020 12:53:29

336 Views

Suppose we have a number n, we have to find the largest number smaller or equal to n where all digits are non-decreasing.So, if the input is like n = 221, then the output will be 199.To solve this, we will follow these steps:digits := a list with all digits in nbound := nullfor i in range size of digits - 1 down to 0, doif digits[i] < digits[i - 1], thenbound := idigits[i - 1] := digits[i - 1] - 1if bound is not null, thenfor i in range bound to size of digits, dodigits[i] := 9join each digit ... Read More

Advertisements