Partition to K Equal Sum Subsets in C++

Arnab Chakraborty
Updated on 29-Apr-2020 07:59:22

433 Views

Suppose we have an array of integers called nums and a positive integer k, check whether it's possible to divide this array into k non-empty subsets whose sums are all same. So if the array is like [4, 3, 2, 3, 5, 2, 1] and k = 4, then the result will be True, as the given array can be divided into four subarray like [[5], [1, 4], [2, 3], [2, 3]] with equal sums.To solve this, we will follow these steps −define two table called dp and total of size 2^n, sort the given array nums, set sum := ... Read More

Prime Arrangements in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:57:12

322 Views

We have to find the number of permutations of 1 to n, so the prime numbers are placed at prime indices. The answers may be large, return the answer modulo 10^9 + 7. So if n = 5, then output will be 12. So there will be 12 permutations. one possible permutation will be [1, 2, 5, 4, 3], one invalid permutation is [5, 2, 3, 4, 1] because 5 is placed at index 1, that is not prime.To solve this, we will follow these steps −Define one method called getNum, as follows −prime := list of all primes from ... Read More

Calculate Day of the Year in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:54:48

3K+ Views

Suppose, we have a date in the format “YYYY-MM-DD”. We have to return the day number of the year. So if the date is “2019-02-10”, then this is 41st day of the year.To solve this, we will follow these steps −Suppose D is an array of day count like [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]Convert the date into list of year, month and dayif the year is leap year then set date D[2] = 29Add up the day count up to the month mm – 1. and day count after that.ExampleLet us see ... Read More

Number of Equivalent Domino Pairs in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:45

302 Views

Suppose we have a list of dominos. Each domino has two numbers. Two dominos D[i] = [a, b] and D[j] = [c, d] will be same if a = c and b = d, or a = d and b = c. So one domino can be reversed. We have to return number of pairs (i, j) for which 0

Top K Frequent Words in C++

Arnab Chakraborty
Updated on 29-Apr-2020 07:52:40

581 Views

Suppose we have a non-empty list of words; we have to find the k most frequent elements. our answer should be sorted by frequency from highest to lowest. When two words have the same frequency, then the word with the lower alphabetical order will be placed at first. So if the array is like [‘the’, ‘sky’, ‘is’, ‘blue’, ‘the’, ‘weather’, ‘is’, ‘comfortable’], so most frequent words are ["is", "the", "blue"]To solve this, we will follow these steps −Define one map called mcreate one priority queue vfor i := 0 to n, where n is the size of the word array, ... Read More

Defanging an IP Address in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:47:43

956 Views

Suppose we have a valid IPv4 IP address. We have to return the Defanged version of the IP address. A Defanged IP address is basically replace every period “.” by “[.]” So if the IP address is “192.168.4.1”, the output will be “192[.]168[.]4[.]1”To solve this, we will follow these steps −We will split the string using dot, then put each element separated by “[.]”ExampleLet us see the following implementation to get better understanding − Live Democlass Solution(object):    def defangIPaddr(self, address):       address = address.split(".")       return "[.]".join(address) ob1 = Solution() print(ob1.defangIPaddr("192.168.4.1"))Input"192.168.4.1"Output"192[.]168[.]4[.]1"

Distribute Candies to People in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:45:56

1K+ Views

Suppose we want to distribute some number of candies to a row of n people in the following way −We then give 1 candy to the first people, 2 candies to the second people, and so on until we give n candies to the last people.After that, we go back to the start of the row again, give n + 1 candies to the first people, n + 2 candies to the second people, and so on until we give 2 * n candies to the last people.We will repeat this process until we run out of candies. The last ... Read More

Duplicate Zeros in Python

Arnab Chakraborty
Updated on 29-Apr-2020 07:43:45

1K+ Views

Suppose we have a fixed length array of integers, we have to duplicate each occurrence of zero, shifting the remaining elements to the right side.Note that elements beyond the length of the original array are not written.So suppose the array is like [1, 0, 2, 3, 0, 4, 5, 0], then after modification it will be [1, 0, 0, 2, 3, 0, 0, 4]To solve this, we will follow these steps −copy arr into another array arr2, set i and j as 0while i < size of arr −if arr2[i] is zero, thenarr[i] := 0increase i by 1if i < ... Read More

Number of Longest Increasing Subsequence in C++

Arnab Chakraborty
Updated on 29-Apr-2020 06:38:00

413 Views

Suppose we have one unsorted array of integers. we have to find the number of longest increasing subsequence, so if the input is like [1, 3, 5, 4, 7], then the output will be 2, as increasing subsequence are [1, 3, 5, 7] and [1, 3, 4, 7]To solve this, we will follow these steps −n := size of the num array, create two arrays len and cnt of size n, and fill them with value 1.lis := 1for i in range 1 to nfor j in range 0 to i – 1if nums[i] > nums[j], thenif len[j] + 1 ... Read More

Maximum Swap in Python

Arnab Chakraborty
Updated on 29-Apr-2020 06:32:59

1K+ Views

Suppose we have a non-negative integer; we could swap two digits at most once to get the maximum valued number. We have to return the maximum valued number we can get. So if the input is like 2736 then the output will be 7236. So here we are swapping 2 and 7.To solve this, we will follow these steps −num := cut each digit from the number, and make a listnum1 := sort the num in reverse orderindex := 0while index < length of numif num1[index] is not same as num[index]a := subarray of num from index (index + 1) ... Read More

Advertisements