Found 26504 Articles for Server Side Programming

Program to check if array pairs are divisible by k or not using Python

Arnab Chakraborty
Updated on 29-May-2021 13:57:57

313 Views

Suppose we have an array called nums, this array contains even number of elements, and have another value k. We have to split nums into exactly n/2 pairs such that the sum of each pair is divisible by k. If we can do so then return true, otherwise false.So, if the input is like nums = [9, 5, 3, 4, 7, 10, 20, 8] k = 3, then the output will be True because we can make pairs like (9, 3), (5, 7), (4, 20), (8, 10), sum of all pairs are divisible by 3.To solve this, we will follow ... Read More

Program to find longest subarray of 1s after deleting one element using Python

Arnab Chakraborty
Updated on 29-May-2021 13:58:34

421 Views

Suppose we have a binary array called nums, we can delete one element from it. We have to find the size of the longest non-empty subarray which is containing only 1's in the resulting array. If there is no such subarray, then return 0.So, if the input is like nums = [1, 0, 1, 1, 1, 0, 1, 1, 0], then the output will be 5 because by removing 0 from position 5, we can get a subarray [1, 1, 1, 1, 1] there are five 1s.To solve this, we will follow these steps −if 0 is not in nums, ... Read More

Program to find the kth factor of n using Python

Arnab Chakraborty
Updated on 29-May-2021 14:04:18

617 Views

Suppose we have two positive values n and k. Now consider we have a list of all factors of n sorted in ascending order, we have to find the kth factor in this list. If there are less than k factors, then return -1.So, if the input is like n = 28 k = 4, then the output will be 7 because, the factors of 28 are [1, 2, 4, 7, 14, 28], fourth one is 7.To solve this, we will follow these steps −if k is same as 1, thenreturn 1cand := a list with one element [1]for i ... Read More

Program to make file names unique using Python

Arnab Chakraborty
Updated on 29-May-2021 14:04:55

1K+ Views

Suppose we have an array of n strings called names. We have to make n directories in the file system such that, at the ith minute, we will create a directory with the name names[i]. Two files cannot have the same name, if we enter a duplicate directory name the system will have a suffix addition to its name in the form of (k), here, k is the smallest positive integer such that the obtained name remains unique. We have to find an array of strings of length n where ans[i] is the actual name that will be assign to ... Read More

Program to find minimum number of days to make m bouquets using Python

Arnab Chakraborty
Updated on 29-May-2021 14:05:42

690 Views

Suppose we have an array with integers called nums, we also have another two values m and k. Now, we need to make m bouquets. To make one bouquet we need k adjacent flowers from the garden. Here the garden consists of n different flowers, the ith flower will bloom in the bloomDay[i]. Each flower can be used inside only one bouquets. We have to find the minimum number of days need to wait to make m bouquets from the garden. If we cannot make m bouquets, then return -1.So, if the input is like bloomDay = [5, 5, 5, ... Read More

Program to find least number of unique integers after K removals using Python

Arnab Chakraborty
Updated on 29-May-2021 14:06:25

801 Views

Suppose we have an array called nums where only integers are stored. If we have a number k. We have to find least number of unique elements after removing exactly k elements.So, if the input is like nums = [5, 4, 2, 2, 4, 4, 3], k = 3, then the output will be 2, because if we remove 5 and 3, and either any one of 2s or any one of 4s, then there only 2 and 4 will be left.To solve this, we will follow these steps −dictionary:= a new mapfor each num in nums, doif num is ... Read More

Program to find two non-overlapping sub-arrays each with target sum using Python

Arnab Chakraborty
Updated on 29-May-2021 14:07:06

216 Views

Suppose we have an array of arr and another value target. We have to find two non-overlapping sub-arrays of arr where each has sum equal to target. If there are multiple answers, then we have to find an answer where the sum of the lengths of the two sub-arrays is smallest. We have to find the minimum sum of the lengths of the two required sub-arrays, if there is no such subarray then return -1.So, if the input is like arr = [5, 2, 6, 3, 2, 5] target = 5, then the output will be 2 there are three ... Read More

Program to find maximum population year using Python

Arnab Chakraborty
Updated on 29-May-2021 14:07:56

602 Views

Suppose we have a table with two columns (birth, death) where each row is representing the birth and death years of the ith person. The population of some year y is the number of people alive during y. The ith person is counted in year y's population when y is in the inclusive range [birth_i, death_i - 1]. (The person is not counted in the year that they die). So, we have to find the earliest year with the maximum population.So, if the input is likeBirthDeath197020101960202019401970then the output will be 2 because there is only one value that matches with ... Read More

Program to find minimum distance to the target element using Python

Arnab Chakraborty
Updated on 29-May-2021 14:10:43

1K+ Views

Suppose we have an array nums and two different values target (target must present in nums) and start, we have to find an index i such that nums[i] = target and |i - start| is minimum. We have to return the |i - start|.So, if the input is like nums = [3, 4, 5, 6, 7] target = 7 start = 2, then the output will be 2 because there is only one value that matches with target, that is nums[4], so i = 4. Now |4-2| = 2.To solve this, we will follow these steps:minimum := infinityfor i in ... Read More

Program to replace all digits with characters using Python

Arnab Chakraborty
Updated on 29-May-2021 14:11:15

3K+ Views

Suppose we have an alphanumeric string s that contains lowercase English letters in its even positions and digits in its odd positions. Consider an operation shift(c, x), where c is any character and x is a number (digit), this will find the xth character after c. So, for example, shift('p', 5) = 'u' and shift('a', 0) = 'a'. Now for every odd index i, we want to replace the digit s[i] with shift(s[i-1], s[i]). We have to find s after replacing all digits.So, if the input is like s = "a2b1d4f3h2", then the output will be "acbcdhfihj" becauseshift('a', 2) = ... Read More

Advertisements