C++ Articles - Page 367 of 719

Program to find Star number in C++

Ayush Gupta
Updated on 17-Sep-2020 04:47:24

171 Views

In this problem, we are given a number n. Our task is to create a program to find Star number in C++.Star Number is a special number that represents a centered hexagram (sixpoint star).Some start numbers are 1, 13, 37, 73, 121.Let’s take an example to understand the problemInputn = 5Output121Solution ApproachTo find the nth star number we will use the formula.Let’s see the general formula for the star number.n = 2 -> 13 = 12 + 1 = 6(2) + 1 n = 3 -> 37 = 36 + 1 = 6(6) + 1 n = 4 -> 73 ... Read More

Data Stream as Disjoint Intervals in C++

Arnab Chakraborty
Updated on 27-May-2020 06:27:14

239 Views

Suppose we have a data stream input of integers, these are like a1, a2, ..., an, ..., we have to summarize the numbers seen so far as a list of disjoint intervals. For example, suppose the input integers are 1, 3, 8, 2, 7, ..., then the summary will be −[1, 1][1, 1], [3, 3][1, 1], [3, 3], [8, 8][1, 3], [8, 8][1, 3], [7, 8].To solve this, we will follow these steps −Make a set called numsin the initializer, set low := -inf and high := infFrom the addNum method that takes num as input, insert num into the ... Read More

Self Crossing in C++

Arnab Chakraborty
Updated on 27-May-2020 06:14:58

290 Views

Suppose we have an array x of n numbers. We start at point (0, 0) and moves x[0] units to the north direction, then x[1] units to the west direction, x[2] units to the south direction , x[3] units to the east direction and so on. In other words, after each move our direction changes counter-clockwise. We have to devise an one-pass algorithm with O(1) extra space to determine whether our path crosses itself, or not.So if the array is like − [3, 4, 2, 5]Answer will be true.To solve this, we will follow these steps −insert 0 at the ... Read More

Patching Array in C++

Arnab Chakraborty
Updated on 27-May-2020 06:12:24

383 Views

Suppose we have an array nums and one number. We can add elements in the array, such that any number in range [1, n] (both are inclusive) can be formed by the sum of some elements in the array. We have to find the minimum number of required patches. So when the array is like [1, 4] and given number is n = 7, then output will be 1, as initially the nums are [1], [4] and [1, 4] = 5, now if we add 2 into array, then the nums will be [1], [2], [4], [1, 2], [1, 4], ... Read More

Remove Duplicate Letters in C++

Arnab Chakraborty
Updated on 27-May-2020 06:10:06

201 Views

Suppose we have a string consisting of only lowercase letters. We have to remove all duplicate letters such that all letters only occur once. And we have to display the result in the smallest lexicographic sequence. So if the input is like “abccb”, then the result will be “abc”To solve this, we will follow these steps −ans := one empty stringDefine one stack stDefine an array onStack of size 26Define one map mn := size of sfor initializing i := 0, when i < n, increase i by 1 do −increase m[s[i]] by 1for initializing i := 0, when i ... Read More

Count of Smaller Numbers After Self in C++

Arnab Chakraborty
Updated on 27-May-2020 06:06:19

404 Views

Suppose we have an array nums, we have to find another array called count, in this count array, the count[i] stores the number of smaller elements to the right of nums[i].So if the input is like: [5, 2, 7, 1], then the result will be [2, 1, 1, 0].To solve this, we will follow these steps −Define one method called update(), this will take index, array bit and nwhile index 0, do −ans = ans + bit[index]index = index – (index AND - index)return ansFrom the main method, do the following −n := size of numsDefine an array res ... Read More

Burst Balloons in C++

Arnab Chakraborty
Updated on 27-May-2020 06:00:28

470 Views

Suppose we have n balloons, these are indexed from 0 to n-1. Here each balloon is painted with a number on it represented by one array called nums. we have to burst all the balloons. If we burst balloon i we will get nums[i – 1] * nums[i] * nums[i + 1] number of coins. After the burst, the i – 1 and i + 1 then becomes adjacent. We have to find the maximum coins to collect by bursting the balloons wisely.So if the input is like [3, 1, 5, 7], then the result will be 148. Initially the ... Read More

Find Median from Data Stream in C++

Arnab Chakraborty
Updated on 27-May-2020 05:49:50

469 Views

Suppose we have a data stream, in that stream some data element may come and join, we have to make one system, that will help to find the median from the data. As we know that the median is the middle data of a sorted list, if it list length is odd, we can get the median directly, otherwise take middle two elements, then find the average. So there will be two methods, addNum() and findMedian(), these two methods will be used to add numbers into the stream, and find the median of all added numbersTo solve this, we will ... Read More

Expression Add Operators in C++

Arnab Chakraborty
Updated on 27-May-2020 05:46:17

321 Views

Suppose we have a string that holds only digits from 0 to 9. And one target value is given. We have to return all possibilities to add binary operators +, - and * into the digits to get the target values. So if the input is like “232” and the target is 8, then the answer will be [“2*3+2”, “2+3*2”]To solve this, we will follow these steps −Define a method called solve(), this will take index, s, curr, target, temp, mult −if idx >= size of s, then, if target is same as curr, then, insert temp at the end ... Read More

Sliding Window Maximum in C++

Arnab Chakraborty
Updated on 27-May-2020 05:36:18

827 Views

Suppose we have an array called nums, there is a sliding window of size k which is moving from the left of the array to the right. We can only see the k numbers in the window. Each time the sliding window moves to the right side by one position. We have to find the max sliding window. So if the input is like −[1, 3, -1, -3, 5, 3, 6, 8] and k is 3, then the window will be like −Window PositionMax13-1-35368313-1-35368313-1-35368313-1-35368513-1-35368613-1-353688To solve this, we will follow these steps −Define an array ansDefine one double ended queue dqif ... Read More

Advertisements