Suppose we have a directed acyclic graph, with n vertices and nodes are numbered from 0 to n-1, the graph is represented by an edge list, where edges[i] = (u, v) represents a directed edge from node u to node v. We have to find the smallest set of vertices from which all nodes in the graph are reachable. (We can return the vertices in any order).So, if the input is likethen the output will be [0, 2, 3] because these two vertices are not reachable from any other vertices, so if we start from them we can cover all.To ... Read More
Suppose we have following function definition:def modify(arr, op, index): if op == 0: arr[index] += 1 if op == 1: for i in range(len(arr)): arr[i] *=2We have to find minimum number of function calls required to make a given array nums from one zero array of same size?So, if the input is like nums = [1, 5, 3], then the output will be 7 because, initially all elements are 0, [0, 0, 0]At first step increase second element by 1, so array is [0, 1, 0]Double second element ... Read More
Suppose we have an array arr this is holding a permutation of numbers from 1 to n. If we have a binary string of size n and initially all its bits set to zero. Now at each step i (Indexing starts from 1 for both the binary string and arr) from 1 to n, the bit at position arr[i] is set to 1. We also have another value m, and we need to find the latest step at which there exists a group of ones of size m. Here a group of ones means a contiguous substring of 1s such ... Read More
Suppose, we are provided a binary tree. We are also given a pointer to a node (named ‘u’) and we have to find the node situated just right of the provided node. The node situated to the given node's right must stay at the same level and the given node can either be a leaf node or an internal node.So, if the input is likeand u = 6, then the output will be 8.The node situated at the right of node 6 is node 8, so the value 8 is returned to us.To solve this, we will follow these steps ... Read More
Suppose, we are given a binary tree and also two specific nodes x and y. We have to find out the lowest common ancestor of the two nodes from the binary tree. The lowest common ancestor in a binary tree is the lowest node of which both the nodes x and y are descendants. A particular node can also be a descendant of itself. We have to find the node and return it as an output.The node structure of the tree is like below −TreeNode: data: left: right: parent: We have to utilize ... Read More
A problem faced by several people using Arduino, or any microcontroller board for that matter, is that you may forget to start the Serial Monitor before programming the board, and miss some print statements by the time you open the Serial Monitor.One way to overcome this is to start the sketch only after an input is received from the user, via the Serial Monitor. This will ensure that you don’t miss any prints on the Serial Monitor because of the delay in starting the Serial Monitor.Examplevoid setup() { // put your setup code here, to run once: Serial.begin(9600); ... Read More
It may happen that a string may change length dynamically during the execution of a program.If you want to ensure that there is always enough memory available for your string, you can reserve some memory using the reserve() function.SyntaxString1.reserve(n_bytes);Where String1 is the string for which you are reserving memory, and n_bytes (unsigned int) is the number of bytes to be reserved in the memory.ExampleString s1 = "Hello"; void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); s1.reserve(20); s1 = s1+" World!"; Serial.println(s1); s1 = s1+" I'm now trying ... Read More
Arduino has an inbuilt compareTo() function that helps compare which string comes before another. Very crudely, you can think of it as this: if you are given two strings, which one will come first in a dictionary.SyntaxString1.compareTo(String2)Where String1 and String2 are the two strings to be compared. This function returns an integer. Here’s the interpretation of the value of the integer −Negative − String1 comes before String20 − String1 and String2 are equalPositive − String2 comes before String1Please note that this function is case sensitive. So ‘A’ comes before ‘a’ and ‘B’ comes before ‘a’. But ‘a’ comes before ‘b’. Also, ... Read More
The Serial Monitor of Arduino has a text box at the top, through which, users can send in text to the Arduino board.The text can be read by Serial.read(). Also, the Serial.available() function can be used to check if there is any data to read. It returns the number of characters or bytes available for reading, i.e., the number of bytes stored in the serial receive buffer.ExampleUsing these functions, let’s create a simple echo program for Arduino. The code for the same can be found below −void setup() { // put your setup code here, to run once: ... Read More
The remove function in Arduino helps you remove one or more characters from within a string.SyntaxmyString.remove(index, count)Here, index refers to the index from where removal has to start. Note that indexing in Arduino starts with 0. Thus, within string "Hello", 'H' is at index 0, 'e' is at index 1, and so on.The count argument is optional, and it specifies the number of characters to remove. If you don’t specify the count, then all characters starting from index till the end of the string will be removed. If you specify count as say, 3, then 3 characters starting from index position will ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP