Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 74 of 377

Program to find size of common special substrings of two given strings in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 192 Views

Suppose we have two strings s1 and s2. We have to find the size of longest string s3 which is special substring of both s1 and s2.We can say a string x is special substring of another string y if x can be generated by removing 0 or more characters from y.So, if the input is like s1 = 'pineapple' s2 = 'people', then the output will be 5 as the special substring is 'peple', of size 5.To solve this, we will follow these steps −prev := a new dictionary, where if some key is not present, return 0for i ...

Read More

Program to find index whose left and right elements sums are equal in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 730 Views

Suppose we have a list of items called nums, we have to find the smallest index i such that the sum of the numbers which are present at the left of i is equal to the sum of numbers present at right of i. If we cannot find any such solution, return -1.So, if the input is like nums = [8, 2, 3, 6, 5, 2, 5, 9, 1, 2], then the output will be 4, because sum of elements that are left of index 4 is [8, 2, 3, 6] = 19, and sum of elements that are present ...

Read More

Program to check n can be represented as sum of k primes or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 742 Views

Suppose we have two inputs n and k. We have to check whether n can be represented as a sum of k number of prime values or not.So, if the input is like n = 30 k = 3, then the output will be True because 30 can be represented like 2 + 11 + 17.To solve this, we will follow these steps −if n < k*2, then return Falseif k > 2, then return Trueif k is same as 2, thenif n is even, then return Trueif (n-2) is prime, then return Truereturn Falseif n is prime, then return ...

Read More

Program to find all substrings whose anagrams are present in a string in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 537 Views

Suppose we have a string s with lowercase letters. We have to find all in s where there must be another substring in s at a different location that is an anagram of that taken substrings. We have to find a list of substrings in in lexicographic order.So, if the input is like s = "abcba", then the output will be ['a', 'a', 'ab', 'abc', 'abcb', 'b', 'b', 'ba', 'bc', 'bcba', 'cb', 'cba'] for each of them we can find different anagrams present in the string itself.To solve this, we will follow these steps −res := a new listL := ...

Read More

Program to find smallest index for which array element is also same as index in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 192 Views

Suppose we have a list of elements called nums where all items are unique, and they are sorted in ascending order, we have to find the minimum i such that nums[i] = i. If we cannot find any solution, then return -1. We have to solve this problem in O(log(n)) time.So, if the input is like nums = [-4, -1, 2, 3, 8], then the output will be 2, because both nums[2] = 2 and nums[3] = 3 but 2 is smaller.To solve this, we will follow these steps −ret := -1, lhs := 0, rhs := size of nums ...

Read More

Program to find first fit room from a list of rooms in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 272 Views

Suppose we have a list of numbers called rooms and another target value t. We have to find the first value in rooms whose value is at least t. If there is no such room, return -1.So, if the input is like rooms = [20, 15, 35, 55, 30] t = 30, then the output will be 35. Because 30 is smaller than 35 and previous rooms are not sufficient for target 30.To solve this, we will follow these steps −for each room in rooms, doif room >= t, thenreturn roomreturn -1ExampleLet us see the following implementation to get better ...

Read More

Program to find multiple of n with only two digits in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 281 Views

Suppose we have a number n. We have to find the least positive value x such that x is made up of only two digits 9's and 0's, and x is multiple of n.So, if the input is like n = 26, then the output will be 90090.To solve this, we will follow these steps −m := 9x := 1while m is not divisible by n, dox := x + 1m := replace all 1s with 9s in the binary form of xreturn m as integerExampleLet us see the following implementation to get better understanding −def solve(n):    m = ...

Read More

Program to check whether elements frequencies are even or not in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 142 Views

Suppose we have a list of elements called nums, we have to check whether all numbers appear even times or not. We have to solve it using constant space.So, if the input is like nums = [8, 9, 9, 8, 5, 5], then the output will be True, because all number have occurred twice.To solve this, we will follow these steps −if size of nums is odd, thenreturn Falsesort the list numsfor i in range 1 to size of nums, doif nums[i] is same as nums[i - 1], thennums[i] := 0, nums[i - 1] := 0return true when sum of ...

Read More

Program to sort given set of Cartesian points based on polar angles in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 1K+ Views

Suppose we have a set of Cartesian points in a list called points. We have to sort them based on their polar angles. The polar angles vary in range 0 and 2*PI. If some points have same polar angles, then arrange them based on the distance of that point from the origin.So, if the input is like points = [(1, 1), (1, -2), (-2, 2), (5, 4), (4, 5), (2, 3), (-3, 4)], then the output will be [(5, 4), (1, 1), (4, 5), (2, 3), (-3, 4), (-2, 2), (1, -2)]To solve this, we will follow these steps −Define ...

Read More

Program to count elements whose next element also in the array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Oct-2021 257 Views

Suppose we have a list of numbers say nums, we have to find the number of elements x in the array, such that x + 1 also exists in the array.So, if the input is like nums = [4, 2, 3, 3, 7, 9], then the output will be 3, because 2+1 = 3 is present, 3+1 = 4 is present and another 3 is present so total 3.To solve this, we will follow these steps −answer := 0c := a list containing frequencies of each elements present in numsdlist := a list from list of all keys of cfor ...

Read More
Showing 731–740 of 3,768 articles
« Prev 1 72 73 74 75 76 377 Next »
Advertisements