Day 12 | Arrival of the General | Drinks | Insomnia Cure | C++
- Ayush Mahajan
- Aug 30, 2022
- 3 min read
Let's Discuss Three Straightforward Questions Today
This question is quite straightforward. We have to find the index of the maximum and minimum height of the soldiers.
Since we have to minimize movement, it is best if we find the smallest index of the person with maximum height. Let's call this person P1 and Index I1
Reason - That person needs to stand at the start of the line.
Similarly, we find the largest index of the person with minimum height. Let's call this person P2 and Index I2
Reason - That person needs to stand at the end of the line.
The number of steps to move P1 to the start of the line is I1 And for P2 it is n - 1 - I2 But remember is P1 was at the right of P2, so when P1 was shifted to position 0, then P2 due to shifting would have shifted to one position right.
if( I1 > I2) ans = I1 + (n - 1 - I2) - 1
else ans = I1 + (n - 1 - I2)We need a little math knowledge to solve this. We are given the percentage of orange juice (OJ) in drinks.
Let's see we have N drinks with A1, A2, ... An composition.
Let's say we take 100 mL of each drink. Then we will have (A1 + A2 +...+An) mL in N*100 mL If you divide the sum by N, you will get the orange juice in 100mL again.
So, basically, we need to average it out.
Now the issue is with how we print up to `k` decimal digits.
There are two ways-
1. Printf
You can use printf like this
printf("%.5f", x); //Prints upto 5 decimals printsRefer to this to learn more about this
2. setprecision
These are also powerful options to use. You can refer to them here [Point 5]
If you had mathematics in high school, you might be trying to remember those complicated sets of formulaes to go ahead with this problem. But if you notice, we only need to go up to 10^5 numbers. That means if we are using a computer we can simply run and loop and mark them.
And then count the numbers we marked.
I did the following -
Create an array of (at least) size d
Run a loop from 1 to d and mark all numbers which is divisible by k, l, m or n
Count the numbers marked.
Btw did you notice this in my code?
int A[100005] = {};Do you know what is Partial Declaration of Array?
By default, the values assigned to un-initialized variables are garbage values. The reason for that is when you create a variable, it will occupy some space. If you have not provided it a value, then it can contain any garbage value.
If you do this
int A[5];
for(int i = 0; i < 5; i++)cout << A[i] << " ";Output can be anything.
You can create an array like this
int A[5] = {1,2,3,4,5};
for(int i = 0; i < 5; i++)cout << A[i] << " ";Then the output will be 1,2,3,4,5 But what will happen if you write this
int A[5] = {1,2};
for(int i = 0; i < 5; i++)cout << A[i] << " ";Then the output will be 1,2,0,0,0 This is called the partial declaration of array
This is what I did. All the elements are set 0
You will find other common partial declarations reading others' code like this
int A[100005] = {};
int A[100005] = {0};![Day 44 | Binary Search Practice [Leetcode] | C++](https://static.wixstatic.com/media/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_167f1b66234f4d30b91bcf5b517f83f3~mv2.jpg)
![Day 43 | Graph and Tree Practice [Leetcode] II | C++](https://static.wixstatic.com/media/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_361d6f7cd90c431b9f25518e1503da94~mv2.jpg)
![Day 42 | Graph Practice [LeetCode] | C++](https://static.wixstatic.com/media/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg/v1/fill/w_980,h_490,al_c,q_85,usm_0.66_1.00_0.01,enc_avif,quality_auto/fbea2a_6570bb28577149f19db06704d542789a~mv2.jpg)
Comments