Programming Articles - Page 1033 of 3363

Program to find the maximum score from all possible valid paths in Python

Arnab Chakraborty
Updated on 06-Oct-2021 09:00:33

613 Views

Suppose we have two arrays nums1 and nums2. A valid path is defined as follows −Select nums1 or nums2 to traverse (from index-0).Traverse the array from left to right.Now, if we are moving through any value that is present in nums1 and nums2 we can change the path to the other array. Here the score is the sum of unique values in a valid path. We have to find the maximum score we can get of all possible valid paths. If the answer is too large then return result modulo 10^9+7.So, if the input is like nums1 = [3, 5, ... Read More

Program to find min length of run-length encoding after removing at most k characters in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:57:34

263 Views

Suppose we have a string s and another value k. We can delete at most k characters from s such that the length of run-length encoded version of s is minimum. As we know the run-length encoding is a string compression method that replaces consecutive identical characters (2 or more times) with the concatenation of the character and the number marking the count of the characters. For example, if we have a string "xxyzzz" then we replace "xx" by "x2" and replace "zzz" by "z3". So compressed string is now "x2yz3". So in this problem we have to find the ... Read More

Program to find maximum score from performing multiplication operations in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:55:36

354 Views

Suppose we have two arrays nums and multipliers of size n and m respectively (n >= m). The arrays are 1-indexed. Now our initial score is 0. We want to perform exactly m operations. On the ith operation (1-indexed), we will −Select one value from x from either the start or the end of the nums.Add multipliers[i] * x to the score.Remove x from the array nums.We have to find the maximum score after performing m operations.So, if the input is like nums = [5, 10, 15], multipliers = [5, 3, 2], then the output will be 115 because we ... Read More

Program to find minimum number of increments on subarrays to form a target array in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:54:33

367 Views

Suppose we have an array called target with positive values. Now consider an array initial of same size with all zeros. We have to find the minimum number of operations required to generate a target array from the initial if we do this operation: (Select any subarray from initial and increment each value by one.)So, if the input is like target = [2, 3, 4, 3, 2], then the output will be 4 because initially array was [0, 0, 0, 0, 0] first pass select subarray from index 0 to 4 and increase it by 1, so array will be ... Read More

Program to find minimum number of operations to move all balls to each box in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:51:33

795 Views

Suppose we have a binary string called boxes, where boxes[i] is '0' indicates the ith box is empty, and '1' indicates it contains one ball. Now, in one operation, we can move one ball from a box to an adjacent box. After doing so, there may be more than one ball in some boxes. We have to find an array answer of size n, where answer[i] is the minimum number of operations needed to move all the balls to the ith box.So, if the input is like boxes = "1101", then the output will be [4, 3, 4, 5]To put ... Read More

Program to find maximum number of non-overlapping substrings in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:55:00

673 Views

Suppose we have a string s with only lowercase letters, we have to find find the maximum number of non-empty substrings of s that meet the following rulesSubstrings are non-overlappingA substring that contains a specific character ch must also contain all occurrences of ch.We have to find the maximum number of substrings that meet these two conditions. If there are more than one such solutions with the same number of substrings, then return that with minimum total length.So, if the input is like s = "pqstpqqprrr", then the output will be ["s", "t", "rrr"] because all of the possible substrings ... Read More

Program to find best position for a service center in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:47:26

192 Views

Suppose we have a list of positions, which is containing a list of coordinate points where some houses are located. If you want to make a service center at (xc, yc) such that the sum of Euclidean distance from any given point to (xc, yc) is minimum. So we have to find the sum of minimum distance.So, if the input is like positions = [(10, 11), (11, 10), (11, 12), (12, 11)], then the output will be 4.0To solve this, we will follow these steps −numIter := 50Define a function total() . This will take cx, cy, positionstotal := 0.0for ... Read More

Program to check whether Amal can win stone game or not in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:42:04

217 Views

Suppose there are two players Amal and Bimal, they are playing a game, and with Amal starts first. Initially, there are n different stones in a pile. On each player's turn, he makes a move consisting of removing any square number (non-zero) of stones from the pile. Also, if one player is unable to make a move, he loses the game. So if we have n, we have to check whether Amal can win the game or not.So, if the input is like n = 21, then the output will be True because at first Amal can take 16, then ... Read More

Program to generate array by concatenating subarrays of another array in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:43:13

279 Views

Suppose we have one 2D array called groups, and another array nums. We have to check whether we can select n disjoint subarrays from the array nums such that the ith subarray is equal to groups[i] (0-indexed), and if i > 0, the (i-1)th subarray will appear before the ith subarray in nums.So, if the input is like groups = [[2, -2, -2], [4, -3, 0]] nums = [1, -1, 0, 2, -2, -2, 4, -3, 0], then the output will be true because array group[0] is present from index 3 to 5 of nums and group[1] is from index ... Read More

Program to find minimum possible integer after at most k adjacent swaps on digits in Python

Arnab Chakraborty
Updated on 06-Oct-2021 08:39:46

228 Views

Suppose we have a string called num representing a very large integer number and also have another value k. We can swap any two adjacent digits of the values at most k times. We have to find the minimum value we can get.So, if the input is like num = "5432" k = 4, then the output will be 2453 because at first number is 5432. Then after first phase it will be 4532, then 4523, then 4253 and at final phase 2453.To solve this, we will follow these stepsmin_num := sort the digits of numi := 0, to_find := ... Read More

Advertisements