Found 33676 Articles for Programming

Count Numbers with N digits which consists of odd number of 0's in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:04:40

312 Views

We are given a number N as input. The goal is to find all N digit numbers that have an odd number of 0’s as digits. The number also could have preceding zeros like in case of N=3 numbers included will be 000, 011, 012….990.Let us understand with examples.Input − N=3Output − Count of no. with N digits which consists of even number of 0's are − 244Explanation − All 3 digit numbers would be like −Smallest will be 000, then 011, 012, 013, 0014…..Highest will be 990.Input − N=5Output − Count of no. with N digits which consists of ... Read More

Count Numbers with N digits which consists of even number of 0's in C++

Sunidhi Bansal
Updated on 01-Dec-2020 12:02:25

185 Views

We are given a number N as input. The goal is to find all N digit numbers that have an even number of 0’s as digits. The number also could have preceding zeros like in case of N=3 numbers included will be 001, 002, 003….010….so on.Let us understand with examples.Input − N=4Output − Count of no. with N digits which consists of even number of 0's are − 7047Explanation − All 4 digits numbers would be like −Smallest will be 0000, then 0011, 0012, 0013, 0014…..Highest will be 9900.Input − N=5Output − Count of no. with N digits which consists ... Read More

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

692 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

364 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

278 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

333 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

189 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

348 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

267 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

Advertisements