Found 7197 Articles for C++

N-ary Tree Preorder Traversal in C++

Arnab Chakraborty
Updated on 11-Jun-2020 11:54:51

641 Views

Suppose we have one n-ary tree, we have to find the preorder traversal of its nodes.So, if the input is likethen the output will be [1,3,5,6,2,4]To solve this, we will follow these steps −Define an array ansDefine a method called preorder(), this will take rootif root is null, then −return empty listinsert value of root at the end of ansfor all child i in children array of rootpreorder(i)return ansExample Let us see the following implementation to get a better understanding − Live Demo#include using namespace std; void print_vector(vector v){    cout

Distribute Candies in C++

Arnab Chakraborty
Updated on 11-Jun-2020 11:53:30

293 Views

Suppose we have an array with even length, here different numbers in this array will represent different kinds of candies. Now each number means one candy of the corresponding kind. we have to distribute candies equally in number to brother and sister. We have to find the maximum number of kinds of candies the sister could receive.So, if the input is like [1, 1, 2, 3], then the output will be 2 as if we consider the sister has candies [2, 3] and the brother has candies [1, 1]. Now the sister has two different kinds of candies, the brother ... Read More

Reshape the Matrix in C++

Arnab Chakraborty
Updated on 11-Jun-2020 11:52:00

2K+ Views

In different platform there is very useful function called 'reshape', that function is used to reshape a matrix into a new one with different size but data will be same. So, if we have a matrix and two values r and c for the row number and column number of the wanted reshaped matrix, respectively.So, if the input is like [[5, 10], [15, 20]], row = 1 and col = 4, then the output will be [[5, 10, 15, 20]]To solve this, we will follow these steps−Define an array tempDefine one 2D array res of size (r x c)count := ... Read More

Convert BST to Greater Tree in C++

Arnab Chakraborty
Updated on 10-Jun-2020 13:09:58

171 Views

Suppose we have a Binary Search Tree, we have to convert it into a Greater Tree such that every key of the original BST is changed to the original key + sum of all keys greater than the original key in BST.So, if the input is likethen the output will beTo solve this, we will follow these steps −Define a function revInorder(), this will take tree root and s, if root is null, then −returnrevInorder(right of root, s)s := s + val of rootval of root := srevInorder(left of root, s)From the main method, do the following −if root is ... Read More

K-diff Pairs in an Array in C++

Arnab Chakraborty
Updated on 10-Jun-2020 13:06:16

353 Views

Suppose we have an array and an integer k, we have to find the number of unique k-diff pairs in the array. Here the k-diff pair is like (i, j), where i and j are both are present in the array and their absolute difference is k.So, if the input is like [3, 1, 4, 1, 5], k = 2, then the output will be 2, as there are two 2-diff pairs in the array-like (1, 3) and (3, 5).To solve this, we will follow these steps −Define maps called seen and doneDefine one set sif k < 0, then ... Read More

Longest Uncommon Subsequence I in C++

Arnab Chakraborty
Updated on 10-Jun-2020 13:04:06

212 Views

Suppose we have two strings; we have to find the longest uncommon subsequence of these two strings. The longest uncommon subsequence is actually the longest subsequence of one string and this subsequence should not come in the other string. So, we have to find the length of the longest uncommon subsequence. If the longest uncommon subsequence doesn't exist, return -1.So, if the input is like "aabbac", "aabbcc", then the output will be 6To solve this, we will follow these steps −if a is same as b, then −return -1Otherwisereturn maximum of size of a and size of bExample Let us see ... Read More

Perfect Number in C++

Arnab Chakraborty
Updated on 10-Jun-2020 13:02:54

2K+ Views

Suppose we have to check whether a given number is perfect number or not. A number is said to be a Perfect Number when that is equal to the sum of all its positive divisors except itself. The number n will be in range 1^8.So, if the input is like 28, then the output will be True, as its sum of divisors − 1 + 2 + 4 + 7+ 14 = 28.To solve this, we will follow these steps −As the numbers are in range 10^8, there are only few perfect numbers, if the given input is in that ... Read More

Relative Ranks in C++

Arnab Chakraborty
Updated on 10-Jun-2020 13:01:32

211 Views

Suppose we have a list of scores of N athletes, we have to find their relative ranks and the people with the top three highest scores, who will be different medals: "Gold", "Silver" and "Bronze".So, if the input is like [2, 5, 3, 1, 0], then the output will be [Bronze, Gold, Silver, 4, 5]To solve this, we will follow these steps −if size of nums is same as 1, then −return "Gold"if size of nums is same as 2, then −if nums[0] > nums[1], then −return "Gold", "Silver"Otherwisereturn "Silver", "Gold"Define an array vDDefine an array vecfor initialize i := ... Read More

Keyboard Row in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:59:16

272 Views

Given a list of words, we have to find those words that can be typed using letters of the alphabet on only one row's of standard keyboard layout.So, if the input is like ["hello", "world", "mom", "dad", "try", "type", "tom"], then the output will be ["dad", "try", "type"]To solve this, we will follow these steps −Define an array outputoneRow := trueDefine one map charToRowMap, this will take all pairs such that {letter, line}, the letter is the letter present on the keyboard, and line is the line number on the keyboard.for each word in words array −if the word is ... Read More

Construct the Rectangle in C++

Arnab Chakraborty
Updated on 10-Jun-2020 12:56:33

339 Views

Suppose we have a specific rectangular web page area, our job is to design a rectangular web page, whose length L and width W that satisfies the following requirements −The area of the web page must equal to the given target area.The width W should not be larger than the length L, and L >= W.The difference between L and W should be as small as possible.So, if the input is like 4, then the output will be [2, 2], as the target area is 4, and all the possible ways to construct it are [1, 4], [2, 2], [4, ... Read More

Advertisements