Suppose we have a list of intervals and a value called point. Each interval interval[i] contains [si, ei] representing the start time and end time of interval i (both inclusive). We have to find the number of intervals that intersect at the given point. So, if the input is like intervals = [[2, 6],[4, 10],[5, 9],[11, 14]] and point = 5, then the output will be 3, because at time 5, there are 3 intervals: [2, 6], [4, 10], and [5, 9]. Algorithm To solve this, we will follow these steps − Initialize count = 0 For each interval with start time i and end time j in intervals, do If point >= i and point = i and point
Suppose we have a number n. Consider x = rand() mod n, where rand() function generates integers between 0 and 10^100 (both inclusive) uniformly at random. And $$Y = \sqrt{x+\sqrt{x+\sqrt{x+\sqrt{x+...}}}}$$ We have to find the expected value of Y. The value of n will be in range 1 and 5*10^6. So, if the input is like n = 5, then the output will be 1.696 Mathematical Background For a nested radical expression $Y = \sqrt{x+\sqrt{x+\sqrt{x+\sqrt{x+...}}}}$, we can solve this algebraically. If we let $Y = \sqrt{x + Y}$, then squaring both sides gives us $Y^2 ... Read More
Inserting a new element into a linked list at a specific position is a common operation in data structures. This involves creating a new node and adjusting the links between existing nodes to maintain the list structure. 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]. Algorithm To solve this, we will follow these steps − new := create a linked list node with value same as val if pos is same as 0, ... Read More
Suppose we have two strings s1 and s2. We have to find the size of longest string s3 which is a special substring of both s1 and s2. We can say a string x is a special substring of another string y if x can be generated by removing 0 or more characters from y. This is also known as the Longest Common Subsequence (LCS) problem. So, if the input is like s1 = 'pineapple' and s2 = 'people', then the output will be 5 as the special substring is 'peple', of size 5. Algorithm To ... Read More
Suppose we have a list of numbers 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 ... Read More
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. Algorithm To solve this, we will follow these steps − if n < k*2, then return False if k > 2, then return True if k is same as ... Read More
In this problem, we need to find all substrings in a string where there exists another substring at a different location that is an anagram of the first substring. An anagram contains the same characters but in different order. For example, if the input is s = "abcba", we need to find substrings like "a" (appears at positions 0 and 4), "ab" and "ba" (anagrams of each other), etc. Algorithm To solve this problem, we follow these steps − Generate all possible substrings of each length Group substrings by their sorted character representation (anagram signature) ... Read More
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. Algorithm To solve this, we will follow these steps − ret := ... Read More
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 35 is the first room that can accommodate the target size of 30. Algorithm To solve this, we will follow these steps − for each ... Read More
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 numbers have occurred twice. Algorithm To solve this, we will follow these steps ? If size of nums is odd, then return False Sort the list nums For i in range 1 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance