Found 10476 Articles for Python

Program to find maximum building height in Python

Arnab Chakraborty
Updated on 08-Oct-2021 08:27:50

839 Views

Suppose we have a value n and another list of pairs called restrictions. We want to build n new buildings in a city. But there are few restrictions. We can built in a line and buildings are labeled from 1 to n. The restrictions has two parameters, so restrictions[i] = (id_i, max_height_i) indicates id_i must have height less than or equal to max_height_i. The city restrictions on the heights of the new buildings are as follows −The height of each building must be 0 or positive values.First building height must be 0.The difference between any two adjacent buildings height cannot ... Read More

Program to find XOR sum of all pairs bitwise AND in Python

Arnab Chakraborty
Updated on 08-Oct-2021 08:24:37

1K+ Views

Suppose we have two arrays arr1 and arr2. The XOR sum of a list is the bitwise XOR of all of its elements. If the list has only one element, then its XOR sum will be the element itself. Now, consider the list has the result of arr1[i] AND arr2[j] (bitwise AND) for every index pair (i, j) where 0

Program to find minimum number of operations to make string sorted in Python

Arnab Chakraborty
Updated on 08-Oct-2021 08:19:25

263 Views

Suppose we have a string s. We have to perform the following operation on s until we get a sorted string −Select largest index i such that 1

Program to find number of different subsequences GCDs in Python

Arnab Chakraborty
Updated on 08-Oct-2021 08:08:56

203 Views

Suppose we have an array nums with positive values. We have to find the number of different GCDs among all non-empty subsequences of nums. As we know the GCD of a sequence of numbers is the greatest value that divides all the numbers in the sequence evenly.So, if the input is like nums = [4, 6, 18], then the output will be 4 because gcd([4]) = 4, gcd([6]) = 6, gcd([18]) = 18 gcd([4, 6]) = 2, gcd([4, 18]) = 2, gcd([6, 18]) = 6, gcd([4, 6, 18]) = 2 so all numbers are [4, 6, 18, 2], there are ... Read More

Program to find maximum number of groups getting fresh donuts in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:58:58

155 Views

Suppose we have a value batchSize and an array group where groups[i] denotes that there is a group of groups[i] customers that will visit the shop. So there is a donuts shop that bakes donuts in batches of given batchSize. But they have one rule, they must serve all of the donuts of a batch before serving any donuts of the next batch. And each customer will get exactly one donut. When a group enters the shop, all customers of that group must be served before addressing any next groups. One group may happy if they all get fresh donuts. ... Read More

Program to maximize number of nice divisors in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:55:56

185 Views

Suppose we have a number pf represents number of prime factors. We have to make a positive number n that satisfies the following conditions −The number of prime factors of n (may or may not be distinct) is at most pf.The number of nice divisors of n is maximized. As we know a divisor of n is nice when it is divisible by every prime factor of n.We have to find the number of nice divisors of n. If the answer is too large then return result modulo 10^9 + 7.So, if the input is like pf = 5, then ... Read More

Program to count pairs with XOR in a range in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:53:47

491 Views

Suppose we have an array nums and have two values l and r, we have to find the number of nice pairs. Here a nice pair is a pair (i, j) where 0 >= 1       return res // 2    return test(nums, r + 1) - test(nums, l) nums = [4,1,7,2] l = 2 r = 6 print(solve(nums, l, r))Input[4,1,7,2], 2, 6 Output6

Program to find maximize score after n operations in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:50:20

491 Views

Suppose we have an array called nums, whose size is 2*n. We have to perform n operations on this array. In the ith operation (1-indexed), we will do the following:Select two elements, x and y.Get a score of i*gcd(x, y).Remove x and y from the array nums.We have to find the maximum score we can get after performing n operations.So, if the input is like nums = [6, 2, 1, 5, 4, 3], then the output will be 14 because the optimal choices are (1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 ... Read More

Program to find out the maximum value of a 'valid' array in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:45:25

239 Views

Suppose, we have an array of n integers 'nums'. Each value in 'nums' represent its 'power'. The array will be evaluated 'valid' if the length of the array is greater than two and the first and last value of the array is equal. We have to make the array valid by deleting elements from the array so that the rest can satisfy the condition. As output, we return the maximum possible power value of the array by adding all the power values of the array.So, if the input is like nums = [3, 4, 5, 3, 4], then the output ... Read More

Program to find maximum score of a good subarray in Python

Arnab Chakraborty
Updated on 08-Oct-2021 07:43:00

495 Views

Suppose we have an array called nums and a value k. Consider the score of a subarray (i, j) is defined as minimum of subarray nums[i..j] * (j-i+1). Now, a good subarray is a subarray where i -1 and nums[i] >= minNum, doi := i - 1while j < size of nums and nums[j] >= minNum, doj := j + 1ans := maximum of ans and ((j - i - 1) * minNum)minNum := maximum of (nums[i] if i > -1 otherwise -1) and (nums[j] if j < size of nums otherwise -1)return ansExampleLet us see the following implementation ... Read More

Advertisements