Server Side Programming Articles - Page 1556 of 2650

Largest product of contiguous digits in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:34:26

345 Views

Suppose we have two numbers num and k, we have to find the largest product of k contiguous digits in num. We have to keep in mind that num is guaranteed to have >= k digits.So, if the input is like num = 52689762 and k = 4, then the output will be 3024, largest product of 4 consecutive digits is (8*9*7*6) = 3024.To solve this, we will follow these steps −largest := 0cand := 1while (quotient of num/10)^(k-1) > 0, dodigits := (last digit of nums)^kcand := 1while digits > 0, docand := cand * (digits mod 10)if cand ... Read More

Largest Number By Two Times in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:32:53

295 Views

Suppose we have a list of numbers; we have to check whether the largest number is bigger than the second-largest number by more than two times. As an example, if the list is like [3, 9, 6], then it will return false, as 9 is not bigger than 12 (2 times 6). When the list is [6, 3, 15], it will return true, as 15 is bigger than 12 (2 times 6).To solve this, we will follow these steps −if size of nums < 2, thenreturn Falsep_max := minimum of nums[0] and nums[1]c_max := maximum of nums[0] and nums[1]for i ... Read More

Largest Gap in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:31:10

1K+ Views

Suppose we have a list of numbers called nums, we have to find the largest difference of two consecutive numbers in the sorted version of nums.So, if the input is like [5, 2, 3, 9, 10, 11], then the output will be 4, as the largest gap between 5 and 9 is 4.To solve this, we will follow these steps −n := sorted list numsans := a new listfor i in range 0 to size of n -2, doinsert n[i+1]-n[i] at the end of ansreturn maximum of ansLet us see the following implementation to get better understanding −Example Live Democlass Solution: ... Read More

K Prefix in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:29:49

448 Views

Suppose we have a list of numbers nums and an integer k, we have to find the maximum possible i where nums[0] + nums[1] + ... + nums[i] ≤ k. We will return -1 if no valid i exists.So, if the input is like nums = [4, -7, 5, 2, 6], k = 5, then the output will be 3, the index is 3 as if we add 4+(-7)+5+2 = 4, this is less than k, if we add last element it will no longer less than k, so the index is 3.To solve this, we will follow these steps ... Read More

Knights' Attack in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:28:18

317 Views

Suppose we have a two-dimensional binary matrix, representing a rectangular chess board, here 0 is for empty cell and 1 is for a knight. The knight is able to move two squares away horizontally and one square vertically, or two squares vertically and one square horizontally (like chess board knight). We have to check whether any two knights are attacking each other or not.So, if the input is like000000100000010Then the output will be TrueTo solve this, we will follow these steps −row, col := row count of matrix, column count of matrixfor r in range 0 to row-1, dofor c ... Read More

K and -K in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:26:33

779 Views

Suppose we have a list of numbers called nums, we have to find the largest number k where k and -k both exist in nums (they can be the same number). If there's no such element, return -1.So, if the input is like [-5, 2, 9, -6, 5, -9], then the output will be 9.To solve this, we will follow these steps −L1:= a list of 0 and positive elements in numsL2:= a list of 0 and negative elements in numssort L1 in reverse ordersort the list L2for each i in L1, dofor each j in L2, doif i+j is ... Read More

Remove One to Make Average k in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:25:01

169 Views

Suppose we have a list of numbers called nums and an integer k, we have to check whether we can remove exactly one element from the list to make the average equal to exactly k. Now we have to keep in mind that, there are some constraints −2 ≤ n ≤ 1, 000 where n is number of elements of nums listnums[i], k ≤ 1, 000, 000So, if the input is like [5, 3, 2, 4, 6, 10], k = 4, then the output will be True as if we remove 10, then the average of elements will be (5+3+2+4+6)/5 ... Read More

Inverse Factorial in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:23:39

2K+ Views

Suppose we have a number a, we have to find n, such that factorial of n (n!) is same as a. As we know, the factorial n = n * (n - 1) * (n - 2) * ... * 1. If there is no such integer n then return -1.So, if the input is like a = 120, then the output will be 5.To solve this, we will follow these steps −i := 0, num := 1L:= a new listwhile i < a, doi := factorial of numinsert i at the end of Lnum := num + 1if a ... Read More

Find Intersecting Intervals in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:21:36

3K+ Views

Suppose we have a list of intervals, where each interval is like [start, end] this is representing start and end times of an intervals (inclusive), We have to find their intersection, i.e. the interval that lies within all of the given intervals.So, if the input is like [[10, 110], [20, 60], [25, 75]], then the output will be [25, 60]To solve this, we will follow these steps −start, end := interval after deleting last element from intervals listwhile intervals is not empty, dostart_temp, end_temp := interval after deleting last element from intervals liststart := maximum of start, start_tempend := minimum ... Read More

String Interleaving in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:20:24

1K+ Views

Suppose we have two strings s and t, we have to find two strings interleaved, starting with first string s. If there are leftover characters in a string they will be added to the end.So, if the input is like s = "abcd", t = "pqrstu", then the output will be "apbqcrdstu"To solve this, we will follow these steps −res:= blank stringi:= 0m:= minimum of size of s, size of twhile i < m, dores := res concatenate s[i] concatenate t[i]i := i + 1return res concatenate s[from index i to end] concatenate t [from index i to end]Example Live Democlass ... Read More

Advertisements