Suppose we have an array nums, where each nums[i] contains three elements [type_i, color_i, name_i]. These are describing the type, color, and name of the ith item. We also have a rule represented by two other strings, ruleKey and ruleValue. Now we can say the ith item is matched the rule if one of the following is true −ruleKey = "type" and ruleValue = type_i.ruleKey = "color" and ruleValue = color_i.ruleKey = "name" and ruleValue = name_i.We have to find number of matching we can find.So, if the input is likeBikeblueElecBCarsilverSumoBikeblueTVSAnd ruleKey = "color", ruleValue = "blue", then the output ... Read More
Suppose we have a set of points given in an array called pts. We also have another point (x, y) which is our current location. We are defining a valid point as, a point which shares the same x-coordinate or the same y-coordinate as our current point. We have to return the index of the valid point with the smallest Manhattan distance from our current location (x, y). If there are more than one points, then return the valid point with the smallest index. (Note: the Manhattan distance between two points (a, b) and (p, q) is |a - p| ... Read More
Suppose we have a binary string s (without leading zeros), We have to check whether s contains at most one contiguous segment of ones or not.So, if the input is like s = "11100", then the output will be True as there is one segment of ones "111".To solve this, we will follow these steps −count := -1if size of s is same as 1, thenreturn Truefor each i in s, doif i is same as "1" and count > -1, thenreturn Falseotherwise when i is same as "0", thencount := count + 1return TrueLet us see the following implementation ... Read More
Suppose we have two strings s and t of same length. Consider an operation where we choose two indices in a string (not necessarily different) and swap the characters at the selected indices. We have to check whether it is possible to make both strings same by performing at most one string swap on exactly one of the strings or not.So, if the input is like s = "hello" t = "hlelo", then the output will be True because we need to swap 'e' and 'l' at either s or t to make them equal.To solve this, we will follow ... Read More
Suppose we have an alphanumeric string s, we have to find the second largest numerical digit that appears in s, if there is no such string then return -1.So, if the input is like s = "p84t3ho1n", then the output will be 4 as the digits are [1,3,4,8], so second largest digit is 4.To solve this, we will follow these steps −lst := a new setfor each let in s, doif let is not alphabetic, theninsert let as integer in lstif size of lst
Suppose we have an array of positive values called nums, we have to find the maximum possible sum of an ascending subarray in nums. We can say a subarray [nums_l, nums_l+1, ..., nums_r-1, nums_r] is ascending when for all i where l nums[i-1], thentotal := total + nums[i]otherwise, total:= nums[i]if total > max_total, thenmax_total:= totalreturn max_totalLet us see the following implementation to get better understanding −Example Live Demodef solve(nums): total=nums[0] max_total=nums[0] for i in range(1, len(nums)): if nums[i] > nums[i-1]: total+=nums[i] else: total=nums[i] ... Read More
In order to get the max/ min values of an array in Arduino, we can run a simple for loop. Two implementations are shown below. One uses the max() and min() functions of Arduino, and the other uses the > and < operators.The max and min functions have the following syntax: max(a, b) and min(a, b), and they return the max and min values out of a and b respectively.Implementation 1 − Using > and < operatorsvoid setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); int myArray[6] = {1, 5, -6, ... Read More
The pow() function of Arduino can be used for evaluating exponential expressions. Any expression of the form ab can be expressed as pow(a, b). For example 23 becomes pow(2, 3).The type for both the base (a) and the exponent (b) is float. This function returns a double.Examplevoid setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); float base = 2; float exponent = 3; Serial.println(pow(base, exponent)); } void loop() { // put your main code here, to run repeatedly: }OutputThe Serial Monitor Output is shown below −You are ... Read More
A wattmeter is an instrument which is used to measure electric power given to or developed by an electrical circuit. Generally, a wattmeter consists of a current coil and a potential coil.Types of WattmeterElectrodynamometer wattmeter – for both DC and AC power measurementInduction wattmeter – for AC power measurement onlyWorking Principle of Electrodynamometer WattmeterThe electrodynamometer wattmeter works on the dynamometer principle i.e. a mechanical force acts between two current carrying conductors or coils.It consists of a fixed which is divided into two halves which are parallel to each other and is connected in series with the load while the moving ... Read More
Converting analog values to digital is a common requirement from microcontrollers in general, and Arduino is no different. Arduino IDE has a built-in analogRead function to facilitate the conversion of analog values to digital.From the programming perspective, the only thing you require to know is the pins of your microcontroller that support ADC. On the Arduino UNO board, the pins A0 to A5 support ADC.Now, let us assume that you’ve connected your A0 pin to an analog wire (maybe the junction between an LDR and a resistor, or the central leg of a potentiometer).The basic Arduino code to print the analog ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP