Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Articles by Sunidhi Bansal
Page 37 of 81
round() in C++.
The round() function in C++ is used to round off the double, float or long double value passed to it as a parameter to the nearest integral value. The header file used to use the round() function in a c++ program is or .Following are the overloaded versions of round() after C++ 11 standarddouble round( double D )float round( float F )long double round( long double LD )double round ( T var )Note − The value returned is the nearest integer represented as floating point, i.e for 2.3 nearest value returned will be 2.0 and not 2.Following program is ...
Read Moreset operator= in C++ STL
The function operator= is used in sets to copy one set (or move to another in C++ STL. It behaves as a normal ‘=’ assignment operation for sets. There are there overloaded forms of this function −copy :- set& operator= (const set& s1) −This function copies all the elements in set s1 to another set. The parameter passed is set of the same type.Usage − set s1=s2;move :- set& operator=( set &&s1 ) −This moves the elements of set s1 into the calling set.Initializer list :- set& operator= (initializer_list ilist) −This version copies the values of the initializer list ilist ...
Read MoreCount Balanced Binary Trees of Height h in C++
We are given with the height H of a binary tree. The goal is to find the number/count of balanced Binary Trees of given height.A binary tree − is a tree data structure in which each node has at most two children, which are the left child and the right child.Height-balanced binary tree − is defined as a binary tree in which the depth of the two subtrees of every node differ by 1 or 0 only. That is the height of the left subtree and the right subtree at every node has a maximum difference of 1.Following figure contains ...
Read MoreMaximum and Minimum Product Subsets in C++
We are given with an array of integers of size N. The goal here is to find the Maximum and Minimum Product Subsets. We will do this by taking two product variables, one for minimum product found so far minProd and other for maximum product found so far, maxProd.While traversing the array we will multiply each element with both minProd and maxProd. Also keep a check on previous maximum product, previous minimum product, current maximum product, current minimum product and the current element itself.InputArr[]= { 1, 2, 5, 0, 2 }OutputMaximum Product: 20 Minimum Product: 0Explanation − Starting from the ...
Read MoreMaximum and minimum sums from two numbers with digit replacements in C++
We are given with two positive numbers num1 and num2. The goal is to find the minimum sum and maximum sum possible of these two after a digit replacement in both of them. We are allowed to replace digits from each of numbers in both of numbers. Suppose num1 is 434 and num2 is 324 and we can replace digit 3 with 4 and digic 4 with 3. Then minimum sum will be − 333+323=656 and maximum sum will be 444+424=864.Let us understand with examples for digit replacement 3 with 4 and vice versa −Inputnum1=3224 num2=4321OutputMaximum sum is : 8645 ...
Read MoreMaximum consecutive numbers present in an array in C++
We are given with an array of positive integers. The goal is to find the maximum number of consecutive numbers present in it. First of all we will sort the array and then compare adjacent elements arr[j]==arr[i]+1 (j=i+1), if difference is 1 then increment count and indexes i++, j++ else change count=1. Store the maximum count found so far stored in maxc.InputArr[]= { 100, 21, 24, 73, 22, 23 }OutputMaximum consecutive numbers in array : 4Explanation − Sorted array is − { 21, 22, 23, 24, 73, 100 } initialize count=1, maxcount=11. 22=21+1 count=2 maxcount=2 i++, j++ 2. 23=22+2 count=3 ...
Read MoreMaximum count of equal numbers in an array after performing given operations in C++
We are given with an array of integers. The goal is to find the maximum numbers in array that are equal after performing given operations −Select two elements a[i] and a[j] such that i != j andIncrement a[i] and decrement a[j] ( a[i]++, a[j]-- )We will take the sum of the array and divide it by the number of elements. If N is size of array thenIf sum is divisible by N then equal numbers will also be N otherwise Equal numbers will be N-1.InputArr[]= { 1, 2, 3 }OutputMaximum count of equal numbers : 3Explanation − After first step ...
Read MoreMaximum count of pairs which generate the same sum in C++
We are given with an array of integers. The goal is to find the maximum number of pairs in the array that when added produce the same sum. We have to find the maximum count of such pairs.InputArr[]= { 1, 2, 3, 4, 2 }OutputMaximum count of pairs with same sum : 3Explanation − Sum of pairs of numbers −{1, 2}, {1, 2} Sum:3 {1, 3}, {2, 2} Sum:4 {1, 4}, {2, 3}, {3, 2} Sum:5 {2, 4} Sum:6 {3, 4} Sum:7 Maximum count of pairs with same sum is 3 ( for sum = 5 )InputArr[]= { 5, 3, ...
Read MoreCount elements such that there are exactly X elements with values greater than or equal to X in C++
We are given with an array of integers. The goal is to find the count of elements in the array that satisfy the following condition −For each element the count of numbers greater than or equal to it present in the array should be exactly equal to it. Excluding the element itself. If element is X then array has exactly X numbers which are greater or equal to X. (Excluding the element).InputArr[]= { 0, 1, 2, 3, 4, 9, 8 }OutputElements exactly greater than equal to itself : 1Explanation − Elements and numbers >= to it −Arr[0]: 6 elements are ...
Read MoreCount even length binary sequences with same sum of first and second half bits in C++
We are given several bits n as input for a binary sequence. The goal here is to find the binary sequence of length 2n such that the sum of its first and second half bits is equal. First n bits and next n bits have the same sum.We have a binary sequence so the only choice to put digits at any place is 0 and 1. For n bits at first and second half, no. of possible combinations are −n bits with all zeros (0 1’s) nC0= 1n bits with 1 1’s nC1n bits with 2 1’s nC2..n bits with ...
Read More