Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Arnab Chakraborty
Page 122 of 377
Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python
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 MoreFind Indexs of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array - Set-2 in Python
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 MoreFind k-th character of decrypted string - Set – 2 in Python
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 MoreFind largest subtree having identical left and right subtrees in Python
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 MoreFind longest palindrome formed by removing or shuffling chars from string in Python
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
Read MoreFind lost element from a duplicated array in Python
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 MoreFind maximum difference between nearest left and right smaller elements in Python
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 MoreFind maximum distance between any city and station in Python
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 MoreFind maximum length Snake sequence in Python
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 MoreFind maximum N such that the sum of square of first N natural numbers is not more than X in Python
Suppose we have a given integer X, we have to find the maximum value N so that the sum of first N natural numbers should not exceed the value X.So, if the input is like X = 7, then the output will be 2 as 2 is the maximum possible value of N, for N = 3, the sum of the series will exceed X = 7 So, 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14.To solve this, we will follow these steps −Define a function sum_of_squares() . This will take Nres :=(N *(N + ...
Read More