Found 10783 Articles for Python

Find maximum sum of triplets in an array such than i < j < k and a[i] < a[j] < a[k] in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:05:35

324 Views

Suppose we have an array of positive numbers, there are n elements in that array, we have to find the maximum sum of triplet (ai + aj + ak ) such that 0 A[i], thensecond_max := maximum of second_max, A[j]if first_max and second_max is non-zero, thenres := maximum of res, first_max + A[i] + second_maxreturn resExampleLet us see the following implementation to get better understanding − Live Demodef get_max_triplet_sum(A) :    n = len(A)    res = 0    for i in range(1, (n - 1)) :       first_max = 0       second_max = 0   ... Read More

Find maximum points which can be obtained by deleting elements from array in Python

Arnab Chakraborty
Updated on 25-Aug-2020 11:02:23

217 Views

Suppose we have an array A with N elements, we also have two integers l and r where, 1≤ ax ≤ 10^5 and 1≤ l≤ r≤ N. Taking an element from the array say ax and remove it, and also remove all elements equal to ax+1, ax+2 … ax+R and ax-1, ax-2 … ax-L from that array. By doing this it will cost ax points. We have to maximize the total cost after removing all of the elements from the array.So, if the input is like A = [2, 4, 3, 10, 5], l = 1, r = 2, then ... Read More

Find maximum operations to reduce N to 1 in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:59:50

169 Views

Suppose we have two numbers P and Q and they form a number N = (P!/Q!). We have to reduce N to 1 by performing maximum number of operations possible. In each operation, one can replace N with N/X when N is divisible by X. We will return the maximum number of operations that can be possible.So, if the input is like A = 7, B = 4, then the output will be 4 as N is 210 and the divisors are 2, 3, 5, 7.To solve this, we will follow these steps −N := 1000005factors := an array of ... Read More

Find maximum N such that the sum of square of first N natural numbers is not more than X in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:57:52

125 Views

Suppose we have a given integer X, we have to find the maximum value N so that the sum of first N natural numbers should not exceed the value X.So, if the input is like X = 7, then the output will be 2 as 2 is the maximum possible value of N, for N = 3, the sum of the series will exceed X = 7 So, 1^2 + 2^2 + 3^2 = 1 + 4 + 9 = 14.To solve this, we will follow these steps −Define a function sum_of_squares() . This will take Nres :=(N *(N + ... Read More

Find maximum length Snake sequence in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:54:38

402 Views

Suppose we have a grid of numbers; we have to find a snake sequence and return it. If there are multiple snake sequence, then return only one. As we know a snake sequence is made using adjacent numbers in the grid so for each number, the number on the right-hand side or the number below it is either +1 or -1 its value. So, if current value is in grid cell (a, b), we can either move right (a, b+1) if that number is ± 1 or move below (a+1, b) if that number is ± 1.So, if the input ... Read More

Find maximum distance between any city and station in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:50:44

192 Views

Suppose we have N number of cities, and they are numbered from 0 to N-1 and we also have the cities in which stations are located, we have to find the maximum distance between any city and its nearest station. We have to keep in mind that the cities with stations can be given in any order.So, if the input is like N = 6 and stations = [2, 4], then the output will be 2To solve this, we will follow these steps −station_present := a list of size N, and fill with Falsefor each city in station, dostation_present[city] := ... Read More

Find maximum difference between nearest left and right smaller elements in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:49:04

150 Views

Suppose we have an array of integers; we have to find the maximum absolute difference between the nearest left and the right smaller element of each of the elements in the array. If there is no smaller element on the right-hand side or left-hand side of any element then we will put zero as the smaller element.So, if the input is like A = [3, 5, 9, 8, 8, 10, 4], then the output will be 4 as left elements L = [0, 3, 5, 5, 5, 8, 3], right elements R = [0, 4, 8, 4, 4, 4, 0], ... Read More

Find lost element from a duplicated array in Python

Arnab Chakraborty
Updated on 25-Aug-2020 10:01:10

212 Views

Suppose we have two arrays which are duplicates of each other except one element, so, one element from one of the given arrays is missing, we have to find that missing element.So, if the input is like A = [2, 5, 6, 8, 10], B = [5, 6, 8, 10], then the output will be 2 as 2 is missing from second array.To solve this, we will follow these steps −Define a function solve() . This will take A, B, Nif N is same as 1, thenreturn A[0];if A[0] is not same as B[0], thenreturn A[0]low := 0, high := ... Read More

Find longest palindrome formed by removing or shuffling chars from string in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:58:29

207 Views

Suppose we have a string; we have to find the longest palindrome that can be generated by deleting or shuffling the characters from the string. And if there are more than one palindrome then return only one.So, if the input is like pqqprrs, then the output will be pqrsrqp.To solve this, we will follow these steps −count := array of size 256, filled with 0for i in range 0 to size of string, docount[ASCII of(string[i]) ] := count[ASCII of(string[i]) ] + 1begin := blank string, mid := blank string, end := blank stringcharacter := ASCII of('a')while character

Find longest bitonic sequence such that increasing and decreasing parts are from two different arrays in Python

Arnab Chakraborty
Updated on 25-Aug-2020 09:56:08

71 Views

Suppose we have two arrays; we have to find the longest possible bitonic sequence so that the increasing part should be from first array and should be a subsequence of first array. similarly decreasing part of must be from second array and a subsequence of the second one.So, if the input is like A = [2, 6, 3, 5, 4, 6], B = [9, 7, 5, 8, 4, 3], then the output will be [2, 3, 4, 6, 9, 7, 5, 4, 3]To solve this, we will follow these steps −Define a function index_ceiling() . This will take arr, T, ... Read More

Advertisements