Calculate Day of the Year in Python

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

4K+ 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

349 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

626 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

995 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

2K+ 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

448 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

Find K Closest Elements in C++

Arnab Chakraborty
Updated on 29-Apr-2020 06:29:19

496 Views

Suppose we have a sorted array, two integers k and x are also given, we have to find the k closest elements to x in that array. The result should be sorted in increasing order. If there is a tie, the smaller elements are always preferred. So if the input is like [1, 2, 3, 4, 5] and k = 4, x = 3, then output will be [1, 2, 3, 4]To solve this, we will follow these steps −Make an array called ansset low := 0, high := size of the array – kwhile low < highmid := low ... Read More

Palindromic Substrings in Python

Arnab Chakraborty
Updated on 29-Apr-2020 06:12:53

6K+ Views

Suppose we have a string; we have to count how many palindromic substrings present in this string. The substrings with different start indices or end indices are counted as different substrings even they consist of same characters. So if the input is like “aaa”, then the output will be 6 as there are six palindromic substrings like “a”, “a”, “a”, “aa”, “aa”, “aaa”To solve this, we will follow these steps −count := 0for i in range 0 to length if stringfor j in range i + 1 to length of string + 1temp := substring from index i to jif ... Read More

Advertisements