Found 7347 Articles for C++

Maximum element in min heap in C++

Narendra Kumar
Updated on 31-Dec-2019 11:48:37

279 Views

Problem statementGiven a minimum heap find maximum element in that.ExampleIf input heap is −Then maximum element is 55AlgorithmIn minimum heap parent node will be lesser than its children. Hence we can conclude that a non-leaf node cannot be the maximum.Search maximum element in the leaf nodesExampleLet us now see an example − Live Demo#include using namespace std; int getMaxElement(int *heap, int n) {    int maxVal = heap[n / 2];    for (int i = n / 2 + 1; i < n; ++i) {       maxVal = max(maxVal, heap[i]);    }    return maxVal; } int main() {    int heap[] = {15, 27, 22, 35, 29, 55, 48}; int n = sizeof(heap) / sizeof(heap[0]);    cout

Maximizing Unique Pairs from two arrays in C++

Narendra Kumar
Updated on 31-Dec-2019 11:45:55

259 Views

Problem statementGiven two arrays of equal size N, form maximum number of pairs by using their elements, one from the first array and second from the second array, such that an element from each array is used at-most once and the absolute difference between the selected elements used for forming a pair is less than or equal to a given element K.ExampleIf input is −arr1[] = {3, 4, 5, 2, 1}arr2[] = {6, 5, 4, 7, 15}and k = 3 then we can form following 4 pairs whose absolute difference is less than or equal to 3 −(1, 4), (2, ... Read More

Maximizing the elements with a[i+1] > a[i] in C++

Narendra Kumar
Updated on 31-Dec-2019 11:41:55

220 Views

Problem statementGiven an array of N integers, rearrange the array elements such that the next array element is greater than the previous element arr[i+1] > arr[i]ExampleIf input array is {300, 400, 400, 300} then rearranged array will be −{300, 400, 300, 400}. In this solution we get 2 indices with condition arr[i+1] > arr[i]. Hence answer is 2.AlgorithmIf all elements are distinct, then answer is simply n-1 where n is the number of elements in the arrayIf there are repeating elements, then answer is n – maxFrequencyExampleLet us now see an example − Live Demo#include #define MAX 1000 using namespace ... Read More

Maximizing array sum with given operation in C++

Narendra Kumar
Updated on 31-Dec-2019 11:38:48

112 Views

DescriptionThere is an array of (2 * n – 1) integers. We can change sign of exactly n elements in the array. In other words, we can select exactly n array elements, and multiply each of them by -1. Find the maximum sum of the array.ExampleIf input array is {-2, 100, -3} then we can obtain maximum changing sign of -2 and -3. After changing sign array becomes −{2, 100, 3} and maximum sum of this array is 105.AlgorithmCount negative numbersCalculate the sum of the array by taking absolute values of the numbers.Find the minimum number of the array by ... Read More

Construct an array from XOR of all elements of array except element at same index in C++

Arnab Chakraborty
Updated on 30-Dec-2019 10:12:25

162 Views

Suppose we have an array A[] with n positive elements. We have to create another array B, such that B[i] is XOR of all elements of A[] except A[i]. So if the A = [2, 1, 5, 9], then B = [13, 14, 10, 6]To solve this, at first we have to find the XOR of all elements of A, and store it into variable x, then for each element of A[i], find B[i] = x XOR A[i]Example Live Demo#include using namespace std; void findXOR(int A[], int n) {    int x = 0;    for (int i = 0; ... Read More

Construct an array from GCDs of consecutive elements in given array in C++

Arnab Chakraborty
Updated on 30-Dec-2019 10:07:54

143 Views

Suppose we have an array A[], with n elements. We have to find another array B[], whose size is n+1, such that GCD of B[i] and B[i + 1] is A[i]. If there are multiple solutions, then print one of them whose array sum is minimum. So if A = [1, 2, 3], then output will be [1, 2, 6, 3]When A has only one element say K, then B = [K, K]. So the B[0] will be A[0]. Now consider we are done up to index i, so we have already processed index i, and calculated B[i + 1]. ... Read More

upper_bound in C++

Arnab Chakraborty
Updated on 30-Dec-2019 10:02:52

96 Views

Here we will see that is the upper_bound() function in C++ STL. This function returns an iterator that points to the first element in the container, which is considered to go after val. The syntax is like:iterator upper_bound (const value_type& val); const_iterator upper_bound (const value_type& val) const;The return value is an iterator, pointing to the first element in the container which is considered to go after val.Example Live Demo#include #include using namespace std; int main () {    set myset;    set::iterator itlow, itup;    for (int i = 1; i < 10; i++) myset.insert(i*10);    itup = myset.upper_bound ... Read More

Shuffle an Array using STL in C++

Arnab Chakraborty
Updated on 30-Dec-2019 10:00:31

604 Views

Here we will see the Shuffle and random_shuffle in C++. These functions are used to shuffle array elements in C++. We can use the vector also instead of arrays, the usage is similar. Let us see the random_shuffle() first. It is used to randomly rearrange the elements in range [left, right). This function randomly swaps the positions of each element with the position of some randomly chosen positions.We can provide some random generator function to tell which element will be taken in every case. If we do not provide some, it will use its own random generator function.Example Live Demo#include ... Read More

Setting up C++ Development Environment

Arnab Chakraborty
Updated on 30-Dec-2019 09:58:46

109 Views

Text EditorThis will be used to type your program. Examples of few editors include Windows Notepad, OS Edit command, Brief, Epsilon, EMACS, and vim or vi.Name and version of text editor can vary on different operating systems. For example, Notepad will be used on Windows and vim or vi can be used on windows as well as Linux, or UNIX.The files you create with your editor are called source files and for C++ they typically are named with the extension .cpp, .cp, or .c.A text editor should be in place to start your C++ programming.C++ CompilerThis is an actual C++ ... Read More

getline (string) in C++

Arnab Chakraborty
Updated on 30-Dec-2019 09:58:05

689 Views

It is used to extracts characters from the stream as unformatted input and stores them into s as a c-string, until either the extracted character is the delimiting character, or n characters have been written to s (including the terminating null character). The declaration is like:basic_istream& getline (char_type* s, streamsize n ); basic_istream& getline (char_type* s, streamsize n, char_type delim);The parameters are ‘s’ pointer to an array of characters, where the extracted characters are stored as a c_string. Next parameter is ‘n’ this is the maximum number of characters to write (including the terminating character). The third parameter is ‘delim’ ... Read More

Advertisements