Articles on Trending Technologies

Technical articles with clear explanations and examples

Longest Turbulent Subarray in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 471 Views

Consider a subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent when it meets these conditions −For i A[k+1] when k is odd, and A[k] < A[k+1] when k is even;Otherwise, for i A[k+1] when k is even, and A[k] < A[k+1] when k is odd.So the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray. Now find the length of a maximum size turbulent subarray of A. So if the input is like [9, 4, 2, 10, 7, 8, 8, 1, 9], output is 5. This ...

Read More

Maximum Width Ramp in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 471 Views

Suppose we have an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i]

Read More

Prison Cells After N Days in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 521 Views

Suppose there are 8 prison cells in a row, and in each cell there is a prisoner or that is empty. In each day, whether the cell is occupied or vacant changes according to the following rules −If one cell has two adjacent neighbors that are both occupied or both vacant, then the cell becomes occupied.Otherwise, it becomes empty.We will describe the current state of the prison in the following way: cells[i] will be 1 if the i-th cell is occupied, else cells[i] will be 0.So we have the initial state of the prison, then return the state of the ...

Read More

Array of Doubled Pairs in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 371 Views

Suppose we have an array of integers A with even length, now we have to say true if and only if it is possible to reorder it in such a way that A[2 * i + 1] = 2 * A[2 * i] for every 0 0, thenif m[key of kv] is not 0 and m[2* key of kv] > 0x := min of m[key of kv] and m[2* key of kv]cnt := cnt – (x * 2)decrease m[2 * key of kv] by xdecrease m[key of kv] by xotherwise when key of kv = 0, thencnt := cnt ...

Read More

Reveal Cards In Increasing Order in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 490 Views

Suppose we have a deck of cards; every card has a one unique number. We can order the deck in any order that we want. So Initially, all the cards start face down (unrevealed) in one deck. Now, we do the following steps multiple times, until all cards are revealed −Suppose we have a deck of cards; every card has a one unique number. We can order the deck in any order that we want. So Initially, all the cards start face down (unrevealed) in one deck. Now, we do the following steps multiple times, until all cards are revealed ...

Read More

Bag of Tokens in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 461 Views

Suppose we have an initial power P, an initial score of 0 points, and one bag of tokens. Now each token can be used at most once, there is a value token[i], and has potentially two ways to use it, these are as follows −If we have at least token[i] power, then we may play the token face up, losing token[i] power, and gaining 1 point.Otherwise when we have at least 1 point, we may play the token face down, gaining token[i] power, and losing 1 point.We have to find the largest number of points that we can have after ...

Read More

Remove Sub-Folders from the Filesystem in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 325 Views

Suppose we have a list of folders, we have to remove all sub-folders in those folders and return in any order the folders after removing. Here if a folder[i] is located within another folder[j], it is denoted as subfolder of it. The paths will be like folder1/subfolder2/… etc.Suppose the input is like["/myfolder", "/myfolder/secondfolder", "/another/document", "/another/document/extrafolder", "/another/final"], then the output will be: ["/myfolder", "/another/final", "/another/document"]To solve this, we will follow these steps −sort the folder array based on the length of the pathscreate one map m, and another array ansfor i in range 0 to size of path array – 1s ...

Read More

Minimum Increment to Make Array Unique in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 408 Views

Suppose we have an array of integers A, here a move consists of choosing any A[i], and incrementing it by 1. We have to find the least number of moves to make every value in A unique. So if the input is like [3, 2, 1, 2, 1, 7], then the output will be 6, as after 6 moves, the array could be [3, 4, 1, 2, 5, 7], it can be shown with 5 or less moves that it is impossible for the array to have all distinct values.To solve this, we will follow these steps −ret:= 0sort array ...

Read More

Toss Strange Coins in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 708 Views

Suppose we have some coins. The i-th coin has a probability prob[i] of facing heads when tossed. We have to show the probability that the number of coins facing heads equals target if you toss every coin exactly once. So if the prob array is like [0.5, 0.5, 0.5, 0.5, 0.5] and target is 0, then the output will be 0.03125.To solve this, we will follow these steps −n := size of prob arraycreate one 2d array of size n x (target + 5)set dp[0, 0] = 1 – prob[0] and dp[0, 1] := prob[0]for i in range 1 to ...

Read More

Beautiful Array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Apr-2020 1K+ Views

Suppose for some fixed value of N, an array A is beautiful when it is a permutation of the integers 1, 2, ..., N, such that −For every i < j, there is no such k with i < k < j such that A[k] * 2 = A[i] + A[j].Suppose we have N, we have to find any beautiful array A.So if the input is like 5, then the output will be [3, 1, 2, 5, 4]To solve this, we will follow these steps −Create one array called ret, insert 1 into retwhile size of ret < Ncreate an ...

Read More
Showing 47531–47540 of 61,248 articles
Advertisements