Found 26504 Articles for Server Side Programming

Set Mismatch in C++

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

276 Views

Suppose there is a set S that is originally containing numbers from 1 to n. But unfortunately, due to some error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.Now if we have an array called nums that is representing the data status of this set after the error. Our task is to find the number occurs twice and then find the number that is missed. return the results in the form of an array.So, if the input is like [1, 2, ... Read More

Maximum Average Subarray I in C++

Arnab Chakraborty
Updated on 11-Jun-2020 12:10:35

186 Views

Suppose we have an array with n elements, we have to find the contiguous subarray of given length k that has the maximum average value. We have to return the maximum average value.So, if the input is like [1, 13, -5, -8, 48, 3] and k = 4, then the output will be 12.0, as (13-5-8+48)/4 = 12.0.To solve this, we will follow these steps −sum := 0for initialize i := 0, when i < k, update (increase i by 1), do −sum := sum + nums[i]maxi := sumfor initialize i := k, when i < size of nums, update ... Read More

Average of Levels in Binary Tree in C++

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

204 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

801 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

194 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

219 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

511 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

642 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

Advertisements