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
Articles by Arnab Chakraborty
Page 106 of 377
Find if it is possible to get a ratio from given ranges of costs and quantities in Python
Suppose we have a range of cost from lowCost to upCost and another range of quantity from lowQuant to upQuant, we have to check whether we can find the given ratio r where r=cost/quantity, and lowCost ⇐ cost ⇐ upCost and lowQuant ⇐ quantity ⇐ upQuant.So, if the input is like lowCost = 2, upCost = 10, lowQuant = 3, upQuant = 9 and r = 3, then the output will be True as the cost = r * quantity = 3 * 3 = 9 where cost is in range [1, 10] and quantity is in [2, 8]To solve ...
Read MoreFind if neat arrangement of cups and shelves can be made in Python
Suppose we have three different types of cups in array p and saucers in array q and m number of shelves, we have to check whether a neat arrangement of cups and shelves can be made.We can say that arrangement of the cups and saucers will be neat if it follows these conditions − 1. No shelf can hold both cups and saucers. 2. A self may contain at most 5 cups. 3. A self may contain at most 10 saucers.So, if the input is like p = [4, 3, 7] q = [5, 9, 10] m = 11, then ...
Read MoreFind if there is a path of more than k length from a source in Python
Suppose we have a graph, we also have a source vertex and a number k. The k is the path length of graph between source to destination, we have to check whether we can find a simple path (without cycle) starting from source and ending at any other vertex (as destination). The graph is shown in following −So, if the input is like Source = 0, k = 64, then the output will be True as there exists a simple path 0 to 7 to 1 to 2 to 8 to 6 to 5 to 3 to 4, this path ...
Read MoreFind 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 More