Construct Linked List from 2D Matrix in C++

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

998 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 Linked List from 2D Matrix in C++ - Iterative Approach

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

216 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

Construct Distinct Elements Array with Given Size, Sum and Upper Bound in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:14:11

180 Views

Suppose we have one size variable N, we also have one variable SUM this is the total sum of all elements available in the array and another variable K such that there is no element in array is greater than K, We have to find one orthogonal array where all elements in the array are distinct. If there is no solution return -1.So, if the input is like N = 4, SUM = 16 K = 9, then the output will be [1, 2, 4, 9]To solve this, we will follow these steps −minimum_sum := (N *(N + 1)) / ... Read More

Construct DataFrame in Pandas Using String Data in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:11:58

648 Views

Here we will see how we can construct a pandas dataframe using string type data. Pandas supports csv files, but we can do the same using string also. For string type data, we have to use one wrapper, that helps to simulate as the data is taken as csv reader.Here we are using a string that takes data and separated by semicolon.ExampleLet us see the following implementation to get better understanding −import pandas as pd from io import StringIO str_data = StringIO("""Id;Subject;Course_Fee    10;DBMS;3000    11;Basic Maths;2000    12;Data Science;40000    13;Algorithm;5000    """) df = pd.read_csv(str_data, sep =";") print(df)OutputId ... Read More

Construct BST from Given Postorder Traversal Using Stack in Python

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

256 Views

Suppose we have one postorder traversal of a binary search tree; we have to find the binary search tree from it.So, if the input is like [6, 12, 10, 55, 45, 15], then the output willTo solve this, we will follow these steps −Define a function solve() . This will take postordern := size of postorderroot := make a new tree node with last element of postorderstk := a stackinsert root into stki := n - 2while i >= 0, dox := make a new node with value postorder[i]while stk is not empty and postorder[i] < value of top of ... Read More

Construct a Binary Tree from Postorder and Inorder in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:07:33

649 Views

Suppose we have the inorder and postorder traversal sequence of a binary tree. We have to generate the tree from these sequences. So if the postorder and inorder sequences are [9, 15, 7, 20, 3] and [9, 3, 15, 20, 7], then the tree will be −Let us see the steps −Define a method build_tree(), this will take inorder, postorder −If inorder list is not empty −root := make a tree node with the last value of postorder, then delete that elementind := index of root data in inorder listright of root := build_tree(inorder from index ind to end, postorder)left ... Read More

Construct Binary Search Tree from Given Postorder in Python

Arnab Chakraborty
Updated on 27-Aug-2020 14:04:34

292 Views

Suppose we have the postorder traversal sequence of a binary search tree. We have to generate the tree from these sequences. So, if the postorder sequences is [9, 15, 7, 20, 3], then the tree will be −To form a tree we need inorder traversal also, but for binary search tree, the inorder traversal will be in the sorted form.Let us see the steps −Inorder = sorted list of postorder traversal.Define a method build_tree(), this will take inorder, postorder −If inorder list is not empty −root := make a tree node with the last value of postorder, then delete that ... Read More

User-Defined Literals in C++

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

367 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++

Nishtha Thakur
Updated on 27-Aug-2020 13:56:42

308 Views

One of the most frequent question is what will be the value of some uninitialized primitive data values in C or C++? Well the answer will be different in different systems. We can assume the compiler will assign 0 into the variables. It can be done for integer as 0, for float 0.0, but what will be for character type data?Example#include using namespace std; main() {    char a;    float b;    int c;    double d;    long e;    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

Advertisements