Molecular Mass

I’m failing 5th time for test case 10(hidden). Can any one explain how to solve this problem. There is also
no editorial available.

problem link:-link text

my solution link:- link text

According to problem statement what will be mass for ((((HH)3)4)C3)6

1 Like

(HH)= 1$+1 = 2 (HH)3 = 23 = 6 ((HH)3)4) = 64 =24 C3= 123 = 36 ((((HH)3)4)C3) = 24+36 = 60 ((((HH)3)4)C3)6 = 60$6 = 360
This should be the solution according to what I understood…

Plz someone give algorithm for this problem. I have tried many times but fails

now this can be done by using some rules…

0) push a zero before doing anything…

1) push a “0” to stack on getting “(” (while iterating to right side)

2) move till you get “)” and then check next right element… if it is a number take it into a temp variable… otherwise temp=1;

3) start pop back and for each element you get pop last element and add value to it and again push it till you get “(”

4) on getting “(” (while iterating left(popping))

pop two elements from it and add the last element to second last element and again push second last element to stack…

Here is an example…

Click to view

( (16)2 (10)3)2

then stack would be like

  1. 0 (push a zero before starting)
  2. 0 0 (#1st bracket)
  3. 0 0 0 (#2nd bracket)
  4. 0 0 0 (temp=2 on reaching “)” )
  5. 0 0 16 (keep popping adding pushing elements values inside it till “(” while moving left side)
  6. 0 (pop two on reaching “(” while moving left side )
  7. 0 32 (do this immediately after 6th step)
  8. 0 32 0 (on reaching bracket “(” before 10)
  9. 0 32 0 (temp=3 on reaching “)”)
  10. 0 32 10 (keep popping adding pushing elements values inside it till “(” while moving left side)
  11. 0 (pop two on reaching “(” while moving left side )
  12. 0 62
  13. (pop two on reaching “(” while moving left side )
    13)124 (do this immediately)
    the stack with formula is empty hence the only element left in stack is the answer
1 Like
  1. for all characters other than ‘)’ – pushing

  2. for ‘)’ – pop till ‘(’
    while poping – add weight of each element store it in sum(say)

  3. check if next to ‘)’ is digit or not if digit – multiply it with sum
    is this logic right similar to urs.

eg-
((OH)2(C(H))O)3

So first sum = (16+1)*2=34

next popped will be H= 1

next popped will be C= 12

next popped will be O= 16 (but here after ‘)’ there is a digit hence 16*3=48)

therefore final weight comes to be = 34+1+12+16+48

which is wrong that last 3 should be multiplied with whole formula mass

but how to catch this part in code??? :frowning:

How come C3= 12*3= 24 it should be 36.

((((HH)3)4)C3) = 24 + 36=60

((((HH)3)4)C3)6 = 60 * 6=360

so final answer comes to be 360

My code produces 360 still WA. Help me

yeah correct…

so was this test case there in their test cases or this is just an example from you ?? was this test case found wrong by them ?

its an eg. i wanted to check whether my algorithm produces correct output or not. Now I want to know for which test case my algorithm produces wrong o/p. As I submitted five times and always it fails on 10th test case(hidden).

1 Like

can you explain your logic a bit ??

((CH3)2(CH2O)(O3)2)2(OH)
check this one

for all characters except ‘)’-> push into stack

for ‘)’ --> pop from stack, and adding molecular mass of popped element until most recent ‘(’ is encountered at top of stack.

Now after every pop() operation I checked whether immediate next element is digit or not.
because if its a digit(n) then that implies the entire group{from ‘(’
to ‘)’} which recently poped is repeated n times. So my mass will be n*mass.

While popping from stack if digit(n) is encountered(top of stack) and immediate character is an element(let say H) so i have added mass of H ‘n’ times.

1 Like

Output according to me should be 329 and your code is giving 449

@l_returns
((CH3)2(CH2O)(O3)2)2(OH) – 449 my code produces.

simplification
((C2H6)(CH2O)(O6))2(OH)
(C3H8O7)2(OH)
(C6H16O14)(OH)
(C6 H17 O15)
6$12 + 17 + 15$16
72 + 17 + 240 = 329

your code is correct when we input a bit simplified version
((C2H6)(CH2O)(O6))2(OH)

So now can u say where my am i missing in algorithm. can u design algorithm for this problem

I can try… i ll reply as soon as i get it…

Thanks plz revert back when u get it. Thanks once again

1 Like

What i found was… ((H)2©3) here in this test case you added H 6 times which should not be the case… the 3 is only multiplied with C and not H… so here is the problem in your algo…

1 Like