Found 7197 Articles for C++

Stone Game II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:44:15

451 Views

Suppose there are two persons Alice and Bob, they are continuing their games with piles of stones. There are a number of piles placed in a row, and each pile has a positive integer number of stones in an array piles[i]. Our objective of the game is to end with the most stones. Alice and Bob take the turns, with Alice starting first. Initially, M = 1. On each player's turn, that player can take all the stones in the first X remaining piles, here 1

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

Max Consecutive Ones III in C++

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

575 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

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

200 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

Most Profit Assigning Work in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:04:56

359 Views

Suppose we have jobs difficulty[i] and this array indicates the difficulty of the ith job, and profit[i] is the profit of the ith job. Now consider we have some workers. worker[i] is the ability of the ith worker, this means that this worker can only complete a job with difficulty at most worker[i]. Every worker can do at most one job, but one job can be completed multiple times. We have to find what is the most profit we can make?For example, if the input is like difficulty = [2, 4, 6, 8, 10] and profit = [10, 20, 30, ... Read More

Number of Matching Subsequences in C++

Arnab Chakraborty
Updated on 30-Apr-2020 06:00:45

334 Views

Suppose we have a string S and a dictionary of words words, find the number of words[i] that is a subsequence of S. So if the input is S= “abcde” and dictionary is [“a”, “bb”, “acd”, “ace”], then output will be 3. Because there are three sequence of words in the dictionary, that are a subsequence of S: “a” “acd” and “ace”To solve this, we will follow these steps −n := size of words arraycreate one map mfor i in range 0 to size of wordsinsert words[i] into the map m[words[i, 0]] positionans := 0for i in range 0 to ... Read More

Reorganize String in C++

Arnab Chakraborty
Updated on 30-Apr-2020 05:57:20

640 Views

Suppose we have a string S, check whether the letters can be rearranged so that two characters that are adjacent to each other are not the same. If that is possible, output any possible result. If that is not possible, return the empty string. So if the input is like “AAB”, then the output will be “ABA”.To solve this, we will follow these steps −Make a priority queue of integer character pairs called pq, define one map mn := size of the stringstore the character frequency in map mfor each key-value pair p in minsert (integer part of p, character ... Read More

Advertisements