Found 10476 Articles for Python

Program to find a pair (i, j) where nums[i] + nums[j] + (i -j) is maximized in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 09:10:15

294 Views

Suppose we have a list of numbers called nums, we have to find a pair (i, j) where i < j, and nums[i] + nums[j] + (i - j) is maximized.So, if the input is like nums = [6, 6, 2, 2, 2, 8], then the output will be 11, as if we pick the two 6 then its score is 6 + 6 + 0 - 1 = 11.To solve this, we will follow these steps:large := nums[0]maxi := 0for i in range 1 to size of nums, dolarge := large - 1maxi := maximum of large + nums[i] ... Read More

Program to find length of longest diminishing word chain in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 09:08:50

195 Views

Suppose we have a list of valid words, and have a string s also, we have to find the length of the longest chain of diminishing words that can be generated by starting at s and removing single letters and still make valid words.So, if the input is like words = ["lii", "limit", "limi", "li", "coffee", "jug", "pool", "type"] s = "limit", then the output will be 4, as we can make the chain, starting from the word "limit", "limit" -> "limi" -> "lii" -> "li".To solve this, we will follow these stepsDefine a function solve() . This will take ... Read More

Program to print maximum number of characters by copy pasting in n steps in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 09:00:23

113 Views

Suppose we have a number n; we have to find the maximum number of characters we can enter using n operations where each operation is likeInserting the character "x".Copy all characters.PasteSo, if the input is like n = 12, then the output will be 81.To solve this, we will follow these stepsif n

Program to count minimum number of operations required to make numbers non coprime in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:57:53

321 Views

Suppose we have two numbers A and B. Now in each operation, we can select any one of the number and increment it by 1 or decrement it by 1. We have to find the minimum number of operations we need such that the greatest common divisor between A and B is not 1.So, if the input is like A = 8, B = 9, then the output will be 1, as we can select 9 then increase it to 10, so 8 and 10 are not coprime.To solve this, we will follow these steps:if gcd of a and b ... Read More

Program to find next state of next cell matrix state in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:54:17

183 Views

Suppose we have a 2D binary matrix where a 1 means a live cell and a 0 means a dead cell. A cell's neighbors are its immediate horizontal, vertical and diagonal cells. We have to find the next state of the matrix using these rulesAny living cell with two or three living neighbors lives.Any dead cell with three living neighbors becomes a live cell.All other cells die.So, if the input is like1100010001011101then the output will be1100010001001100To solve this, we will follow these steps:n := row size of matrix, m := column size of matrixres := a matrix of size n ... Read More

Program to remove all nodes with only one child from a binary tree in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:51:15

696 Views

Suppose we have a binary tree root; we have to remove all nodes with only one child.So, if the input is likethen the output will beTo solve this, we will follow these steps:Define a method called solve(), this will take tree rootif root is null, thenreturn rootif left of root is null and right of root is null, thenreturn rootif left of root is null, thenreturn solve(right of root)if right of root is null, thenreturn solve(left of root)left of root := solve(left of root)right of root := solve(right of root)return rootExampleclass TreeNode:    def __init__(self, data, left = None, right ... Read More

Program to find length of longest possible stick in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:47:08

277 Views

Suppose we have a list of integers sticks. Here each element in the list represents a stick with two ends, these values are between 1 and 6. These are representing each end. We can connect two sticks together if any of their ends are same. The resulting stick's ends will be the leftover ends and its length is increased. We have to find the length of the longest stick possible.So, if the input is like sticks = [ [2, 3], [2, 4], [3, 5], [6, 6] ], then the output will be 3, as we can connect [2, 3] and ... Read More

Program to find a matrix for each condominium's height is increased to the maximum possible height in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:44:10

265 Views

Suppose we have a 2D matrix, where matrix [r, c] represents the height of a condominium in a city. The west-east skyline is visible by taking the maximum of each row in the matrix. And the north-south skyline can be visible by taking the maximum of each column. We have to find a new matrix where each condominium's height is increased to the maximum possible height while keeping the same west-east and north-south skyline.So, if the input is like23456789104447778910as the west-east skyline is [4, 7, 10] and north-south skyline is [8, 9, 10]. We can increase everything in the first ... Read More

Program to find sum of concatenated pairs of all each element in a list in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:41:57

571 Views

Suppose we have a list of numbers called nums. We have to find the sum of every concatenation of every pair of numbers in nums. Here the pair (i, j) and pair (j, i) are considered different.So, if the input is like nums = [5, 3], then the output will be 176, as We have the following concatenations: (nums[0] + nums[0]) = (5 concat 5) = 55, (nums[0] + nums[1]) = (5 concat 3) = 53, (nums[1] + nums[0]) = (3 concat 5) = 35, (nums[0] + nums[0]) = (3 concat 3) = 33, then the sum is 55 + ... Read More

Program to find length of concatenated string of unique characters in Python?

Arnab Chakraborty
Updated on 10-Nov-2020 08:36:22

260 Views

Suppose we have a list of strings words. We have to make a string that is constructed by concatenating a subsequence of words such that each letter is unique. We have to finally find the length of the longest such concatenation.So, if the input is like words = ["xyz", "xyw", "wab", "cde"], then the output will be 9, as we cannot pick any word since they contain duplicate characters.To solve this, we will follow these stepsans := 0Define a function recur() . This will take i:= 0, cur:= blank stringif i is same as size of words , then   ... Read More

Advertisements