Count Number of Elements in a Set with Recursive Indexing in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:29:51

243 Views

Suppose we have a list of numbers called A and another number k, we have to make a new set of possible elements {A[k], A[A[k]], A[A[A[k]]], ... } stopping before it's out of index. We have to find the size of this set, otherwise -1 when there is a cycle.So, if the input is like A = [1, 2, 3, 4, 5, 6, 7], k = 1, then the output will be 6 as A[1] = 2, A[2] = 3, A[3] = 4, A[4] = 5, A[5] = 6, A[6] = 7, So the set is {2, 3, 4, 5, ... Read More

Find Index of First Recurring Character in a String in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:28:37

368 Views

Suppose we have a string s, we have to find the index of the first recurring character in it. If we cannot find no recurring characters, then return -1.So, if the input is like "abcade", then the output will be 3, as 'a' is again present at index 3.To solve this, we will follow these steps −define a map charsfor i in range 0 to size of s, doif s[i] in chars, thenreturn iotherwise, chars[s[i]] := chars[s[i]] + 1return -1Let us see the following implementation to get better understanding −Example Live Demofrom collections import defaultdict class Solution:    def solve(self, s): ... Read More

Check Two Rectangles Overlap in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:26:20

4K+ Views

Suppose we have a rectangle that is represented as a list with four elements [x1, y1, x2, y2], where (x1, y1) is the coordinates of its bottom-left corner, and (x2, y2) is the coordinates of its top-right corner. Two rectangles overlap when the area of their intersection is positive. So, two rectangles that only touch at the corner or edges do not overlap.So, if the input is like R1 = [0,0,2,2], R2 = [1,1,3,3], then the output will be True.To solve this, we will follow these steps −if R1[0]>=R2[2] or R1[2]=R2[2]) or (R1[2]

Check Pythagorean Triplets in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:24:11

1K+ Views

Suppose we have a list of numbers called nums, we have to check whether there exist three numbers a, b, and c such that a^2 + b^2 = c^2.So, if the input is like [10, 2, 8, 5, 6], then the output will be True, as 8^2 + 6^2 = 64+36 = 100 = 10^2.To solve this, we will follow these steps −tmp := list of square of all numbers in nums in descending orderfor each index i and corresponding number n in tmp, dobase := nleft := i+1, right := size of tmp -1while left base, thenleft := ... Read More

Find All Prime Factors of a Given Number in Sorted Order in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:20:24

757 Views

Suppose we have a number n greater than 1, we have to find all of its prime factors and return them in sorted sequence. We can write out a number as a product of prime numbers, they are its prime factors. And the same prime factor may occur more than once.So, if the input is like 42, then the output will be [2, 3, 7].To solve this, we will follow these steps −res:= a new listwhile n mod 2 is same as 0, doinsert 2 at the end of resn := quotient of n/2for i in range 3 to (square ... Read More

Minimum Total Cost for Equalizing List Elements in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:15:20

274 Views

Suppose we have two lists of numbers called nums and costs. Now consider, there is an operation where we can increase or decrease nums[i] for cost costs[i]. We can perform any number of these operations, and we want to make all elements equal in the nums. We have to find the minimum total cost required.So, if the input is like nums = [3, 2, 4] costs = [1, 10, 2], then the output will be 5, as if we can decrease the number 3 into 2 for a cost of 1. Then we can decrement 4 two times for a ... Read More

Update Tree Values with Left and Right Subtree Sum in Python

Arnab Chakraborty
Updated on 07-Oct-2020 13:13:10

279 Views

Suppose we have a binary tree, we have to find the same tree but every node's value is replaced by its its value + all of the sums of its left and right subtrees.So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function tree_sum() . This will take root of a treeif root is null, thenreturn 0data of root := tree_sum(left of root) + tree_sum(right of root) + data of rootreturn data of rootFrom the main method, do the following:tree_sum(root)return rootLet us see the following implementation to get better understanding ... Read More

Find Minimum Edit Distance Between Two Strings in C++

Arnab Chakraborty
Updated on 07-Oct-2020 13:09:22

510 Views

Suppose we have two words S and T, we have to find the minimum number of operations needed to convert from S to T. The operations can be of three types, these areinsert a character, delete a characterreplace a character.So if the input strings are “evaluate” and “fluctuate”, then the result will be 5.To solve this, we will follow these steps −n := size of s, m := size of t, create an array dp of size n + 1for i in range 0 to ndp[i] := new array of size m + 1for j in range 0 to m:dp[i, ... Read More

Count Unique Palindromes Using String Characters in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:49:28

282 Views

Suppose we have a string s, we have to find the number of distinct palindromes we can generate using all characters. If the answer is very large then mod the result by 10^9 + 7.So, if the input is like s = "xyzzy", then the output will be 2, as we can make "zyxyz" and "yzxzy"To solve this, we will follow these steps −m = 10^9+7char_freq := a map holding each character of s and their frequenciesodd := 0for each character k and frequency v in char_freq, doif v mod 2 is 1, thenodd := odd + 1if odd > ... Read More

Calculate Vertex-to-Vertex Reachability Matrix in Python

Arnab Chakraborty
Updated on 07-Oct-2020 12:46:39

349 Views

Suppose we have a graph as an adjacency list representation, we have to find 2D matrix M whereM[i, j] = 1 when there is a path between vertices i and j.M[i, j] = 0 otherwise.So, if the input is likethen the output will be1111101111011110111101111To solve this, we will follow these steps −ans:= a 2d matrix of size n x n, where n is the number of vertices, fill with 0sfor i in range 0 to n, doq:= a queue, and insert i at firstwhile q is not empty, donode:= first element of q, and delete first element from qif ans[i, ... Read More

Advertisements