Found 7197 Articles for C++

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

117 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

206 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

246 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

222 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

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

143 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

287 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

261 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

110 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

JavaScript: replacing object keys with an array

Disha Verma
Updated on 07-Mar-2025 12:49:32

2K+ Views

Replacing object keys with an array is a common task every developer does while working with data transformations. An object is a data structure that stores data in the form of key-value pairs, and an array stores values of the same data type in a linear form. Sometimes, developers need to use object keys with an array. This article explains how to efficiently replace object keys using arrays in JavaScript. We are required to write a JavaScript function that takes in an object and an array of literals. The length of the array and the number of keys in the ... Read More

Advertisements