Found 7347 Articles for C++

Find time taken for signal to reach all positions in a string - C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:30:07

81 Views

In this tutorial, we are going to write a program that computes the time taken for a signal to reach all positions in a string. Let me explain it with an example.We will have a string that contains only s and p characters. s is a signal and p is a position in the string. A signal starts at s and travels in both left and right directions. We are assuming it takes one unit of time to travel to the next position in the string. Our task is to compute the time needed to convert all positions into signals.Let's ... Read More

Find three integers less than or equal to N such that their LCM is maximum - C++

Hafeezul Kareem
Updated on 29-Dec-2020 10:27:54

344 Views

In this tutorial, we are going to write a program that is based on the concepts of LCM. As the title says, we have to find three numbers that are less than or equal to the given number whose LCM is maximum.Let's see an example.Before diving into the problem let's see what LCM is and write program for it.LCM is the least common multiple of a number. It is also known as Least common divisor. For two positive number a and b, the LCM is the smallest integer that is evenly divisible by both a and b.If the given integers ... Read More

Program to Find Out Median of an Integer Array in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:55:00

186 Views

Suppose we have to implement a class named MedianClass which contains the following methods −add(value) which adds a value to the data structure.median() finds the median of all the numbers currently present in the data structure.So, if we add 5, 3, 8 and find median, the output will be 5.0, then if we add 9 and find the median, the output will be 6.5.To solve this, we will follow these steps −Define priority queue left and rightDefine addNum method, this will take the number as input −if left is empty or num < top element of left, then, insert num ... Read More

Program to Find Out Integers Less than n Containing Multiple Similar Digits in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:29:16

129 Views

Suppose we have an integer n, we have to find the number of positive integers that are less than or equal to n, where the integer numbers at least have a digit occurring more than once.So, if the input is like n = 200, then the output will be 38To solve this, we will follow these steps −Define an array afor initialize x := n, when x is non−zero, update x := x / 10, do −insert x mod 10 at the end of areverse the array aret := nfor initialize w := 1, d := 1, when w < ... Read More

Program to construct Maximum Stack with given operations in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:11:10

258 Views

Suppose we want to make a maximum stack, which supports following operations −MaxStk() this will construct a new instance of a maximum stackpush(val) inserts val to the stacktop() get the top most element from stackmax() get the maximum element from the stackpop() removes and returns the top most element from the stackpopmax() removes and returns the maximum element from the stackNow construct the maximum stack by calling MasStk(), then push three values like 5, 15, 10, then call top(), max(), popmax(), max() pop(), top() functions respectively. then the initial stack status will be [5, 15, 10], and corresponding output for ... Read More

Program to find maximum adjacent absolute value sum after single reversal in C++

Arnab Chakraborty
Updated on 26-Dec-2020 11:07:28

104 Views

Suppose we have a list of numbers called nums and we can reverse any sublist in the list at most once. After performing this operation, we have to find the maximum possible value of$\displaystyle\sum\limits_{i=0}^{n-2}| nums[i+1]-[nums[i]|$So, if the input is like nums = [2, 4, 6], then the output will be 6, as when we reverse [4, 6] we will get the list as [2, 6, 4] and the value |2 − 6| + |6 − 4| = 6To solve this, we will follow these steps −if size of nums

Program to construct Frequency Stack in C++

Arnab Chakraborty
Updated on 26-Dec-2020 10:56:34

100 Views

Suppose we want to construct one stack called FrequencyStack, Our FrequencyStack has two functions −append(x), This will append or push a value x onto the stack.pop(), This will remove and returns the most frequent element in the stack. If there are more than one elements with the same frequency, then the element closest to the top of the stack is removed and returned.So, if the input is like append some elements like 7, 9, 7, 9, 6, 7, then perform the pop operations four times, then the output will be 7, 9, 7, 6 respectively.To solve this, we will follow ... Read More

Program to find final amount that should be paid to employees based on their performance in C++

Arnab Chakraborty
Updated on 26-Dec-2020 10:54:40

149 Views

Suppose we have two lists of numbers of same length called performance and costs. And we also have another number k. These indicates that each worker i performs at performance[i] level and it takes costs at least costs[i]. We have to find the minimum cost to hire k employees given also that the workers will be paid proportionate to their performance compared to other employees in the group.So, if the input is like performance = [5, 3, 2] costs = [100, 5, 4] k = 2, then the output will be 10, as we can select emp1 and emp2. They ... Read More

Program to count number of unique subsequences of a string in C++

Arnab Chakraborty
Updated on 26-Dec-2020 10:54:01

532 Views

Suppose we have a string s, we have to find the number of non-empty unique subsequences of s. If the answer is very large then mod the result by 10^9 + 7.So, if the input is like s = "xxy", then the output will be 5, as there are five subsequences: "x", "xx", "xy", "y" and "xxy".To solve this, we will follow these steps −m := 10^9 + 7n := size of sDefine an array table of size 26res := 0for initialize i := 1, when i

Program to find number of unique subsequences same as target in C++

Arnab Chakraborty
Updated on 25-Dec-2020 06:03:39

69 Views

Suppose we have two lowercase strings s and t, we have to find the number of subsequences of s that are equal to t. If the answer is very large then return result by 10^9 + 7.So, if the input is like s = "abbd" t = "bd", then the output will be 2, as there are two possible subsequences "bd".s[1] concatenate s[3]s[2] concatenate s[3].To solve this, we will follow these steps −m := 10^9 + 7if size of t is same as 0, then −return 0if t is same as s, then −return 1if size of t > size ... Read More

Advertisements