Maximum Width of Binary Tree in C++

Arnab Chakraborty
Updated on 02-May-2020 09:59:28

217 Views

Suppose we have a binary tree, we have to define a function to get the maximum width of the given tree. Here the width of a tree is the maximum width among all levels. We will consider the binary tree has the same structure as a full binary tree, but some nodes are null. The width of one level is actually the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the null nodes between the end-nodes are also counted for the length calculation). So if the tree is like −Then the maximum width ... Read More

Implement JShell Using JavaFX in Java 9

raja
Updated on 02-May-2020 09:45:07

300 Views

JShell is an interactive tool used to implement sample expressions. We can implement JShell programmatically using JavaFX application then we need to import a few packages in the java program listed belowimport jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet;In the below example, implemented a sample Java FX application. We will enter different values in the text field and press the "eval" button. It will display values with corresponding data types in a list.Exampleimport javafx.application.Application; import javafx.scene.Scene; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.stage.Stage; import java.util.List; import jdk.jshell.JShell; import jdk.jshell.SnippetEvent; import jdk.jshell.VarSnippet; public class JShellFXTest extends Application {    @Override    public void start(Stage primaryStage) ... Read More

K-Concatenation Maximum Sum in C++

Arnab Chakraborty
Updated on 02-May-2020 09:28:30

318 Views

Suppose we have an integer array arr and one integer k, we have to change the array by repeating it k times. So if arr = [1, 2] and k = 3 then the modified array will be [1, 2, 1, 2, 1, 2].Now we have to find the maximum sub-array sum in the modified array. Note that the length of the sub-array can be 0 and its sum in that case is 0. As the answer may be very large, find the answer modulo 10^9 + 7.So if the input is like [1, -2, 1] and k = 5, ... Read More

Invalid Transactions in C++

Arnab Chakraborty
Updated on 02-May-2020 09:25:30

240 Views

Suppose there are some transactions. A transaction is possibly invalid if −The amount exceeds $1000, or;If it occurs within (and including) 60 minutes of another transaction with the same name in a different city.Here each transaction string transactions[i] consists of comma separated values representing the name, time (in minutes), amount, and city of the transaction. We have a list of transactions, find a list of transactions that are possibly invalid. So if the input is like ["alice, 20, 800, mtv", "bob, 50, 1200, mtv"], then the answer will be ["bob, 50, 1200, mtv"].To solve this, we will follow these steps ... Read More

As Far From Land As Possible in C++

Arnab Chakraborty
Updated on 02-May-2020 09:21:47

337 Views

Suppose we have one N x N grid containing only values like 0 and 1, where 0 represents water and 1 represents the land, we have to find a water cell such that its distance to the nearest land cell is maximized and return the distance. Here we will use the Manhattan distance − the distance between two cells (x0, y0) and (x1, y1) is |x0 - x1| + |y0 - y1|. If no land or water is present in the grid, then return -1.101000101Then the output will be 2, as the cell (1, 1) is as far as possible ... Read More

CamelCase Matching in C++

Arnab Chakraborty
Updated on 02-May-2020 09:17:22

797 Views

Suppose we have a list of queries, and a pattern, we have to return an answer that will be list of booleans, where answer[i] is true if and only if queries[i] matches the pattern. A query word matches a given pattern when we can insert lowercase letters to the pattern word so that it equals the query.So if the input is like ["FooBar", "FooBarTest", "FootBall", "FrameBuffer", "ForceFeedBack"] and pattern = "FB", then the results will be [true, false, true, false, false].To solve this, we will follow these steps −Define a method called insertNode(), this takes head and string scurr := ... Read More

Minimum Swaps to Make Sequences Increasing in C++

Arnab Chakraborty
Updated on 02-May-2020 09:14:29

353 Views

Suppose we have two integer sequences A and B of the same non-zero length. We can swap elements A[i] and B[i]. We have to keep in mind that both elements are in the same index position in their respective sequences. After completing some number of swaps, A and B are both strictly increasing. We have to find the minimum number of swaps to make both sequences strictly increasing.So if the input is like A = [1, 3, 5, 4] and B = [1, 2, 3, 7], then the answer will be 1, if we swap A[3] with B[3], then the ... Read More

All Paths from Source to Target in C++

Arnab Chakraborty
Updated on 02-May-2020 09:11:33

519 Views

Suppose we have a directed, acyclic graph with N nodes. We have to find all possible paths from node 0 to node N-1, and return them in any order. The graph is given as follows: the nodes are 0, 1, ..., graph.length - 1. graph[i] is a list of all nodes j for which the edge (i, j) exists.So if the input is like [[1, 2], [3], [3], []], then the output will be [[0, 1, 3], [0, 2, 3]].To solve this, we will follow these steps −Make one 2d array called resDefine a method called solve, this will take ... Read More

Number of Subarrays with Bounded Maximum in C++

Arnab Chakraborty
Updated on 02-May-2020 09:09:25

225 Views

Suppose we have an array A of positive integers, and two positive integers L and R are also given. We have to find the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least L and at most R. So if A = [2, 1, 4, 3] and L = 2 and R = 3, then output will be 3 as there are three sub arrays that meet the requirements. So these are [2], [2, 1], [3].To solve this, we will follow these steps −ret := 0, dp := 0, ... Read More

Custom Sort String in C++

Arnab Chakraborty
Updated on 02-May-2020 09:07:20

558 Views

Suppose we have S and T two strings these are composed of lowercase letters. In S, no letter occurs more than once. S was sorted in some custom order previously. We have to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x will occur before y in the returned string.So if the S = “cba” and T = “abcd”, then the output will be “cbad”. Here "a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", ... Read More

Advertisements