We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
One thing I don't agree with the editorial is "Ans as we know, Answer for null set is 1". So, I used ans[1] as a base. And it will be better if editorial wouldn't contain typos.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Shashank and List
You are viewing a single comment's thread. Return to all comments →
Your approach is absolutely correct (with only comment, that it should be
ans[i] = ans[i-1](2^ai + 1) + 2^ai
instead of
ans[i] = ans[i-1](2^i + 1) + 2^i),
which is just a misprint, I suppose).
And it can be converted as:
ans[i-1](2^a[i] + 1) + 2^a[i] =
= ans[i-1](2^a[i] + 1) + (2^a[i] + 1) - 1 =
= (2^a[i] + 1)(ans[i-1] + 1) - 1 = ... =
= (2^a[i] + 1)(2^a[i-1] + 1)...(2^a[2] + 1)(ans[1] + 1) - 1 =
= (2^a[i] + 1)(2^a[i-1] + 1)...(2^a[2] + 1)(2^a[1] + 1) - 1,
which is exactly what editorial has.
One thing I don't agree with the editorial is "Ans as we know, Answer for null set is 1". So, I used ans[1] as a base. And it will be better if editorial wouldn't contain typos.