Server Side Programming Articles - Page 1743 of 2650

Average of Levels in Binary Tree in C++

Arnab Chakraborty
Updated on 11-Jun-2020 12:09:13

217 Views

Suppose we have a non-empty binary tree; we have to find the average value of the nodes on each level in the return the average values as an array.So, if the input is likethen the output will be [3, 14.5, 11].To solve this, we will follow these steps −Define an array resultDefine one queue qinsert root into qwhile (not q is empty), do −n := size of qDefine an array tempwhile n is non-zero, do −t := first element of qinsert value of t into tempdelete element from qif left of t is not null, then −insert left of t ... Read More

Sum of Square Numbers in C++

Arnab Chakraborty
Updated on 11-Jun-2020 12:05:53

1K+ Views

Suppose we have a non-negative integer c, we have to decide whether there're two integers a and b such that it satisfies a^2 + b^2 = c.So, if the input is like 61, then the output will be True, as 61 = 5^2 + 6^2.To solve this, we will follow these steps −Define a function isPerfect(), this will take x, sr := square root of xreturn true when (sr - floor of sr) is 0From the main method do the following, if c is same as 0, then −return truefor initialize i := 0, when i < the ceiling of ... Read More

Maximum Product of Three Numbers in C++

Arnab Chakraborty
Updated on 11-Jun-2020 12:04:32

807 Views

Suppose we have an integer array; we have to find three numbers whose product is maximum then return the maximum product.So, if the input is like [1, 1, 2, 3, 3], then the output will be 18, as the three elements are [2, 3, 3].To solve this, we will follow these steps −sort the array numsl := size of numsa := nums[l - 1], b := nums[l - 2], c := nums[l - 3], d := nums[0], e := nums[1]return maximum of a * b * c and d * e * aExample Let us see the following implementation to get ... Read More

Can Place Flowers in C++

Arnab Chakraborty
Updated on 11-Jun-2020 12:03:16

1K+ Views

Suppose we have a long flowerbed in which some of the plots are planted and some are empty. Now there is a constraint, flowers cannot be planted in adjacent plots, they would compete for water and both would die. So if we have a flowerbed, represented by an array containing 0 and 1, 0 indicates empty and 1 indicates fill, and a number n is also given, we have to check whether n new flowers can be planted in it without violating the no-adjacent-flowers rule or not.So, if the input is like flowerbed = [1, 0, 0, 0, 1], n ... Read More

Minimum Index Sum of Two Lists in C++

Arnab Chakraborty
Updated on 11-Jun-2020 12:00:29

202 Views

Suppose there are two fiends Amal and Bimal want to choose a restaurant for dinner, now they both have a list of favorite restaurants represented by strings. We have to help them to find out their common interest with the least list index sum. If there is a choice tie between different answers, then return all of them with no order requirement.So, if the input is like ["ABC", "PQR", "MNO", "XYZ"], and ["TUV", "GHI", "KLM", "ABC"], then the output will be ["ABC"]To solve this, we will follow these steps −Define one map mpleast := inffor initialize i := 0, when ... Read More

Range Addition II in C++

Arnab Chakraborty
Updated on 11-Jun-2020 11:59:08

227 Views

Suppose we have one m * n matrix called M and this is initialized with all 0's and we also have several update operations. Now, operations are represented by a 2D array, and each operation is represented by an array with two positive integers x and y, it means M[i][j] should be added by one for all values i in range 0 to a - 1 and all values j in range 0 to b - 1. We have to find the count of the number of maximum integer in the matrix after performing all the operations.So, if the input ... Read More

Longest Harmonious Subsequence in C++

Arnab Chakraborty
Updated on 11-Jun-2020 11:56:24

522 Views

Suppose we have an integer array; we have to find the length of its longest harmonious subsequence among all its possible subsequences. As we know a harmonious sequence array is an array where the difference between its maximum value and its minimum value is exactly 1.So, if the input is like [1, 3, 2, 2, 5, 2, 3, 7], then the output will be 5, as the longest harmonious subsequence is [4, 3, 3, 3, 4].To solve this, we will follow these steps −Define one map mfor n in nums −(increase m[n] by 1)for key-value pair (k, v) in m ... Read More

N-ary Tree Preorder Traversal in C++

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

654 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

309 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

Advertisements