Found 7347 Articles for C++

Program to rotate a linked list by k places in C++

Arnab Chakraborty
Updated on 21-Oct-2020 10:34:18

256 Views

Suppose we have a linked list. We have to rotate the list to the right by k places. The value of k will be positive. So if the list is like [1 −> 2 −> 3 −> 4 −> 5 −> NULL], and k = 2, then the output will be [4 −> 5 −> 1 −> 2 −> 3 −> NULL]Let us see the steps −If the list is empty, then return nulllen := 1create one node called tail := headwhile next of tail is not nullincrease len by 1tail := next of tailnext of tail := headk := ... Read More

Program to find maximum profit by cutting the rod of different length in C++

Arnab Chakraborty
Updated on 21-Oct-2020 10:24:57

608 Views

Suppose we have a rod is given of length n. We also have a list, that contains different size and price for each size. We have to find the maximum price by cutting the rod and selling them in the market. To get the best price by making a cut at different positions and comparing the prices after cutting the rod.So, if the input is like prices = [1, 5, 8, 9, 10, 17, 17, 20], n = 8, then the output will be 22, as by cutting the rod in length 2 and 6. The profit is 5 + ... Read More

Program to remove sublist to get same number of elements below and above k in C++

Arnab Chakraborty
Updated on 20-Oct-2020 10:43:04

92 Views

Suppose we have a list of numbers called nums and another number k, we can remove any sublist at most once from the list. We have to find the length of the longest resulting list such that the amount of numbers strictly less than k and strictly larger than k is the same.So, if the input is like nums = [6, 10, 8, 9, 3, 5], k = 6, then the output will be 5, as if we remove the sublist [9] then we will get [6, 10, 8, 3, 5] and there are two numbers [3, 5] which are ... Read More

Program to find minimum number of intervals to be removed to remove overlaps in C++

Arnab Chakraborty
Updated on 20-Oct-2020 10:37:18

384 Views

Suppose we have a set of intervals; we have to find the minimum number of intervals that should be removed to make the rest of the intervals non-overlapping. So if the intervals are [[8, 10], [3, 5], [6, 9]], then the output will be 1, as we have to remove [6, 9] to make all others are non-overlapping.To solve this, we will follow these steps −n := size of arrayif n is 0, then return 0count := 1sort the array based on the end time of the intervalsend := end date of the first intervalfor i in range 1 to ... Read More

Program to evaluate Postfix Notation in C++

Arnab Chakraborty
Updated on 20-Oct-2020 07:41:51

12K+ Views

Suppose we have postfix expression and we have to evaluate the value. Postfix expression is also known as Reverse polish notation. Here we have to use the stack data structure to solve the postfix expressions.So if the expression is “21+3*”, then the answer will be 9.Let us see the steps −for each character ch in the postfix expression, doif ch is an operator $\odot$ , thena := pop first element from stack, b := pop second element from the stackres := b $\odot$ apush res into the stackelse if ch is an operand, thenadd ch into the stackreturn element of ... Read More

Program to count number of perfect squares are added up to form a number in C++

Arnab Chakraborty
Updated on 20-Oct-2020 07:37:58

75 Views

Suppose we have a positive number n, we have to find the least number of perfect square numbers whose sum is same as n. So if the number is 10, then the output is 2, as the numbers are 10 = 9 + 1.To solve this, we will follow these steps −create one table for dynamic programming, of length n + 1, and fill it with infinitydp[0] := 0for i := 1, when i*i dp(n+1,INF);       dp[0] = 0;       for(int i =1;i*i

Program to merge two binary trees in C++

Arnab Chakraborty
Updated on 19-Oct-2020 15:29:27

125 Views

Suppose we have two binary trees and consider that when we put one of them to cover the other, some nodes of the two trees are overlapped while the others are overlapping. We have to merge them into a new binary tree. The merge rule is like that if two nodes are overlapping, then sum node values up as the new value of the merged node. Otherwise, the non-empty node will be used as the node of new tree.So if the trees are −Then the output will be −To solve this, we will follow these steps −The method is solve(). ... Read More

Program to find length of longest common substring in C++

Arnab Chakraborty
Updated on 10-Oct-2020 12:15:42

2K+ Views

Suppose we have two lowercase strings X and Y, we have to find the length of their longest common substring.So, if the input is like X = "helloworld", Y = "worldbook", then the output will be 5, as "world" is the longest common substring and its length is 5.To solve this, we will follow these steps −Define an array longest of size: m+1 x n+1.len := 0for initialize i := 0, when i

Program to find length of longest common subsequence in C++

Arnab Chakraborty
Updated on 10-Oct-2020 12:05:58

189 Views

Suppose we have two strings text1 and text2, we have to find the length of their longest common subsequence. As we know a subsequence of a string is a new string generated from the original string with some characters deleted without changing the relative order of the remaining characters. (So for example "abe" is a subsequence of "abcde" but "adc" is not). A common subsequence of two strings is a subsequence that is common to both strings. So If there is no common subsequence, return 0. If the input is like “abcde”, and “ace”, then the result will be 3.To ... Read More

Program to find length of longest bitonic subsequence in C++

Arnab Chakraborty
Updated on 10-Oct-2020 12:00:43

132 Views

Suppose we have a list of numbers. We have to find length of longest bitonic subsequence. As we knot a sequence is said to be bitonic if it's strictly increasing and then strictly decreasing. A strictly increasing sequence is bitonic. Or a strictly decreasing sequence is bitonic also.So, if the input is like nums = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], size of sequence 16., then the output will be 7.To solve this, we will follow these steps −increasingSubSeq := new array of given array size, and fill with 1for ... Read More

Advertisements