Found 26504 Articles for Server Side Programming

Count number of pairs (A <= N, B <= N) such that gcd (A , B) is B in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:01:08

694 Views

We are given an input N. The goal is to find all pairs of A, B such that 1

Program to convert a string to zigzag string of line count k in python

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

365 Views

Suppose we have a string s and another value k, We have to find a new string by taking each character from s and starting diagonally from top left to bottom right until reaching the kth line, then go up to top right, and so on.So, if the input is like s = "ilovepythonprogramming" k = 5, then the output will beTo solve this, we will follow these steps:line := a new mapcnt := 0delta := 1for each index i and character c in s, doinsert (c, i) at the end of line[cnt]cnt := cnt + deltaif cnt is same ... Read More

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

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

279 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

334 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

193 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

350 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

268 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

880 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

288 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

Advertisements