Find Sign of the Product of an Array Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:16:31

241 Views

Suppose we have an array called nums. We have to find sign of the multiplication result of all elements present in the array.So, if the input is like nums = [-2, 3, 6, -9, 2, -4], then the output will be Negative, as the multiplication result is -2592To solve this, we will follow these steps −zeroes := 0, negatives := 0for each i in nums, doif i is same as 0, thenzeroes := zeroes + 1if i < 0, thennegatives := negatives + 1if zeroes > 0 , thenreturn "Zero"otherwise when negatives mod 2 is same as 0, thenreturn "Positive"otherwise, ... Read More

Minimum Operations to Make Array Increasing Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:16:17

505 Views

Suppose we have an array nums. In one operation, we can select one element of the array and increase it by 1. For example, if we have [4, 5, 6], we can select element at index 1 to make the array [4, 5, 5]. Then we have to find the minimum number of operations required to make nums strictly increasing.So, if the input is like nums = [8, 5, 7], then the output will be 7, because we need to increase like [8, 6, 7], [8, 7, 7], [8, 8, 7], [8, 9, 7], [8, 9, 8], [8, 9, 9], ... Read More

Find Sum of Digits in Base K Using Python

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

732 Views

Suppose we have a number n in decimal number system (base 10) have another value k, we have to find the sum of the digits of n after converting given number n from base 10 to base k. When we calculate digit sum, we will consider each digit as decimal (base 10) number.So, if the input is like n = 985 k = 8, then the output will be 12 because the number 985 in octal is 1731, so the digit sum is 1+7+3+1 = 12.To solve this, we will follow these steps −ans := 0while n >= k, doans ... Read More

Find Largest Perimeter Triangle Using Python

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

570 Views

Suppose we have an array nums of positive lengths, we have to find the largest perimeter of a triangle, by taking three values from that array. When it is impossible to form any triangle of non-zero area, then return 0.So, if the input is like [8,3,6,4,2,5], then the output will be 19.To solve this, we will follow these steps −sort the list numsa := delete last element from numsb := delete last element from numsc := delete last element from numswhile b+c

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

Find Minimum Distance to 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

Find Maximum Population Year Using Python

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

619 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

Find Two Non-Overlapping Sub-Arrays with Target Sum Using Python

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

223 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

Find Least Number of Unique Integers After K Removals Using Python

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

811 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

Find Minimum Number of Days to Make M Bouquets Using Python

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

704 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

Advertisements