Found 10784 Articles for Python

K Prefix in Python

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

385 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

182 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

664 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

102 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

1K+ 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

Integer to Base 3 Number in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:19:05

2K+ Views

Suppose we have a number n, we have to find the base 3 equivalent of this number as string.So, if the input is like 17, then the output will be 122.To solve this, we will follow these steps −if n

In-place Move Zeros to End of List in Python

Arnab Chakraborty
Updated on 23-Sep-2020 07:16:15

3K+ Views

Suppose we have a list of numbers nums, we have to put all the zeros to the end of the list by updating the list in-place. And the relative ordering of other elements should not be changed. We have to try to solve this in O(1) additional space.So, if the input is like [2, 0, 1, 4, 0, 5, 6, 4, 0, 1, 7], then the output will be [2, 1, 4, 5, 6, 4, 1, 7, 0, 0, 0]To solve this, we will follow these steps −if size of L is same as 0, thenreturn a blank listk := ... Read More

Index into an Infinite String in Python

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

355 Views

Suppose we have a string s and two integers i and j (i < j). Now suppose p is an infinite string of s repeating forever. We have to find the substring of p from indexes [i, j).So, if the input is like s = "programmer", i = 4, j = 8, then the output will be "ramm".To solve this, we will follow these steps −p:= blank stringfor t in range i to j, dop := p concatenate a character from s at index (t mod size of s)return pLet us see the following implementation to get better understanding −Example Live ... Read More

Advertisements