Found 33676 Articles for Programming

Find maximum length Snake sequence in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:54:38

660 Views

Suppose we have a grid of numbers; we have to find a snake sequence and return it. If there are multiple snake sequence, then return only one. As we know a snake sequence is made using adjacent numbers in the grid so for each number, the number on the right-hand side or the number below it is either +1 or -1 its value. So, if current value is in grid cell (a, b), we can either move right (a, b+1) if that number is ± 1 or move below (a+1, b) if that number is ± 1.So, if the input ... Read More

Find maximum distance between any city and station in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:50:44

252 Views

Suppose we have N number of cities, and they are numbered from 0 to N-1 and we also have the cities in which stations are located, we have to find the maximum distance between any city and its nearest station. We have to keep in mind that the cities with stations can be given in any order.So, if the input is like N = 6 and stations = [2, 4], then the output will be 2To solve this, we will follow these steps −station_present := a list of size N, and fill with Falsefor each city in station, dostation_present[city] := ... Read More

Find maximum difference between nearest left and right smaller elements in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:49:04

272 Views

Suppose we have an array of integers; we have to find the maximum absolute difference between the nearest left and the right smaller element of each of the elements in the array. If there is no smaller element on the right-hand side or left-hand side of any element then we will put zero as the smaller element.So, if the input is like A = [3, 5, 9, 8, 8, 10, 4], then the output will be 4 as left elements L = [0, 3, 5, 5, 5, 8, 3], right elements R = [0, 4, 8, 4, 4, 4, 0], ... Read More

Find lost element from a duplicated array in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:01:10

305 Views

Suppose we have two arrays which are duplicates of each other except one element, so, one element from one of the given arrays is missing, we have to find that missing element.So, if the input is like A = [2, 5, 6, 8, 10], B = [5, 6, 8, 10], then the output will be 2 as 2 is missing from second array.To solve this, we will follow these steps −Define a function solve() . This will take A, B, Nif N is same as 1, thenreturn A[0];if A[0] is not same as B[0], thenreturn A[0]low := 0, high := ... Read More

Find longest palindrome formed by removing or shuffling chars from string in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:58:29

302 Views

Suppose we have a string; we have to find the longest palindrome that can be generated by deleting or shuffling the characters from the string. And if there are more than one palindrome then return only one.So, if the input is like pqqprrs, then the output will be pqrsrqp.To solve this, we will follow these steps −count := array of size 256, filled with 0for i in range 0 to size of string, docount[ASCII of(string[i]) ] := count[ASCII of(string[i]) ] + 1begin := blank string, mid := blank string, end := blank stringcharacter := ASCII of('a')while character

Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:56:08

167 Views

Suppose we have two arrays; we have to find the longest possible bitonic sequence so that the increasing part should be from first array and should be a subsequence of first array. similarly decreasing part of must be from second array and a subsequence of the second one.So, if the input is like A = [2, 6, 3, 5, 4, 6], B = [9, 7, 5, 8, 4, 3], then the output will be [2, 3, 4, 6, 9, 7, 5, 4, 3]To solve this, we will follow these steps −Define a function index_ceiling() . This will take arr, T, ... Read More

Find largest subtree having identical left and right subtrees in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:52:45

115 Views

Suppose we have a binary tree; we have to find the largest subtree having identical left and right subtree. Preferred time complexity is O(n).So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function solve() . This will take root, encode, maxSize, maxNodeif root is None, thenreturn 0left_list := list with a blank stringright_list := list with a blank stringls := solve(root.left, left_list, maxSize, maxNode)rs := solve(root.right, right_list, maxSize, maxNode)size := ls + rs + 1if left_list[0] is same as right_list[0], thenif size > maxSize[0], thenmaxSize[0] := sizemaxNode[0] := rootencode[0] := ... Read More

Find k-th character of decrypted string - Set – 2 in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:49:33

474 Views

Suppose we have one encoded string where repetitions of substrings are represented as substring followed by count of substrings. As an example, if the string is "pq2rs2" and k=5, so output will be 'r', this is because the decrypted string is "pqpqrsrs" and 5th character is 'r'. We have to keep in mind that the frequency of encrypted substring can be of more than one digit.So, if the input is like string = "pq4r2ts3" and k = 11, then the output will be i, as the string is pqpqpqpqrrtststsTo solve this, we will follow these steps −encoded := blank stringoccurrence ... Read More

Find Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in Python

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

293 Views

Suppose we have one binary array. We have to find the position of 0 that can be replaced with 1 to get maximum number of continuous sequence of 1s.So, if the input is like [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1], then the output will be 10, so the array will be [1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1].To solve this, we will follow these steps −i := 0, n := size of Acount_left := 0, count_right := 0max_i := -1, last_i := -1count_max := 0while i < ... Read More

Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:43:26

191 Views

Suppose we have two strings S1 and S2 of same lengths, we have to find an index i such that S1[0…i] and S2[i+1…n-1] give a palindrome when they are concatenated together. When it is not possible, return -1.So, if the input is like S1 = "pqrsu", S2 = "wxyqp", then the output will be 1 as S1[0..1] = "pq", S2[2..n-1] = "ypq", then S1 + S2 = "pqyqp" indicates is a palindrome.To solve this, we will follow these steps −n := size of str1str := blank stringfor i in range 0 to n, dostr := str concatenate str1[i]temp := blank ... Read More

Advertisements