Found 7197 Articles for C++

Maximum consecutive numbers present in an array in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:01:47

2K+ Views

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 More

Maximum array sum that can be obtained after exactly k changes in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:59:01

687 Views

We are given with an array of positive and negative integers and a number K. The task is to find the maximum sum of the array after K changes in it’s elements. The single change operation here multiplies the single element by -1.Approach used will be to convert every negative number to positive. If there are N negative numbers then, for this we will sort the array −If NK then change the sign of K negative numbers and add the array. Sum will be maximum.InputArr[]= { 0, -2, 6, 4, 8, 2, -3 } K=4OutputMaximum array sum is : 25Explanation ... Read More

Maximum and minimum sums from two numbers with digit replacements in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:57:06

346 Views

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 More

Maximum and Minimum Product Subsets in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:54:51

383 Views

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 More

Count Balanced Binary Trees of Height h in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:52:45

396 Views

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 More

Sets of pairs in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:50:15

8K+ Views

Set in C++ is an associative container and contains unique elements. All the elements once added to a specific cannot be modified. One can only remove and add elements in order to change them.Pair is defined under header and is used to couple together two pair values. The pair can have values of different or same type. The class has member functions first() and second() to individually access the values in a pair.The order of pair elements is fixed (first, second). We can use pair to combine two heterogeneous values of different types.To access any element we use variable_name.first ... Read More

set operator= in C++ STL

Sunidhi Bansal
Updated on 28-Jul-2020 10:47:26

272 Views

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 More

C++ Rule Of Three.

Akansha Kumari
Updated on 15-Jul-2025 17:12:43

887 Views

The rule of three in C++ states that, if a class in C++ has any one (or more) of the following, then it should define all three. Destructor Copy Constructor Copy Assignment Constructor These three are special member functions of class and are responsible for managing resources such as dynamic memory, file handles, sockets, etc. And if one of them is defined explicitly, means that class is managing those resources manually (like memory using new/delete), and if we fail to define others then it can lead to ... Read More

round() in C++.

Sunidhi Bansal
Updated on 28-Jul-2020 10:42:58

16K+ Views

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 More

Rotation of a point about another point in C++

Sunidhi Bansal
Updated on 28-Jul-2020 10:41:29

1K+ Views

Rotation of a point X about origin is by an angle θ in anti-clockwise direction is done by − X by θ about origin anti-clRotateockwise: X*polar( 1.0, θ ).Here, the function polar for complex numbers is defined under header file and is used to find a complex number using phase angle and magnitude. polar(mag, angle) returns a complex number.Rotation of point X about a point YTo rotate a point about another point, we will use translation in which movement of all coordinates occur in a particular direction.Steps to rotate X about Y.Translate X to Y, so Y becomes the new ... Read More

Advertisements