Generating random numbers is one of the key requirements from microcontrollers. Random numbers have several applications. Let’s not get there. You must have an application in mind, which brought you to this page. Generating random numbers is very easy in Arduino, thanks to the inbuilt random() function.Syntaxrandom(min, max)ORrandom(max)where min is 0 by default.Min is inclusive, while max is exclusive. Thus, random(10, 50) will return a number integer between 10 and 49 (10 and 49 included). random(100) will return a random number between 0 and 99, both included. Note that the random function’s return type is long.Examplevoid setup() { // put ... Read More
In an earlier article, we have seen how PWM can be set on Arduino Uno using the analogWrite() function. Pins 3, 5, 6, 9, 10 and 11 of Arduino Uno can support PWM. The frequency of the square wave is 490 Hz (about 2 ms time period) on all pins except 5 and 6, on which it is 980 Hz (about 1s time period). With analogWrite() you get control over the duty cycle, but not on the frequency of the generated square wave.In this article, we will look at another way of setting PWM in Arduino Uno, specific to Timer1. The advantage ... Read More
Suppose we have a binary array called nums, we can delete one element from it. We have to find the size of the longest non-empty subarray which is containing only 1's in the resulting array. If there is no such subarray, then return 0.So, if the input is like nums = [1, 0, 1, 1, 1, 0, 1, 1, 0], then the output will be 5 because by removing 0 from position 5, we can get a subarray [1, 1, 1, 1, 1] there are five 1s.To solve this, we will follow these steps −if 0 is not in nums, ... Read More
Suppose we have an array called nums, this array contains even number of elements, and have another value k. We have to split nums into exactly n/2 pairs such that the sum of each pair is divisible by k. If we can do so then return true, otherwise false.So, if the input is like nums = [9, 5, 3, 4, 7, 10, 20, 8] k = 3, then the output will be True because we can make pairs like (9, 3), (5, 7), (4, 20), (8, 10), sum of all pairs are divisible by 3.To solve this, we will follow ... Read More
Suppose we have an array called arr, this contains unique elements and we also have another value k. Now consider a game where we take first two elements of the array. In each turn, we compare arr[0] with arr[1], and the larger value wins and remains at position 0 and the smaller value moves to the end of the array. This game will end when a value wins’ k consecutive rounds. We have to find the winner from the array.So, if the input is like arr = [1, 5, 6, 3, 4, 2], and k = 3, then the output ... Read More
Suppose we have a string s with opening and closing parenthesis '(' and ')'. We can say a parentheses string is balanced when −Any left parenthesis '(' have a corresponding two consecutive right parenthesis '))'.A Left parenthesis '(' must go before the corresponding two consecutive right parenthesis '))'.So for example, "())", "())(())))" are balanced but ")()", "()))" are not. If we have such string, we have to count number of parenthesis (opening or closing) to make string balanced.So, if the input is like s = "(())))))", then the output will be 1 because if we split it up, we can ... Read More
Suppose, we are provided two expression trees. We have to write a program that checks the two expression trees and determines if the expression trees generate similar values. The two expression trees are provided to us in an in-order manner and we return a True value if they match, or else we return a False value.So, if the input is likethen the output will be True.The two expression trees evaluate to the same value.To solve this, we will follow these steps:Define a function dfs() . This will take node, dicif node is empty, thenreturnif left of node and right of ... Read More
Suppose we have n bulbs in a room, these bulbs are numbered from 0 to n-1. We have to arrange them in a row from left to right. Initially, all the bulbs are turned off (0-state). We have to get the configuration represented by given target array 't' where t[i] is '1' if the ith bulb is on and '0' if it is off. We also have a switch to flip the state of the bulb. And flipping operation is defined as follows −Select any bulb index i.Flip each bulb from index i to index n - 1.We have to ... Read More
Suppose we have a binary tree. and another value distance d. A pair of two different leaf nodes are said to be good, when the shortest path between these two nodes is smaller or same as distance d.So, if the input is likeAnd distance d = 4, then the output will be 2 because the pairs are (8, 7) and (5, 6) as their path length distance is 2, but (7, 5) or (8, 6) or other pairs are not good as their path length is 5 which is larger than d = 4To solve this, we will follow these ... Read More
Suppose we have a n x n binary matrix. We can perform an operation on it like, at one step we select two adjacent rows and swap them. We have to count number of minimum swaps required, so that all nodes above the major diagonal of the matrix is 0. If there is no such solution, then return -1.So, if the input is like010011100then the output will be 2 because −To solve this, we will follow these steps:n := row count of matrixm := make an array of size n and fill with nfor i in range 0 to n ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP