Server Side Programming Articles - Page 1450 of 2650

Range Addition in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:50:25

242 Views

Suppose we have an array of size n and that is initialized with 0's and we also have a value k, we will perform k update operations. Each operation will be represented as triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ... endIndex] (startIndex and endIndex inclusive) with inc. We have to find the modified array after all k operations were executed.So, if the input is like length = 5, updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]], then the output will be [- 2, 0, 3, 5, 3]To solve this, we will follow ... Read More

Plus One Linked List in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:48:16

251 Views

Suppose we have a non-negative integer represented as non-empty a singly linked list of digits, now we have to plus one to the integer. We may assume the integer do not contain any leading zero, except the number 0 itself. In the linked list the most significant digit is at the head of the list.So, if the input is like [1, 2, 3], then the output will be [1, 2, 4]To solve this, we will follow these steps −if head is null, then −return headcurr = headreq = NULLwhile curr is non-zero, do −if val of curr is not equal ... Read More

Find Leaves of Binary Tree in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:45:51

668 Views

Suppose we have a binary tree. We will collect and remove all leaves and repeat until the tree is empty.So, if the input is likethen the output will be [[4, 5, 3], [2], [1]]To solve this, we will follow these steps −Define one map szDefine one 2D array retDefine a function dfs(), this will take node, if node is null, then −sz[val of node] := 1 + maximum of dfs(left of node) and dfs(right of node)if size of ret < sz[val of node], then −Define an array tempinsert temp at the end of retinsert val of node at the end ... Read More

Nested List Weight Sum II in Python

Arnab Chakraborty
Updated on 19-Nov-2020 09:43:00

304 Views

Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Different from the previous question where weight is increasing from root to leaf, now the weight is defined from bottom up. i.e., the leaf level integers have weight 1, and the root level integers have the largest weight.So, if the input is like [[1, 1], 2, [1, 1]], then the output will be 8, as four 1's at depth 1, one 2 ... Read More

Design Hit Counter in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:39:49

688 Views

Suppose we want to design a hit counter which counts the number of hits received in the past 5 minutes. There will be a function and that accepts a timestamp parameter in the second unit and we may assume that calls are being made to the system in chronological order (so, the timestamp is monotonically increasing). We also assume that the earliest timestamp starts at 1.It is possible that several hits arrive roughly at the same time.So we will call the hit() function to hit and getHits() function to get the count of hits.To solve this, we will follow these ... Read More

Bomb Enemy in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:38:01

368 Views

Suppose we have a 2D grid, here each cell is either a wall 'W', an enemy 'E' or that is empty '0', We have to find the maximum enemies we can kill using one bomb. The bomb kills all the enemies in the same row and column from the planted point until it hits the wall. And we can put bombs only on blank spaces.So, if the input is likethen the output will be 3, as placing bomb at the green place, it will kill three enemies.To solve this, we will follow these steps −ret := 0n := row count ... Read More

Sort Transformed Array in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:35:05

268 Views

Suppose we have a sorted array of integer nums and integer values a, b and c. We have to apply a quadratic function of the form f(x) = ax^2 + bx + c to each element x in the array. And the final array must be in sorted order.So, if the input is like nums = [-4, -2, 2, 4], a = 1, b = 3, c = 5, then the output will be [3, 9, 15, 33]To solve this, we will follow these steps −Define function f(), that takes x, a, b, c −return ax^2 + bx + cFrom ... Read More

Line Reflection in C++

Arnab Chakraborty
Updated on 19-Nov-2020 09:32:53

510 Views

Suppose we have n points on a 2D plane, we have to check whether there is any line parallel to y-axis that reflect the given points symmetrically, in other words, check whether there exists a line that after reflecting all points over the given line the set of the original points is the same that the reflected ones.So, if the input is like points = [[1, 1], [-1, 1]]then the output will be trueTo solve this, we will follow these steps −Define one set okn := size of pointsminVal := infmaxVal := -inffor initialize i := 0, when i < ... Read More

How to replace missing values in a column with corresponding values in other column of an R data frame?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:23:52

501 Views

Often, we get missing data for analysis and we need to replace those missing values with something. Sometimes, we might want to replace them with the corresponding values in other column especially in situations when both the columns depict similar characteristics. This type of replacement can be easily done with the help of mutate function of dplyr package as shown in the below examples.Example1Consider the below data frame:Live Demo> set.seed(214) > x1 x2 df1 df1Output x1 x2 1 4 75 2 8 24 3 5 38 4 4 38 5 7 NA 6 6 24 7 10 75 8 4 75 ... Read More

How to create a matrix using vector generated with rep function in R?

Nizamuddin Siddiqui
Updated on 19-Nov-2020 08:21:56

393 Views

Matrix can be only generated if we pass an even number of elements for it. If we want to create a matrix using vector generated with rep function then the length of this vector must be divisible by 2. For example, if we have a vector x that is created with rep function and it’s length is 20 then the matrix say M of size 10x2 using that vector can be constructed by using matrix(x, ncol=2).Example 1Live Demo> x M1 M1Output[, 1] [, 2] [1, ] 10 10 [2, ] 4 4 [3, ] 7 7 [4, ] 3 3 ... Read More

Advertisements