Server Side Programming Articles - Page 1434 of 2650

Program to find length of longest alternating path of a binary tree in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:30:08

289 Views

Suppose we have a binary tree, we have to find the longest path that alternates between left and right child and going down.So, if the input is likethen the output will be 5 as the alternating path is [2, 4, 5, 7, 8].To solve this, we will follow these steps:if root is null, thenreturn 0Define a function dfs() . This will take node, count, flagif node is not null, thenif flag is same as True, thena := dfs(left of node, count + 1, False)b := dfs(right of node, 1, True)otherwise when flag is same as False, thena := dfs(right of ... Read More

Program to find length of longest word that can be formed from given letters in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:27:28

348 Views

Suppose we have a list of words and a string called letters, we have to find the size of the longest word that can be made by rearranging the given letters. In the letters there may be asterisk character (*) it can match any character. And it is not necessary to use all the letters.So, if the input is like words = ["prince", "rice", "price", "limit", "hello"] letters = "*r**ce*", then the output will be 6, as the longest word we can make is "prince" the length is 6.To solve this, we will follow these steps:has := a map containing ... Read More

Program to count number of word concatenations are there in the list in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:25:59

200 Views

Suppose we have a list of strings; we have to find the number of words that are concatenations of other words also in the list. We can reuse words when concatenating and concatenate any number of times.So, if the input is like words = ["hello", "world", "helloworld", "famous", "worldfamous", "programming"], then the output will be 2, as "helloworld" is concatenation of "hello" and "world". "worldfamous" is concatenation of "world" and "famous".To solve this, we will follow these steps:trie := a new mapfor each word in words, dolayer := triefor each w in word, doif w is not in layer, thenlayer[w] ... Read More

Program to find nearest time by reusing same digits of given time in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:22:21

363 Views

Suppose we have a 24-hour string in "hh:mm" format, we have to find the next closest time that can be formed by reusing given digits. We can reuse digits from the given string as many times as we want.So, if the input is like s = "03:15", then the output will be 03:30, as the nearest time 03:30 that repeats the given digits.To solve this, we will follow these steps:use := a list with two digit hour and two digit mins valuespossible := a new setDefine a function backtrack() . This will take pathif size of path is same as ... Read More

Program to find minimum number of days to wait to make profit in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:18:33

274 Views

Suppose we have a list of prices representing the daily stock market prices of a company in chronological sequence. We have to find a same length list where the value at index i will be the minimum number of days we would have to wait until we make a profit. If there is no such way to make a profit the value should be 0.So, if the input is like prices = [4, 3, 5, 9, 7, 6], then the output will be [2, 1, 1, 0, 0, 0]To solve this, we will follow these steps:ans := a list of ... Read More

Program to find all possible strings typed using phone keypad in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:15:07

1K+ Views

Suppose we have a string containing digits from 2-9. We have to find all possible letter combinations that the number could generate. One mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does map some characters but no letters.12a b c3d e f4g h i5j k l6m n o7p q r s8t u v9w x y z*0#For an example, if the given string is “49”, then the possible strings will be ['gw', 'gx', 'gy', 'gz', 'hw', 'hx', 'hy', 'hz', 'iw', 'ix', 'iy', 'iz']To solve this, we will follow these steps:Define an array ... Read More

Program to check whether a board is valid N queens solution or not in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:12:22

898 Views

Suppose we have a n x n matrix represents a chess board. There are some 1s and 0s, where 1 represents a queen and 0 represents an empty cell. We have to check whether the board is valid solution to the N-Queen puzzle or not. As we know a board is a solution of valid N-queen solution where no two queens are attacking each other.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps:n := row count of matrixrows := a new set, cols := a new set, diags := a new ... Read More

Program to check whether we can unlock all rooms or not in python

Arnab Chakraborty
Updated on 26-Nov-2020 08:08:16

299 Views

Suppose we have a list of lists called rooms. Each index i in rooms represents a room and rooms[i] represents different keys to open other rooms. The room 0 is open and we are at that room and every other room is locked. We can move freely between opened rooms; we have to check whether we can open every room or not.So, if the input is like rooms = [[2, 0], [3], [1], []], then the output will be True, as we start from room 0 and can go to room 2 with its key 2. From room 2 we ... Read More

Program to find nth ugly number in C++

Arnab Chakraborty
Updated on 26-Nov-2020 08:06:06

342 Views

Suppose we have a number n; we have to find the nth ugly number. As we know that the ugly numbers are those numbers, whose prime factors are only 2, 3 and 5. So if we want to find 10th ugly number, the output will be 12, as the first few ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 and so on.To solve this, we will follow these steps:Define an array v of size(n + 1)if n is same as 1, then:return 1two := 2, three := 3, five := 5twoIdx := 2, threeIdx := 2, fiveIdx := 2for initialize i := 2, when i

Program to find median of two sorted lists in C++

Arnab Chakraborty
Updated on 26-Nov-2020 07:56:38

223 Views

Suppose we have two sorted lists. We have to find the median of these two lists. So if the arrays are like [1,5,8] and [2,3,6,9], then the answer will be 5.To solve this, we will follow these steps:Define a function solve(), this will take an array nums1, an array nums2,if size of nums1 > size of nums2, then:return solve(nums2, nums1)x := size of nums1, y := size of nums2low := 0, high := xtotalLength := x + ywhile low

Advertisements