Found 7347 Articles for C++

Maximum number of partitions that can be sorted individually to make sorted in C++

Sunidhi Bansal
Updated on 29-Aug-2020 08:27:47

551 Views

We are given with an array of N numbers with elements lying in range 0 and N-1. The elements are unsorted. The goal is to find the maximum number of partitions of the array which can be sorted individually and then can be concatenated to make a whole sorted array of length N.Each partition is chosen such that elements in it are unsorted. For N numbers ranging between 0 and N-1, sorted elements are at index equal to the value. Arr[i] = i.We will solve this by comparing each element by maximum value found so far on its left. When ... Read More

Find a permutation that causes worst case of Merge Sort in C++

Arnab Chakraborty
Updated on 28-Aug-2020 08:25:54

80 Views

Suppose we have a set of elements; we have to find which permutation of these elements would result in worst case of Merge Sort? As we know asymptotically, merge sort always consumes O (n log n) time, but some cases need more comparisons and consumes more time. Here we have to find a permutation of input elements that will require higher number of comparisons when sorted implementing a typical Merge Sort algorithm.So, if the input is like [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26] , then the output will be [11, ... Read More

Find a pair with given sum in a Balanced BST in C++

Arnab Chakraborty
Updated on 28-Aug-2020 08:20:06

68 Views

Suppose we have a balanced binary search tree and a target sum, we have to define a method that checks whether it is a pair with sum equals to target sum, or not. In this case. We have to keep in mind that the Binary Search Tree is immutable.So, if the input is likethen the output will be (9 + 26 = 35)To solve this, we will follow these steps −Define stacks s1, s2done1 := false, done2 := falseval1 := 0, val2 := 0curr1 := root, curr2 := rootinfinite loop, do −while done1 is false, do −if curr1 is not ... Read More

Construct a linked list from 2D matrix in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:20:57

838 Views

Suppose we have one matrix, we have to convert it to 2d linked list using recursive approach.The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −Define a function make_2d_list(), this will take matrix mat, i, j, m, n, if i and j are not in the matrix boundary, then −return nulltemp := create a new node with value mat[i, j]right of temp := make_2d_list(mat, i, j + 1, m, n)down of temp := make_2d_list(mat, i + 1, j, m, n)return tempExampleLet us see the ... Read More

Construct a linked list from 2D matrix (Iterative Approach) in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:18:10

141 Views

Suppose we have one matrix, we have to convert it to 2d linked list using iterative approach. The list will have the right and down pointer.So, if the input is like102030405060708090then the output will beTo solve this, we will follow these steps −real_head := NULLDefine an array head_arr of size: m.for initialize i := 0, when i < m, update (increase i by 1), do −head_arr[i] := NULLfor initialize j := 0, when j < n, update (increase j by 1), do −p := new tree node with value mat[i, j]if real_head is null, then −real_head := pif head_arr[i] is ... Read More

Variable Length Arrays in C and C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:02:29

560 Views

Here we will discuss about the variable length arrays in C++. Using this we can allocate an auto array of variable size. In C, it supports variable sized arrays from C99 standard. The following format supports this concept −void make_arr(int n){    int array[n]; } int main(){    make_arr(10); }But, in C++ standard (till C++11) there was no concept of variable length array. According to the C++11 standard, array size is mentioned as a constant-expression. So, the above block of code may not be a valid C++11 or below. In C++14 mentions array size as a simple expression (not constant-expression).ExampleLet ... Read More

User Defined Literals in C++

Arnab Chakraborty
Updated on 27-Aug-2020 14:00:41

268 Views

Here we will see the concept of the user-defined literals in C++. From C++ version 11, the User Defined Literals (UDL) are added in C++. C++ also provides literals for a variety of built-in types but these are limited.Built-in Literals −31 (Integer)3.5 (Double)4.2F (Float)'p' (Character)31ULL (Unsigned Long Long)0xD0 (Unsigned Hexadecimal Integer)"pq" (String)Apart from the built-in literals, sometimes we need user defined literals. There are few reasons behind that. Let us see with few examples −Suppose we want to define one weight variable, but we cannot specify the units, like if we define as follows −long double Weight = 3.5;We have ... Read More

Uninitialized primitive data types in C/C++ Program

Arnab Chakraborty
Updated on 27-Aug-2020 13:56:57

197 Views

In this section we will see when we declare one variable that is un-initialized, which value they hold in C or C++ language. Sometimes we assume that the compiler assigns some value like 0 for int, 0.0 for float etc. But what will be for character datatype? Let us see using implementation and compile using different compilers.Example (C++)Let us see the following implementation to get better understanding − Live Demo#include using namespace std; int main() {    char char_var;    float float_var;    int int_var;    double double_var;    long long_var;    cout

Uniform Initialization in C++

Arnab Chakraborty
Updated on 27-Aug-2020 13:55:32

1K+ Views

Here we will discuss about the uniform initialization in C++. This is supported from C++11 version. The uniform initialization is a feature that permits the usage of a consistent syntax to initialize variables and objects which are ranging from primitive type to aggregates. In other words, it introduces brace-initialization that applies braces ({}) to enclose initializer values.Syntaxtype var_name{argument_1, argument_2, .... argument_n}Initialize Dynamically allocated arraysExample (C++)Let us see the following implementation to get better understanding − Live Demo#include using namespace std; int main() {    int* pointer = new int[5]{ 10, 20, 30, 40, 50 };    cout

Traversing a map (or unordered_map) in C++ STL

Arnab Chakraborty
Updated on 27-Aug-2020 13:51:33

2K+ Views

Here we will see the map container and its use in C++. The maps are defined as associative containers that store elements in a hash-mapped fashion. Each element is associated with a key and value. No two mapped values can have identical keys. These are some fundamental methods that are present inside the map container in C++.begin(): This returns an iterator to the first element in the map.end()− This returns an iterator to the theoretical element that follows last element in the map.size() − This returns the number of elements in the map.max_size() − This returns the maximum number of ... Read More

Advertisements