Its giving right answer on dry run
Link:https://ide.codingblocks.com/s/335261
Please tell me why my code is not passing all the test cases
Please correct the code and tell me the error.Also tell is there any effficient method other than this
hello @KetanPandey i dont think yout approach will worl for test cases or for the question .
i am attaching the code here https://ide.codingblocks.com/s/335271.
if you dint understand the logic or anything please reply here .
we will discuss .
Happy Learning !!
I want to know why my approach is not valid,otherwise i will never be able to figure out what wrong did i do
Even in your approach also,i am unable to figure out what you are doing,please explain your approach
@KetanPandey the problem is your code is not able to handle the test cases which are larger than 1e7 thats why you are not able to find your mistake .
sieve type approach is valis till 1e7.
like try for this
1
5000
expected ouput is : 50837316566580
but yout output is different from the expected output .
sometimes to optimised your approach you need to follow particular appraoch because test cases in question are larger .
that why i recommend you to go with the code which i gave you .
Happy Learning !!
@KetanPandey the explaation for my code is here :
Ugly numbers are those number whose prime factors are 2, 3 or 5.
The ugly-number sequence is 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
because every number can only be divided by 2, 3, 5, one way to look at the sequence is to split the sequence to three groups as below:
(1) 1×2, 2×2, 3×2, 4×2, 5×2, …
(2) 1×3, 2×3, 3×3, 4×3, 5×3, …
(3) 1×5, 2×5, 3×5, 4×5, 5×5, …
Then we use similar merge method as merge sort, to get every ugly number from the three subsequence. Every step we choose the smallest one, and move one step after.
So do something like this:
Begin
define array named uglyNum of size n
i2 := 0, i3 := 0, i5 := 0
next2mul := 2, next3mul := 3, next5Mul := 5
next := 1
ugluNum[0] := 1
for i := 1 to n, do
next := minimum of next2Mul, next3Mul and next5Mul
uglyNum[i] := next
if next = next2Mul, then
i2 := i2 + 1
next2mul := uglyNum[i2] x 2
if next = next3Mul, then
i3 := i3 + 1
next3mul := uglyNum[i3] x 3
if next = next5Mul, then
i5 := i5 + 1
next5mul := uglyNum[i5] x 5
done
return next
End
Happy Learning !!
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.