Server Side Programming Articles - Page 1806 of 2650

Partition Array for Maximum Sum in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:06:21

1K+ Views

Suppose we have an integer array A, we have to partition the array into (contiguous) subarrays of length at most K. After partitioning, each subarray has their values changed to become the maximum value of that subarray. We have to find the largest sum of the given array after partitioning. So if input is like [1, 15, 7, 9, 2, 5, 10] and k = 3, then the output will be 84. This is because the array becomes [15, 15, 15, 9, 10, 10, 10]To solve this, we will follow these steps −make one array dp of length same as ... Read More

Arithmetic Slices in C++

Arnab Chakraborty
Updated on 30-Apr-2020 08:07:41

266 Views

Suppose we have a sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same. So for example, these are arithmetic sequence: [1, 3, 5, 7, 9], [7, 7, 7, 7], [3, -1, -5, -9], But the following sequence is not arithmetic. [1, 1, 2, 5, 7]Now a zero-indexed array A consisting of N numbers is given. A slice of that given array is any pair of integers (P, Q) such that 0

Smallest Integer Divisible by K in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:52:31

356 Views

Suppose we have a positive integer K, we need find the smallest positive integer N such that N is divisible by K, and N only contains the digit 1. We have to find the length of N. If there is no such N, return -1. So if the input is like 3, then the output will be 3. The smallest answer will be N = 111.To solve this, we will follow these steps −if k is even, or k is divisible by 5, then return -1set r := 0 and N = 1for i in range 1 to K + ... Read More

Clumsy Factorial in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:49:18

2K+ Views

As we know that the factorial of a positive integer n is the product of all positive integers less than or equal to n. So factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We will try to find a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order.The clumsy factorial is like clumsy(10) = 10 * 9 / 8 + 7 - 6 * ... Read More

Max Consecutive Ones III in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:45:44

588 Views

Suppose we have an array A of 0s and 1s, we can update up to K values from 0 to 1. We have to find the length of the longest (contiguous) subarray that contains only 1s. So if A = [1,1,1,0,0,0,1,1,1,1,0] and k = 2, then the output will be 6, So if we flip 2 0s, the array can be of like [1,1,1,0,0,1,1,1,1,1,1], the length of longest sequence of 1s is 6.To solve this, we will follow these steps −set ans := 0, j := 0 and n := size of arrayfor i in range 0 to n – 1if A[i] is 0, then decrease k by 1while j

Subarray Sums Divisible by K in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:42:38

524 Views

Suppose we have an array A of integers. We have to find the number of contiguous non-empty subarray, that have a sum divisible by k. If A = [4, 5, 0, -2, -3, 1] and k = 5, then the output will be 7. There are seven subarrays. [[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]]To solve this, we will follow these steps −make one map m and set m[0] as 1temp := 0, ans := 0, and n := size of array afor i in range 0 to ... Read More

Minimum Falling Path Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:40:40

294 Views

Suppose we have a square array of integers A, we want the minimum sum of a falling path through A. Falling path is basically a path that starts at any element in the first row, and chooses one element from each row. And the next row's element must be in a column that is different from the previous row's column by at most one. So if the matrix is like −123456789Then the output is 12. There are few different falling paths. these are [1, 4, 7], [1, 4, 8], [1, 5, 7], [1, 5, 8], [1, 5, 9], [2, 4, ... Read More

Minimum Add to Make Parentheses Valid in Python

Arnab Chakraborty
Updated on 30-Apr-2020 07:38:19

412 Views

Suppose we have a string S of '(' and ')' parentheses, we add the minimum number of parentheses at any positions, so that the resulting parentheses string is valid. A parentheses string is valid if and only if −It is the empty stringIt can be written as XY (X concatenated with Y), where X and Y are valid stringsIt can be written as (A), where A is a valid string.So if the string is like "()))((", then we need to add 4 more parentheses to make the string valid.To solve this, we will follow these steps −if S is empty, ... Read More

Fruit Into Baskets in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:35:38

1K+ Views

Suppose we have a row of trees, the i-th tree produces fruit with type tree[i]. we can start at any tree of our choice, then repeatedly perform these steps −Add one piece of fruit from this tree to our baskets. If there is no chance, then stop.Move to the next tree to the right of the current one. If there is no tree to the right, then stop.We have two baskets, and each basket can carry any quantity of fruit, but we want each basket should only carry one type of fruit each. We have to find the total amount ... Read More

Online Stock Span in C++

Arnab Chakraborty
Updated on 30-Apr-2020 07:33:05

214 Views

Suppose we have an API, that collects daily price quotes for some stock, and returns the span of that stock's price for the current day. Here the span of the stock's price today is defined as −The maximum number of consecutive days (starting from today and going backwards) where the price of the stock was less than or equal to today's price.For example, if we see 7 days stock share records like [100, 80, 60, 70, 60, 75, 85], then the stock spans would be [1, 1, 1, 2, 1, 4, 6]. We have to write the actual module for ... Read More

Advertisements