Programming Articles - Page 2015 of 3363

Reveal Cards In Increasing Order in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:25:09

469 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

443 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

384 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

Binary Subarrays With Sum in C++

Arnab Chakraborty
Updated on 30-Apr-2020 13:01:18

551 Views

Suppose an array A of 0s and 1s is given, we have to find how many non-empty subarrays have sum S? So if the input is like [1, 0, 1, 0, 1], and S = 2, then the result will be 4, as the subarrays are [1, 0, 1, 0, 1], [1, 0, 1, 0, 1], [1, 0, 1, 0, 1], [1, 0, 1, 0, 1].To solve this, we will follow these steps −Define a method called atMost(), this will take array A and integer xif x < 0, then return 0, set j := 0 and set ret := ... Read More

Flip String to Monotone Increasing in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:55:51

387 Views

Suppose a string of '0's and '1's is given. That string will be monotonic increasing if it consists of some number of '0's (possibly 0), followed by some number of '1's (also possibly 0.). We have a string S of '0's and '1's, and we may flip any '0' to a '1' or a '1' to a '0'. Find the minimum number of flips to make S monotone increasing. So if the input is like “010110”, then the output will be 2. By flipping we can get “011111” or “000111”.To solve this, we will follow these steps −n := size ... Read More

Partition Array into Disjoint Intervals in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:52:20

467 Views

Suppose we have an array A, we have to partition it into two subarrays left and right such that −Every element in left subarray is less than or equal to every element in right subarray.left and right subarrays are non-empty.left subarray has the smallest possible size.We have to find the length of left after such a partitioning. It is guaranteed that such a partitioning exists.So if the input is like [5, 0, 3, 8, 6], then the output will be 3, as left array will be [5, 0, 3] and right subarray will be [8, 6].To solve this, we will ... Read More

Sort an Array in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:48:57

314 Views

Suppose we have an array of integers; we have to sort them in ascending order. So if the array is like [5, 2, 3, 1], then the result will be [1, 2, 3, 5]To solve this, we will follow these steps −Make one method called partition, this will take array, low and highset pivot := lowfor i in range low to high – 1if nums[i] < nums[high], then swap(nums[i] and nums[pivot]), increase pivot by 1swap nums[pivot] and nums[high]Define a method called sortArr(), this will take array, low and highif low >= high, then returnpartitionIndex := partition(nums, low, high)sortArr(nums, low, partitionIndex ... Read More

Online Election in C++

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

639 Views

Suppose in an election, the i-th vote was cast for persons[i] at time times[i]. Now, we have to implement the following query function: TopVotedCandidate.q(int t) this will find the number of the person that was leading the election at time t. Votes cast at time t will count towards our query. If there is a tie, the most recent vote (among tied candidates) wins.So if we initialize this with TopVotedCandidate([0, 1, 1, 0, 0, 1, 0], [0, 5, 10, 15, 20, 25, 30]), then call q() like: q(3), q(12), q(25), q(15), q(24), q(8), then the result will be [0, 1, ... Read More

Smallest Range II in C++

Arnab Chakraborty
Updated on 30-Apr-2020 12:40:40

286 Views

Suppose we have an array A of integers, for each integer A[i] we have to choose either x = -K or x = K, and add x to A[i] (only once). So after this process, we have some array B. We have to find the smallest possible difference between the maximum value of B and the minimum value of B. So if the input is A = [0, 10], K = 2, then the output will be 6, as B = [2, 8].To solve this, we will follow these steps −set ret := 0, n := size of array Asort ... Read More

RLE Iterator in C++

Arnab Chakraborty
Updated on 30-Apr-2020 11:03:20

344 Views

Suppose we have to create an iterator that iterates through a run-length encoded sequence. Here the iterator is initialized by calling RLEIterator(int[] A), where A is a run-length encoding of a sequence. So we can say that for all even i, A[i] tells us the number of times that the non-negative integer value A[i+1] is repeated in the sequence. Here iterator supports one function −next(int n): This function exhausts the next n elements (n >= 1) and returns the last element exhausted in this way. So if there is no element left to exhaust, next returns -1 instead.Suppose we start ... Read More

Advertisements