Found 33676 Articles for Programming

Find alphabetical order such that words can be considered sorted in C++

Arnab Chakraborty
Updated on 28-Aug-2020 09:40:02

161 Views

Suppose we have an array of words, we have to find any alphabetical order in the English alphabet so that the given words can be considered sorted in ascending order, if there is any such order exists, otherwise return "impossible".So, if the input is like words = ["efgh", "wxyz"], then the output will be zyxvutsrqponmlkjihgfewdcbaTo solve this, we will follow these steps −ALPHABET := 26n := size of vif n is same as 1, then −display "abcdefghijklmnopqrstuvwxyz"returnDefine an array adj of size ALPHABETDefine an array in of size ALPHABET and fill with 0pre := v[0]for initialize i := 1, when ... Read More

Find all strings formed from characters mapped to digits of a number in Python

Arnab Chakraborty
Updated on 28-Aug-2020 09:32:19

260 Views

Suppose we have a character mapping as follows, here each digit, from 1 to 9, maps to few characters.1 -> ['A', 'B', 'C'] 2 -> ['D', 'E', 'F'] 3 -> ['G', 'H', 'I'] 4 -> ['J', 'K', 'L'] 5 -> ['M', 'N', 'O'] 6 -> ['P', 'Q', 'R'] 7 -> ['S', 'T', 'U'] 8 -> ['V', 'W', 'X'] 9 -> ['Y', 'Z']If we have a number, we have to change its digits with corresponding characters in given mapping list and show all generated strings. We should consider same character for every occurrence of a digit in the number. The given ... Read More

Find all rectangles filled with 0 in Python

Arnab Chakraborty
Updated on 28-Aug-2020 09:25:38

387 Views

Suppose we have a binary 2D matrix, now we have to find the beginning point and terminating point of all rectangles filled with 0s. We have to keep in mind that rectangles are separated and do not touch each other however they can touch the array boundary. A rectangle with only single element is also possible.So, if the input is like −10111011101111101100110110011011011101000011100011011101then the output will be [[0, 1, 0, 1], [0, 5, 0, 5], [1, 2, 1, 2], [2, 3, 2, 4], [3, 1, 5, 1], [3, 4, 6, 5], [5, 3, 6, 5], [7, 1, 7, 1], [7, 5, ... Read More

Find all reachable nodes from every node present in a given set in C++

Arnab Chakraborty
Updated on 28-Aug-2020 09:18:13

645 Views

Suppose we have one undirected graph and a set of vertices; we have to find all reachable nodes from every vertex present in the given set.So, if the input is likethen the output will be [1, 2, 3] and [4, 5] as these are two connected components.To solve this, we will follow these steps −nodes := number of nodes in the graphDefine an array visited of size: nodes+1. And fill with 0Define one map mcomp_sum := 0for initialize i := 0, when i < n, update (increase i by 1), do −u := arr[i]if visited[u] is false, then −(increase comp_sum ... Read More

Find all palindromic sub-strings of a given string - Set 2 in Python

Arnab Chakraborty
Updated on 28-Aug-2020 09:09:34

137 Views

Suppose we have a string; we have to find all the palindromic sub-strings from that string. Here aa and aa are considered as two sub-strings, not one.So, if the input is like redivider, then the output will be ['r', 'e', 'd', 'i', 'v', 'ivi', 'divid', 'edivide', 'redivider', 'i', 'd', 'e', 'r']To solve this, we will follow these steps −v := a new listpos := 0.0while pos < size of s, dorad := pos - (pos as integer)while (pos + rad) < size of s and (pos - rad) >= 0 and (s[integer of (pos - rad)] is same as s[integer ... Read More

Find all good indices in the given Array in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:59:25

555 Views

Suppose we have an array A of numbers, we have to find all indices of this array so that after deleting the ith element from the array, the array will be a good array. We have to keep in mind that −Good array is an array with an element that equals to the sum of all other elements.1-based indexing will be used here.So, if the input is like [10, 4, 6, 2], then the output will be [1, 4] as when we remove A[1], the array will look like [4, 6, 2] and it is good, as 6 = 4+2. ... Read More

Find all distinct palindromic sub-strings of a given String in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:55:03

414 Views

Suppose we have a string with lowercase ASCII characters, we have to find all distinct continuous palindromic substrings of it.So, if the input is like "bddaaa", then the output will be [a, aa, aaa, b, d, dd]To solve this, we will follow these steps −m := a new mapn := size of smatrix := make two rows of n number of 0ss := "@" concatenate s concatenate "#"for j in range 0 to 1, dotemp := 0matrix[j, 0] := 0i := 1while i

Find a string such that every character is lexicographically greater than its immediate next character in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:46:39

222 Views

Suppose we have a number n; we have to check a lower case stringof length n+1 so that the character at any position should be lexicographically bigger than its immediate next character.So, if the input is like 15, then the output will be ponmlkjihgfedcba.To solve this, we will follow these steps −temp_str := blank stringextra := n mod 26if extra >= 1, thenfor i in range 26 -(extra + 1) to 25, dotemp_str := temp_str + str[i]count := n / 26 (integer division)for i in range 1 to count + 1, dofor j in range 0 to 25, dotemp_str := ... Read More

Find a sorted subsequence of size 3 in linear time in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:38:31

202 Views

Suppose we have an array with N numbers, we have to check whether the 3 elements such that b[i]< b[j] < b[k] and i < j < k in linear (O(n)) time. If there are multiple such triplets, then print any one of them.So, if the input is like [13, 12, 11, 6, 7, 3, 31], then the output will be [6, 7, 31]To solve this, we will follow these steps −n := size of Amaximum := n-1, minimum := 0smaller := an array of size 1000, and fill with 0smaller[0] := -1for i in range 1 to n, doif ... Read More

Find a positive number M such that gcd(N^M,N&M) is maximum in Python

Arnab Chakraborty
Updated on 28-Aug-2020 08:32:45

154 Views

Suppose we have a number N, we have to find a positive number M such that gcd(N^M, N&M) is as large as possible and m < n. We will also return the largest gcd thus obtained.So, if the input is like 20, then the output will be 31To solve this, we will follow these steps −if bit_count(n) is same as 0, thenfor i in range 2 to int(square root of (n)) + 1, doif n mod i is same as 0, thenreturn int(n / i)otherwise, val := 0p :=dupn := nwhile n is non-zero, doif (n AND 1) is same ... Read More

Advertisements