Found 33676 Articles for Programming

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

Distribute Candies in C++

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

296 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

PHP asin() Function

Ranjit Kumar
Updated on 29-Jun-2020 08:20:15

249 Views

Definition and UsageThe asin() function returns the arc sine or sine inverse of arg in radians. asin() is the inverse function of sin(). Therefore if sin(x)=y, asin(y)=x.For example, sin(pi/2)=1 and asin(1)=1.57079633 rad which is equal to pi/2.This function returns a float value.Syntaxasin ( float $arg ) : floatParametersSr.NoParameter & Description1argA floating point number whose arc sine is to be calculated. Number should be withing -1 to 1Return ValuesPHP asin() function returns arc sine of given number. It is angle represented in radians.PHP VersionThis function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.Example Live DemoFollowing example calculates ... Read More

PHP acosh() Function

Malhar Lathkar
Updated on 11-Jun-2020 09:51:30

265 Views

Definition and UsageThe acosh() function returns the inverse hyperbolic cosine ratio of given angle in of given parameter. In other words, the return value of asinh() is hyperbolic sine of given parameter. A hyperbolic inverse hyperbolic cosine function is defined as −.acosh(x) = log(x+sqrt(pow(x, 2)-1))This function returns a float value.Syntaxacosh ( float $arg ) : floatParametersSr.NoParameter & Description1argA floating point value whose inverse hyperbolic cosine is to be calculatedReturn ValuesPHP acosh() function returns inverse hyperbolic cosine ratio of given parameter.PHP VersionThis function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.ExampleFollowing example calculates acosh(pi/2) and returns ... Read More

PHP acos() Function

Malhar Lathkar
Updated on 11-Jun-2020 08:36:23

295 Views

Definition and UsageThe acos() function Returns the arc cosine or cos inverse of arg in radians. acos() is the inverse function of cos(). Therefore if cos(x)=y, acos(y)=x.For example, cos(pi/2)=0 and acos(0)=1.57079633 rad which is equal to pi/2.This function returns a float value.Syntaxacos ( float $arg ) : floatParametersSr.NoParameter & Description1argA floating point number whose arc cosine is to be calculated. Number should be withing -1 to 1Return ValuesPHP acos() function returns arc cosine of given number. It is angle represented in radians.PHP VersionThis function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.Example Live DemoFollowing example calculates ... Read More

Advertisements