Find Maximum Population Year Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:07:56

635 Views

Suppose we have a table with two columns (birth, death) where each row is representing the birth and death years of the ith person. The population of some year y is the number of people alive during y. The ith person is counted in year y's population when y is in the inclusive range [birth_i, death_i - 1]. (The person is not counted in the year that they die). So, we have to find the earliest year with the maximum population.So, if the input is likeBirthDeath197020101960202019401970then the output will be 2 because there is only one value that matches with ... Read More

Find Two Non-Overlapping Sub-Arrays with Target Sum Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:07:06

229 Views

Suppose we have an array of arr and another value target. We have to find two non-overlapping sub-arrays of arr where each has sum equal to target. If there are multiple answers, then we have to find an answer where the sum of the lengths of the two sub-arrays is smallest. We have to find the minimum sum of the lengths of the two required sub-arrays, if there is no such subarray then return -1.So, if the input is like arr = [5, 2, 6, 3, 2, 5] target = 5, then the output will be 2 there are three ... Read More

Find Least Number of Unique Integers After K Removals Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:06:25

832 Views

Suppose we have an array called nums where only integers are stored. If we have a number k. We have to find least number of unique elements after removing exactly k elements.So, if the input is like nums = [5, 4, 2, 2, 4, 4, 3], k = 3, then the output will be 2, because if we remove 5 and 3, and either any one of 2s or any one of 4s, then there only 2 and 4 will be left.To solve this, we will follow these steps −dictionary:= a new mapfor each num in nums, doif num is ... Read More

Find Minimum Number of Days to Make M Bouquets Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:05:42

726 Views

Suppose we have an array with integers called nums, we also have another two values m and k. Now, we need to make m bouquets. To make one bouquet we need k adjacent flowers from the garden. Here the garden consists of n different flowers, the ith flower will bloom in the bloomDay[i]. Each flower can be used inside only one bouquets. We have to find the minimum number of days need to wait to make m bouquets from the garden. If we cannot make m bouquets, then return -1.So, if the input is like bloomDay = [5, 5, 5, ... Read More

Make File Names Unique Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:04:55

1K+ Views

Suppose we have an array of n strings called names. We have to make n directories in the file system such that, at the ith minute, we will create a directory with the name names[i]. Two files cannot have the same name, if we enter a duplicate directory name the system will have a suffix addition to its name in the form of (k), here, k is the smallest positive integer such that the obtained name remains unique. We have to find an array of strings of length n where ans[i] is the actual name that will be assign to ... Read More

Find the Kth Factor of N Using Python

Arnab Chakraborty
Updated on 29-May-2021 14:04:18

661 Views

Suppose we have two positive values n and k. Now consider we have a list of all factors of n sorted in ascending order, we have to find the kth factor in this list. If there are less than k factors, then return -1.So, if the input is like n = 28 k = 4, then the output will be 7 because, the factors of 28 are [1, 2, 4, 7, 14, 28], fourth one is 7.To solve this, we will follow these steps −if k is same as 1, thenreturn 1cand := a list with one element [1]for i ... Read More

Do-While Loop in Arduino

Yash Sanghvi
Updated on 29-May-2021 14:03:29

2K+ Views

The do-while loop’s syntax in Arduino is similar to the syntax in C. It is given below −do{    //Code } while (condition);Note the semicolon at the end.Examplevoid setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    int i = 5;    do{       Serial.println(i);       i--;    } while(i > 0); } void loop() {    // put your main code here, to run repeatedly: }OutputThe Serial Monitor output is shown below −

Timer Registers in Arduino

Yash Sanghvi
Updated on 29-May-2021 14:03:08

7K+ Views

In a previous article, we used the TimerOne library to add timer interrupts to Arduino. But what if we wish to generate timer interrupts without a third-party library? In that case, you will directly have to meddle with the timer registers in Arduino. In this article, we will just introduce the registers relevant to timer operations and explain their significance. We will also provide the page numbers of the ATmega328 (used in Arduino Uno) datasheet wherein you can find detailed information on these registers.You can find the datasheet here −https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdfTCCRxA and TCCRxBThese are timer control registers. The x stands for ... Read More

Square and Square Root in Arduino

Yash Sanghvi
Updated on 29-May-2021 14:02:50

4K+ Views

Arduino has support for several popular math functions, square and square root being among them. Let’s look at the square root first.Syntaxsqrt(x)where x is a number of any data type. It returns a double.For square, you ideally shouldn’t need a separate function. You can just multiply the number by itself.x_squared = x*x;However, Arduino does have a separate function for calculating squares. The syntax is −sq(x) where x is a number of any data type. This again returns a double.ExampleThe following example illustrates the use of these functions −void setup() {    // put your setup code here, to run once:   ... Read More

Trigonometric Expressions in Arduino

Yash Sanghvi
Updated on 29-May-2021 14:02:29

2K+ Views

Arduino provides 3 basic trigonometric functions: sin(), cos() and tan(). All other trigonometric expressions can be derived from these three functions.All the three functions take in angle in radians (type float) as the input. They return a double.For sin() and cos(), the value is between -1 and 1. The value for tan() has no such bounds.ExampleThe example code below illustrates the use of these functions −void setup() {    // put your setup code here, to run once:    Serial.begin(9600);    Serial.println();    float pi = 3.14159;    float angle_deg = 30;    float angle_rad = angle_deg*pi/180;    Serial.println(sin(angle_rad));   ... Read More

Advertisements