Found 7197 Articles for C++

Non-negative Integers without Consecutive Ones in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:42:46

361 Views

Suppose we have a positive integer n. We have to find the non-negative integers less than or equal to n. The constraint is that the binary representation will not contain consecutive ones. So if the input is 7, then the answer will be 5, as binary representation of 5 is 101.To solve this, we will follow these steps −Define a function convert(), this will take n, ret := empty stringwhile n is non-zero, do −ret := ret + (n mod 2)n := right shift n, 1 timereturn retFrom the main method, do the following −bits := call the function convert(num)n ... Read More

Find the Closest Palindrome in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:40:05

246 Views

Suppose we have a number n, we have to get the closest number that is palindrome. So the palindrome could be less than or greater than the number whose absolute difference is smaller. So if the number is like 145, then the result will be 141.To solve this, we will follow these steps −sn := size of nif sn is same as 1, then −decrease n[0] by 1 and return a string of 1s of size n[0]half_sn := (sn + 1) / 2half_val := stol(substring of n from index 0 to half_snDefine an array candidates = {10^(sn) – 1, 10^(sn ... Read More

Student Attendance Record II in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:37:19

548 Views

Suppose we have a positive integer n, we have to find the number of all possible attendance records with length n, which will be regarded as rewardable. As the answer may be very large, we will return it using mod 109 + 7.In the student attendance record the string can only contain the following three characters −'A' means absent.'L' means late.'P' means present.one attendance is treated as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late). So we have to find the maximum points.If the input is like 2, then the output ... Read More

Remove Boxes in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:35:07

202 Views

Suppose we have several boxes with different colors These colors are represented by different positive numbers. We can experience several rounds to remove boxes until there is no box left. In each round we can choose some continuous boxes with the same color (composed of k boxes, k >= 1), and remove them and get k*k points. So if the input is like − [1, 3, 2, 2, 2, 4, 4, 3, 1], then the output will be 21.Find the maximum points you can get.To solve this, we will follow these steps −Define a function solve(), this will take an ... Read More

Super Washing Machines in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:31:06

605 Views

Suppose we have n super washing machines on a row. Initially, each washing machine has some dresses or empty. Now, for each move, we can choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time. Suppose we have one integer array representing the number of dresses in each washing machine from left to right on the row, we should find the minimum number of moves to make all the washing machines have the same number of clothes. If it is not ... Read More

IPO in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:30:11

428 Views

Suppose one company A wants to start its IPO soon. In order to sell a good price of its shares to B, A would like to work on some projects to increase its capital before the IPO. A has limited resources, it can only finish at most k distinct projects before the IPO. Can you help A, by designing the best way to maximize its total capital after finishing at most k distinct projects?Suppose we have several projects. For each project i, it has a pure profit Pi and a minimum capital of Ci is needed to start the corresponding ... Read More

Reverse Pairs in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:29:05

901 Views

Suppose we have an array, in this array we will say one pair (A[i] and A[j]) as important reverse pairs if this satisfies the following condition −if i < j and A[i] > 2* nums[j]We have to find the number of important reverse pairs. So if the input is like [2,8,7,7,2], then the result will be 3.To solve this, we will follow these steps −ans := 0Define a function merge(), this will take an array a, low, mid, high,k := high - low + 1Define an array temp of size ki := low, j = mid + 1, k := 0first := mid + 1while i

Zuma Game in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:26:46

846 Views

Let us consider about the Zuma Game. Suppose we have a row of balls on the table, these balls are colored as red(R), yellow(Y), blue(B), green(G), and white(W). We also have several balls with us.Now, each time, we may choose a ball from our side, and insert it into the row. Then, if there is a group of 3 or more balls in the same color touching, remove them. Keep doing this until no more balls can be removed.We have to find the minimal balls we have to insert to remove all the balls on the table. If we cannot ... Read More

Sliding Window Median in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:21:03

1K+ Views

Suppose we have a list of numbers, and we have one window size k, we have to find the list of medians using sliding window manner. So, if the distribution is like below −Window PositionMedian13-1-35368113-1-35368-113-1-35368-113-1-35368313-1-35368513-1-353686Here we have considered the k is 3, and the result will be [1, -1, -1, 3, 5, 6]To solve this, we will follow these steps −Define one set arrDefine a function insert(), this will take x, insert x into arrDefine a function delete_(), this will take x, delete x from arr, if this existsDefine a function getMedian()n := size of arra := jump to n/2 ... Read More

Largest Palindrome Product in C++

Arnab Chakraborty
Updated on 01-Jun-2020 11:10:12

152 Views

Suppose we have input n, we have to find the largest palindrome that can be made using multiplication of two n digit numbers. As the numbers are very large, we can perform mod using 1337. So if the input is say 2, then the answer will be 987, 987 = (99*91) mod 1337 = 9009 mod 1337 = 987.To solve this, we will follow these steps −maxVal := 10^n – 1minVal := maxVal / 10for initialize h := maxVal, when h > minVal, update (decrease h by 1), do −left := h, right := 0for initialize i := h, when ... Read More

Advertisements