Found 7347 Articles for C++

Program to check whether list can be split into sublists of k increasing elements in C++

Arnab Chakraborty
Updated on 10-Oct-2020 11:31:20

81 Views

Suppose we have a list of numbers called nums, and another number k, we have to check whether the list can be split into lists where each list contains k values and the values are consecutively increasing.So, if the input is like nums = [4, 3, 2, 4, 5, 6], k = 3, then the output will be True, as we can split the list into [2, 3, 4] and [4, 5, 6]To solve this, we will follow these steps −Define one mapfor each key it in mincrease m[it] by 1ok := truewhile (size of m is not 0 and ... Read More

Program to find tree level that has minimum sum in C++

Arnab Chakraborty
Updated on 10-Oct-2020 11:18:23

224 Views

Suppose we have a binary tree, the level of its root is 1, the level of its children is 2, and so on.We have to find the smallest level X such that the sum of all the values of nodes at level X is minimum. So if the tree is like −Output will be 2 as the sum is 4 – 10 = -6, which is minimum.To solve this, we will follow these steps −level := 1, sum := value of r, ansLevel := level, ansSum := sumdefine a queue q, insert given node r into qwhile q is not ... Read More

Program to perform level order traversal of binary tree in C++

Arnab Chakraborty
Updated on 10-Oct-2020 11:10:22

169 Views

Suppose we have a binary tree. We have to traverse this tree using level order traversal fashion. So if the tree is likeThe traversal sequence will be like: [1, 2, 3, 5, 4]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front element from queLet us ... Read More

Program to find the left side view of a binary tree in C++

Arnab Chakraborty
Updated on 10-Oct-2020 10:43:56

69 Views

Suppose we have a binary tree, if we see the tree from left side, then we can see some elements of it. we have to display those elements. So if the tree is like −The output will be [1, 2, 5]To solve this, we will follow these steps −Define an array retDefine a function dfs(), this will take node, c initialize it with 1, if node is null, then −returnif c > lvl, then −lvl := cinsert value of node into retdfs(left of node, c + 1)dfs(right of node, c + 1)From the main method, do the following −lvl := ... Read More

Program to find longest consecutive run of 1 in binary form of a number in C++

Arnab Chakraborty
Updated on 08-Oct-2020 14:56:00

190 Views

Suppose we have a number n, we have to find the length of the longest consecutive run of 1s in its binary representation.So, if the input is like n = 312, then the output will be 3, as 312 is 100111000 in binary and there are 3 consecutive 1s.To solve this, we will follow these steps −ret := 0, len := 0for initialize i := 0, when i < 32, update (increase i by 1), do:if n/2 is odd, then(increase len by 1)Otherwiselen := 0ret := maximum of ret and lenreturn retLet us see the following implementation to get better ... Read More

Program to check whether a tree is height balanced or not in C++

Arnab Chakraborty
Updated on 08-Oct-2020 14:46:10

473 Views

Suppose we have a binary tree; we have to check whether its height is balanced or not. We know that for a height balanced tree, for every node in the tree, the absolute difference of the height of its left subtree and the height of its right subtree is 0 or 1.So, if the input is likethen the output will be TrueTo solve this, we will follow these steps −Define a function dfs(), this will take node, if node is null, then −return 0l := 1 + dfs(left of node)r := 1 + dfs(right of node)if |l - r| > ... Read More

Program to find total mutation group of genes in C++

Arnab Chakraborty
Updated on 08-Oct-2020 14:17:07

235 Views

Suppose we have a list of strings called genes where each element has the same length and each element contains characters "A", "C", "G" and/or "T". Now there are some rules −When two strings s1 and s2 are the same string except for one character, then s1 and s2 are in the same mutation group.When two strings s1 and s2 are in a group and s2 and s3 are in a group, then s1 and s3 are in the same group.We have to find the total number of mutation groups we can generate.So, if the input is like genes = ... Read More

Program to find the maximum number in rotated list in C++

Arnab Chakraborty
Updated on 08-Oct-2020 10:23:53

65 Views

Suppose there is an array, and that is sorted, consider that array is rotated at some pivot, that is unknown to us. So we have to find the maximum from that rotated array. So if the array is like[3, 4, 5, 1, 2], then the output will be 5.To solve this, we will follow these steps −low := 0 and high := last index of array, n := size of array, ans := 0while low arr[mid], then ans := maximum of ans and arr[mid], high := mid – 1else if low = mid, then ans := maximum of ans ... Read More

Program to find the minimum edit distance between two strings in C++

Arnab Chakraborty
Updated on 07-Oct-2020 13:09:22

353 Views

Suppose we have two words S and T, we have to find the minimum number of operations needed to convert from S to T. The operations can be of three types, these areinsert a character, delete a characterreplace a character.So if the input strings are “evaluate” and “fluctuate”, then the result will be 5.To solve this, we will follow these steps −n := size of s, m := size of t, create an array dp of size n + 1for i in range 0 to ndp[i] := new array of size m + 1for j in range 0 to m:dp[i, ... Read More

Maximum sum subsequence with at-least k distant elements in C++

Ayush Gupta
Updated on 09-Sep-2020 13:30:59

126 Views

In this tutorial, we will be discussing a program to find maximum sum subsequence with at-least k distant elements.For this we will be provided with an array containing integers and a value K. Our task is to find the subsequence having maximum sum such that all the elements are at least K elements apart.Example Live Demo#include using namespace std; //finding maximum sum subsequence int maxSum(int arr[], int N, int k) {    int MS[N];    MS[N - 1] = arr[N - 1];    for (int i = N - 2; i >= 0; i--) {       if (i ... Read More

Advertisements