Found 26504 Articles for Server Side Programming

Distant Barcodes in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:16:13

162 Views

Suppose in a warehouse, there is a row of barcodes. The i-th barcode is barcodes[i]. We have to rearrange the barcodes so that no two adjacent barcodes are same. So if the input is [1, 1, 1, 2, 2, 2] so output is [2, 1, 2, 1, 2, 1].To solve this, we will follow these steps −make one map named dstore the frequency of numbers present in the barcode array into dx := empty listinsert all key-value pairs into xi := 0res := make a list whose length is same as barcodes, and fill [0]sort x based on the frequencywhile ... Read More

Previous Permutation With One Swap in Python

Arnab Chakraborty
Updated on 30-Apr-2020 12:09:47

423 Views

Suppose we have an array A of positive integers (not necessarily unique), we have to find the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]). If it cannot possible, then return the same array. So if the array is like [3, 2, 1], then the output will be [3, 1, 2], by swapping 2 and 1To solve this, we will follow these steps −n := size of Afor left in range n – 2 down to -1if left = -1, then return ... Read More

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

256 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

338 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

576 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

509 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

279 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

403 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

Advertisements