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
-
Economics & Finance
Articles by Arnab Chakraborty
Page 30 of 377
Program to find array of length k from given array whose unfairness is minimum in python
Suppose we have an array A and another value k. We have to form an array of size k by taking elements from A and minimize the unfairness. Here the unfairness is calculated by this formula − (𝑚𝑎𝑥𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟) − (𝑚𝑖𝑛𝑖𝑚𝑢𝑚 𝑜𝑓 𝑎𝑟𝑟) So, if the input is like A = [25, 120, 350, 150, 2500, 25, 35] and k = 3, then the output will be 10, because we can take elements [25, 25, 35] so max(arr) = 35 and min(arr) = 25. So their difference is 10. Approach The key insight is that ...
Read MoreProgram to find total duration of K most watched shows in Python
Suppose we have a list of strings called shows, a list of integers called durations, and a value k. Here shows[i] and durations[i] represent a show and its duration watched by the ith person. We need to find the total duration watched of the k most watched shows. So, if the input is like shows = ["The BGT", "Jack jumper", "The BGT", "Jokers Company", "Music magic"] durations = [10, 8, 10, 18, 9] k = 2, then the output will be 38, as the top 2 most watched shows are "Jokers Company" and "The BGT" with total durations of ...
Read MoreProgram to count number of intervals which are intersecting at given point in Python
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
Read MoreProgram to find expected value of given equation for random numbers in Python
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 MoreProgram to insert new element into a linked list before the given position in Python
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 MoreProgram to find size of common special substrings of two given strings in Python
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 MoreProgram to find index whose left and right elements sums are equal in Python
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 MoreProgram to check n can be represented as sum of k primes or not in Python
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 MoreProgram to find all substrings whose anagrams are present in a string in Python
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 MoreProgram to find smallest index for which array element is also same as index in Python
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