Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 280 of 377

Program to find length longest prefix sequence of a word array in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 271 Views

Suppose we have a list of words called w, with lowercase strings. We have to find the length of the longest sequence of w where each previous word is the prefix of the next word and the next word has just one new character appended.So, if the input is like w = ["pqr", "pq", "m", "mn", "pqrs"], then the output will be 3 because we can get the sequence: ["pq", "pqr", "pqrs"], whose length is 3.To solve this, we will follow these steps −sort the list wdp := a map, where default value for a key is 0res := 0for ...

Read More

Program to find length of longest consecutively increasing substring in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 276 Views

Suppose we have a lowercase string s. This contains English letters as well as "?" Symbol. For each "?" we must either remove it or replace it with any lowercase letter. We have to find the length of the longest consecutively increasing substring that starts with letter "a".So, if the input is like s = "vta???defke", then the output will be 6, as we can turn s into "vtabcdefke" and "abcdef" is the longest consecutively increasing substring, and this is also starting with "a".To solve this, we will follow these steps −maxlen := 0length := 0qmarks := 0for each c ...

Read More

Program to find longest consecutive run of 1s in binary form of n in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 771 Views

Suppose we have a non-negative value n, we have to find the length of the longest consecutive run of 1s in its binary representation.So, if the input is like n = 1469, then the output will be 4, because binary representation of 156 is "10110111101", so there are four consecutive 1sTo solve this, we will follow these steps −count := 0while n is not same as 0, don := n AND (n after shifting one bit to the left)count := count + 1return countExampleLet us see the following implementation to get better understanding −def solve(n):    count = 0    while n != 0:       n = n & (n

Read More

Program to find length of longest substring with 1s in a binary string after one 0-flip in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 701 Views

Suppose we have a binary string s. We are allowed to flip at most one "0" to "1", we have to find the length of the longest contiguous substring of 1s.So, if the input is like s = "1010110001", then the output will be 4, as if we flip the zero present at index 3, then we get the string "1011110001", here length of the longest substring of 1s is 4.To solve this, we will follow these steps −n := size of sans := 0, ones := 0, left := 0, right := 0while right < n, doif s[right] is ...

Read More

Program to find largest size to truncate logs to store them completely in database in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 154 Views

Suppose we have a list of numbers called logs and another value limit. Each element in logs[i] represents the size of logs generated by the i-th user. And limit represents the total size of logs we can store in our database. We have to find the largest x such that if we truncate every log in logs to be at most size x, and the sum of the left log sizes is at most limit. If no log needs to be truncated, then simply return the largest log size.So, if the input is like logs = [500, 200, 10000, 500, ...

Read More

Program to find minimum length of first split of an array with smaller elements than other list in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 220 Views

Suppose we have a list of numbers nums, we want to split the list into two parts part1 and part2 such that every element in part1 is less than or equal to every element in part1. We have to find the smallest length of part1 that is possible (not 0 length).So, if the input is like nums = [3, 1, 2, 5, 4], then the output will be 3, because we can split the list like part1 = [3, 1, 2] and part2 = [5, 4].To solve this, we will follow these steps −p := minimum of numss := 0for ...

Read More

Program to count number of on lights flipped by n people in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 241 Views

Suppose we have a number n, consider there are n toggle switches in a room and there are n people present in that room, they flip switches as follows −Person 1 comes and flips all switches.Person 2 comes and flips switches that are multiples of 2: 2, 4, 6, ...Person i comes and flips switches that are multiples of i. and so on.We have to find the number of switches that will be in on position finally.So, if the input is like n = 5, then the output will be 2, as initially bulbs are [0, 0, 0, 0, 0].After ...

Read More

Program to find lexicographically smallest lowercase string of length k and distance n in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 408 Views

Suppose we have two numbers n and k. We have to find the lexicographically smallest lowercase string of size k and distance n. The distance is the sum of the letter number in alphabet. For example, 'a' has letter number 1, 'b' has 2, 'y' has 25, 'z' has 26 as so on.So, if the input is like n = 15 k = 3, then the output will be "aam", because "aam" is the lexicographically smallest string of length 3 whose distance is 1 + 1 + 13 = 15.To solve this, we will follow these steps −dist := an ...

Read More

Program to find length of the longest path in an n-ary tree in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 488 Views

Suppose we have an edge list where each items are holding (u, v) represents u is parent of v. We have to find the length of the longest path in the tree. The path length is 1 + number of nodes in that path.So, if the input is likethen the output will be 5, because the path is [1, 4, 5, 7], there are 4 nodes in total, so path length is 1 + 4 = 5.To solve this, we will follow these steps −g := adjacency list of the graph from given edge listd := a new mapDefine a ...

Read More

Program to find area of largest submatrix by column rearrangements in Python

Arnab Chakraborty
Arnab Chakraborty
Updated on 19-Oct-2021 302 Views

Suppose we have a binary matrix. We can first rearrange the columns as many times as we want, then find return the area of the largest submatrix containing only 1s.So, if the input is like100111101then the output will be 4, because we can arrange is like −100111110To solve this, we will follow these steps −n := row count of matrixm := column count of matrixans := 0for i in range 1 to n - 1, dofor j in range 0 to m - 1, doif matrix[i, j] is 1, thenmatrix[i, j] := matrix[i, j] + matrix[i-1, j]for each row in ...

Read More
Showing 2791–2800 of 3,768 articles
« Prev 1 278 279 280 281 282 377 Next »
Advertisements