Found 7347 Articles for C++

Constructor Overloading in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:55:35

3K+ Views

As we know function overloading is one of the core feature of the object oriented languages. We can use the same name of the functions; whose parameter sets are different. Here we will see how to overload the constructors of C++ classes. The constructor overloading has few important concepts.Overloaded constructors must have the same name and different number of argumentsThe constructor is called based on the number and types of the arguments are passed.We have to pass the argument while creating objects, otherwise the constructor cannot understand which constructor will be called.Example Live Demo#include using namespace std; class Rect{ ... Read More

Cons of using the whole namespace in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:53:42

235 Views

In C++, we use different namespaces. We can also create our own namespaces. For example, generally, we use standard namespace called std. We write the syntax like:using namespace std;In the standard library, it contains common functionality you use in building your applications like containers, algorithms, etc. If names used by these were out in the open, for example, if they defined a queue class globally, you'd never be able to use the same name again without conflicts. So they created a namespace, std to contain this change.The using namespace statement just means that in the scope it is present, make ... Read More

Find the compatibility difference between two arrays in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:50:39

451 Views

Consider there are two friends and now they want to test their bonding. So they will check, how much compatible they are. Given the numbers n, numbered from 1..n. And they are asked to rank the numbers. They have to find the compatibility difference between them. The compatibility difference is basically the number of mismatches in the relative ranking of the same movie given by them. So if A = [3, 1, 2, 4, 5], and B = [3, 2, 4, 1, 5], then the output will be 2. The compatibility difference is 2, as first ranks movie 1 before ... Read More

Find the closest pair from two sorted arrays in c++

Arnab Chakraborty
Updated on 30-Dec-2019 09:47:51

377 Views

Suppose we have two sorted arrays and a number x, we have to find the pair whose sum is closest to x. And the pair has an element from each array. We have two arrays A1 [0..m-1] and A2 [0..n-1], and another value x. We have to find the pair A1[i] + A2[j] such that absolute value of (A1[i] + A2[j] – x) is minimum. So if A1 = [1, 4, 5, 7], and A2 = [10, 20, 30, 40], and x = 32, then output will be 1 and 30.We will start from left of A1 and right from ... Read More

Maximize value of (arr[i] – i) – (arr[j] – j) in an array in C++

Narendra Kumar
Updated on 24-Dec-2019 07:21:26

155 Views

Problem statementGiven an array, arr[] find the maximum value of (arr[i] – i) – (arr[j] – j) where i is not equal to j. Where i and j vary from 0 to n-1 and n is the size of input array arr[].If the input array is {7, 5, 10, 2, 3} then we can obtain 9 maximum value as follows−(element 10 – index 2) - (element 2 – index 3) (10 – 2) – (2 – 3) = 8 – (-1) = 9Algorithm1. Find maximum value of (arr[i] – i) in whole array. 2. Find minimum value of (arr[i] – ... Read More

Maximize the value of the given expression in C++

Narendra Kumar
Updated on 24-Dec-2019 07:17:59

221 Views

Problem statementGiven three non-zero integers a, b and c. The task is to find the maximum value possible by putting addition and multiplication signs between them in any order.Please note that rearrangement of integers is allowed but addition and multiplication sign must be used once.If a = 1, b = 3 and c = 5 then maximum value will be 20 as follows−(1 + 3) * 5 = 20Algorithm1. If all numbers are positive, then add two small numbers and multiply result with larger one 2. If only two numbers are positive, then multiply 2 positive numbers and add remaining ... Read More

Maximize the sum of arr[i]*i in C++

Narendra Kumar
Updated on 24-Dec-2019 07:15:21

335 Views

Problem statementGiven an array of N integers. You are allowed to rearrange the elements of the array. The task is to find the maximum value of Σarr[i]*i, where i = 0, 1, 2, .. n – 1.If input array = {4, 1, 6, 2} then the maximum sum will be 28 if we rearrange elements in sorted order−{1, 2, 4, 6} = (1 * 0) + (2 * 1) + (4 * 2) + (6 * 3) = 28Algorithm1. Sort array in ascending order 2. Iterate over array and multiply each array element by 1 where i = 0, 1, ... Read More

Maximize the number of segments of length p, q and r in C++

Narendra Kumar
Updated on 24-Dec-2019 07:12:10

122 Views

Problem statementGiven a rod of length L, the task is to cut the rod in such a way that the total number of segments of length p, q and r is maximized. The segments can only be of length p, q, and rIf l = 15, p = 2, q = 3 and r = 5 then we can make 7 segments as follows −{2, 2, 2, 2, 2, 2, 3}AlgorithmWe can solve this problem using dynamic programming1. Initialize dp[] array to 0 2. Iterate till the length of the rod. For every i, a cut of p, q and ... Read More

Maximize the number by rearranging bits in C++

Narendra Kumar
Updated on 24-Dec-2019 07:09:42

83 Views

Problem statementGiven an unsigned number, find the maximum number that could be formed by using the bits of the given unsigned numberIf the input number is 8 then its binary representation is−00000000000000000000000000001000To maximize it set MSB to 1. Then number becomes 2147483648 whose binary representation is−10000000000000000000000000000000Algorithms1. Count number of set bits in the binary representation of a given number 2. Find a number with n least significant set bits 3. shift the number left by (32 – n)Example Live Demo#include using namespace std; unsigned getMaxNumber(unsigned num){    int n = __builtin_popcount(num);    if (n == 32) {       return num;    }    unsigned result = (1

Maximize the median of the given array after adding K elements to the same array in C++

Narendra Kumar
Updated on 24-Dec-2019 07:07:25

180 Views

Problem statementGiven an array arr[] of N elements and an integer K where K < N. The task is to insert K integer elements to the same array such that the median of the resultant array is maximizedIf input array is {1, 3, 2, 5} and k = 3 then −Sorted array becomes {1, 2, 3, 5}Insert 3 elements that are greater than 5. After this operation array becomes {1, 2, 3, 5, 6, 6, 6}Median of the new array is 5Algorithm1. In order to maximize the median of the resultant array, all the elements that need to be inserted ... Read More

Advertisements