Count Intervals Intersecting at a Given Point in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:37:17

336 Views

Suppose we have a list of intervals and a value called point. Each interval interval[i] contains [si, ei] represents start time and end time of interval i (both inclusive). We have to find the number of intervals that are intersecting at given point.So, if the input is like intervals = [[2, 6],[4, 10],[5, 9],[11, 14]] point = 5, then the output will be 3, because at time 5, there are 3 intervals those are [3, 6], [4, 10], [5, 9]To solve this, we will follow these steps −count := 0for each start time i and end time j in intervals, doif point >= i and point = i and point

Insert New Element into Linked List Before Given Position in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:35:37

1K+ Views

Suppose we have a list of elements; these elements are stored in a singly linked list. We also have a value pos and value val. We have to insert val before index pos of the linked list.So, if the input is like nums = [1, 5, 3, 6, 8] pos = 3 val = 7, then the output will be [1, 5, 3, 7, 6, 8]To solve this, we will follow these steps −new := create a linked list node with value same as valif pos is same as 0, thennext of new := list_headreturn newtemp := list_headwhile temp is ... Read More

Find Size of Common Special Substrings in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:29:17

181 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

Find Index Where Left and Right Sums are Equal in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:28:14

715 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

Find Highest Common Factor of a List of Elements in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:26:33

4K+ Views

Suppose we have a list of elements called nums, we have to find the largest positive value that divides each of the integers.So, if the input is like nums = [15, 81, 78], then the output will be 3, as 3 is the largest integer that divides all 15, 81, and 78.To solve this, we will follow these steps −if size of nums is same as 1, thenreturn nums[0]div := gcd of nums[0] and nums[1])if size of nums is same as 2, thenreturn divfor i in range 1 to size of nums - 2, dodiv := gcd of div and ... Read More

Check if N Can Be Represented as Sum of K Primes in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:25:38

711 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

Find All Substrings Whose Anagrams Are Present in a String in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:22:40

520 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

Check if a Given Number is a Fibonacci Term in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:20:43

2K+ Views

Suppose we have a number say n. We have to check whether n is present in Fibonacci sequence or not. As we know in Fibonacci sequence f(i) = f(i-1) + f(i-2) for each i from 2 to n, and f(0) = 0, f(1) = 1.So, if the input is like n = 13, then the output will be True, as some terms in Fibonacci sequence are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, so 34 is present.To solve this, we will follow these steps −phi := 0.5 + 0.5 * square root of(5.0)a := phi * nreturn ... Read More

Find Smallest Index Where Array Element Equals Index in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:20:25

185 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

Find First Fit Room from a List of Rooms in Python

Arnab Chakraborty
Updated on 11-Oct-2021 07:18:40

260 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

Advertisements