Found 7197 Articles for C++

String Without AAA or BBB in C++

Arnab Chakraborty
Updated on 30-Apr-2020 14:04:45

276 Views

Suppose we have two integers A and B, we have to return any string S, such that −S has length A + B and contains exactly A number of letter ‘a’ and B number of ‘b’ letters.Substring “aaa” and “bbb” will not be in the string SSo if the given integers are A = 4, B = 1, then the result will be “aabaa”.To solve this, we will follow these steps −Define a string ret, initially this is emptywhile |A – B| >= 2, if A > B, thenret := ret concatenate ‘aa’decrease A by 2if B is non-zero concatenate ... Read More

Time Based Key-Value Store in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:59:02

833 Views

Suppose we have to make a timebased key-value store class called TimeMap, that supports two operations.set(string key, string value, int timestamp): This will store the key and value, along with the given timestamp.get(string key, int timestamp): This will return a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev

Longest Turbulent Subarray in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:54:39

416 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

Numbers With Same Consecutive Differences in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:44:37

299 Views

Suppose we have to find all non-negative integers of length N such that the absolute difference between every two consecutive digits is K. We have to keep in mind that every number in the answer must not have leading zeros except for the number 0 itself. We may return the answer in any order. So if N = 3 and K = 7, then output will be [181, 292, 707, 818, 929], Here we can see 070 is not a valid number, because it has one leading zero.To solve this, we will follow these steps −Create one matrix called dp, ... Read More

Maximum Width Ramp in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:41:11

411 Views

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

Prison Cells After N Days in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:33:33

467 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
Updated on 30-Apr-2020 13:29:46

301 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
Updated on 30-Apr-2020 13:25:09

449 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
Updated on 30-Apr-2020 13:11:23

420 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

Minimum Increment to Make Array Unique in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:08:33

361 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

Advertisements