C++ Articles - Page 335 of 719

Range Sum Query 2D - Mutable in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:17:37

500 Views

Suppose we have a 2D matrix called matrix, we have to calculate the sum of the elements inside the rectangle defined by its upper left corner and lower right corner.So, if the input is like3014256321120154101710305So will be methods to find sum, update value, if we call them likesumRegion(2, 1, 4, 3)update(3, 2, 2)sumRegion(2, 1, 4, 3) ,then the output will be 8 and 10, as the above rectangle (with the green color) is defined by (2, 1) and (4, 3), which contains sum = 8.To solve this, we will follow these steps −Define one 2D array treeDefine one 2D array ... Read More

Smallest Rectangle Enclosing Black Pixels in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:12:40

428 Views

Suppose we have an image and that image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. Here the black pixels are connected, so there is only one black region. Pixels are connected horizontally and vertically. If we have a location (x, y) of one of the black pixels, we have to find the area of the smallest (axis-aligned) rectangle that encloses all black pixels.So, if the input is like001001100100and x = 0, y = 2, then the output will be 6To solve this, we will follow these steps −Define one ... Read More

Serialize and Deserialize Binary Tree in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:09:58

939 Views

Suppose we have one binary tree and we have to serialize and deserialize them. As we know that the serialization is the process of converting a data structure or object into a sequence of bits so we can store them in a file or memory buffer, and that can be reconstructed later in the same or another computer environment.Here we have to devise an algorithm to serialize and deserialize binary tree. The binary tree is a rooted tree in which each node has no more than 2 children.So, if the input is likethen the output will be Serialize − 1 ... Read More

Best Meeting Point in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:05:42

269 Views

Suppose there is a group of two or more people and they wants to meet and minimize the total travel distance. We have a 2D grid of values 0 or 1, where each 1 mark the home of someone in the group. The distance is calculated using the formula of Manhattan Distance, so distance(p1, p2) = |p2.x - p1.x| + |p2.y - p1.y|.So, if the input is like100010000000100then the output will be 6 as from the matrix we can understand that three people living at (0, 0), (0, 4), and (2, 2): The point (0, 2) is an ideal meeting ... Read More

Word Pattern II in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:03:39

276 Views

Suppose we have a pattern and a string called str, we have to check whether str follows the same pattern or not. Here pattern follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str.So, if the input is like pattern is "abaa", str is "orangegreenorangeorange", then the output will be trueTo solve this, we will follow these steps −Define a function solve(), this will take i, j, ptr, s, a map m, one set called used, if i >= size of s and j >= size of ptr, ... Read More

Closest Binary Search Tree Value II in C++

Arnab Chakraborty
Updated on 21-Jul-2020 08:00:41

447 Views

Suppose we have a binary search tree and a target value; we have to find k values in that BST that are closest to the target. We have to keep in mind that the target value is a floating-point number. We can assume k is always valid, and k ≤ total nodes.So, if the input is liketarget = 3.714286, and k = 2, then the output will be [4, 3]To solve this, we will follow these steps −Define a function pushSmaller(), this will take node, stack st, and target, while node is not present, do −if val of node < ... Read More

Alien Dictionary in C++

Arnab Chakraborty
Updated on 21-Jul-2020 07:55:11

569 Views

Suppose there is a new alien language and that uses the latin alphabet. However, the order among letters are not known. We have a list of non-empty words from the dictionary, these words are sorted lexicographically by the rules of this new language. we have to find the order of letters in this language.So, if the input is like ["wrt", "wrf", "er", "ett", "rftt" ], then the output will be "wertf"To solve this, we will follow these steps −Define one map called degreeDefine one map called graphn := size of wordsfor initialize i := 0, when i < size of ... Read More

Paint House II in C++

Arnab Chakraborty
Updated on 21-Jul-2020 07:52:38

454 Views

Suppose we have n houses in a row, now each house can be painted with one of the k colors. The painting cost of each house with a certain color is different. Now we have to keep in mind that we have to paint all the houses such that no two adjacent houses have the same color.The cost of painting each house with a certain color is represented by a matrix of order n x k. And we have to find the minimum cost to paint all houses.So, if the input is like153294then the output will be 5, as paint ... Read More

Strobogrammatic Number III in C++

Arnab Chakraborty
Updated on 21-Jul-2020 07:50:23

651 Views

Suppose we want to define a function to count the total strobogrammatic numbers that exist in the range of (low and high). As we know that a strobogrammatic number is a number that looks the same when rotated 180 degrees.So, if the input is like low = "50", high = "100", then the output will be 3 as there are three results, 69, 88, and 96.To solve this, we will follow these steps −Define a function findStrobogrammatic(), this will take n, Define an array retif n & 1 is non-zero, then −insert "0" at the end of retinsert "1" at ... Read More

C program for pipe in Linux

sudhir sharma
Updated on 17-Jul-2020 13:14:14

2K+ Views

Here, we will create a C program for pipe in Linux. In this program, we will read some text from the input stream and then print it to the output screen.First, let’s learn basics about pipe in LinuxPipe is used to transfer data, it can be used for communication between process/ command/ program for transferring standard output between two in Linux or Unix based system.One important thing to be noted is that pipes are unidirectional i.e. data can either flow from left to right or from right to left in the program.Here, we will create a pipe that will read ... Read More

Advertisements