Hackearth's Data Structures solutions( Charged Up Array, Modify Sequence, EEDC Lab ) :
Problem 13: Charged Up Array
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
The total number of subsets of a given array of n elements will be 2^n and therefore the subsets that consist of element Ai will be (2^n)/2 or 2^(n-1) .
Problem 14: Modify Sequence
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
This code is simple. There's no need for any explanation.
Problem 15: EEDC Lab
Solution: (in c++)
( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)
I am going to break the solution in steps so that you can easily understand.
Step 1: putting the elements in a vector v and storing the sum of it's elements in variable sm.
Step 2: now using a for loop to make a new vector v2. In the for loop you can see 3 conditions -
1. if(i = = 0)
2. else
3. if((sm-v[i])%3==0)
First two conditions are easily understandable as the first condition is saying that v2[0]=0 and the second condition is saying that the present element of v2 will become same as the previous element i.e v2[i]=v2[i-1].
Now coming to 3rd condition: Remember the divisibility rule for 3? " If the sum of the digits of a number is divisible by 3, then the number is divisible by 3 " therefore , it will check whether the remaining sum of numbers(left number and right number) is divisible by 3 or not. If it is divisible then we will go deeper. Till this let's take the given example for more clarity --->
v = [1 5 7 5 6 7 5]
sum = 36
Forming v2 vector with each iteration.....
iteration v2 vector
i=0 [0]
i=1 [0,0]
i=2 [0,0,0]
i=3 [0,0,0,0]
i=4 [0,0,0,0,0] since at index 4 , 6 is there so 36-6=30 which is divisible by 3. Now
we will take a look at the deeper conditions.
There are three conditions:
1. i==0 && v[n-1]==0
2. i==n-1 && v[n-2]==0
3. (v[i-1]+v[n-1])%10==0
We have already checked the divisibility by 3 but according to the question sum of numbers(left number and right number) must be divisible by 2 and 5 also or you can say 10 also. For checking the divisibility we have broken the conditions in 3 parts.
First condition says , first element is considered i.e i=0 and it's obvious that v[n-1] must be zero so that formed left number's last digit is zero(obviously here in this case right number will be zero) and of course it will be divisible by 10.
Second condition says , last element is considered i.e i=n-1 and it's obvious that v[n-2] must be zero so that formed right number's last digit is zero(obviously here in this case left number will be zero) and of course it will be divisible by 10.
Third condition says , middle elements are considered and to check divisibility we will check if the sum of the last digit of formed left and right numbers (obviously last digit of the left number will be v[i-1] and last digit of the right number will be v[n-1] i.e last number) is divisible by 10 or 10.
All these conditions confirms that the sum of numbers(left number and right number) is divisible by 2,3 and 5 and after meeting one of these condition (that's why used ||) we will increment the element of v2 by 1. Remember the above else loop it set the v2[4] to 0 but now it will get increment by one as 6 follows the 3rd condition.
Now looking at the iterations:
i=4 [0,0,0,0,1]
i=5 [0,0,0,0,1,1]
i=6 [0,0,0,0,1,1,1]
Now inserting zero at the beginning of the vector v2 as it is mentioned in the question values of L and R is given based on 1-based index.
Therefore the final vector v2 will look like this:
v2 = [0,0,0,0,0,1,1,1]
Now we can easily calculate the number of lucky indices in a given range by using v2[R]-v2[L-1].
bhai ,i still dont get any thing, plzz help bhai...
ReplyDeletewhich question are you talking about ? please message me on instagram at @makingcodesimple .
DeleteWonderful Explanation!!!!!!
ReplyDeleteMeans a lot. Thank you!!!
Delete