Can Someone please help me with this question...? ...
# announcements
s
Can Someone please help me with this question...? https://www.hackerrank.com/challenges/between-two-sets/forum My answer fails two test cases , becuz pf timeout...😥 Answer: https://justpaste.it/50bns Thanks in advance....
t
just looking at your code : that's pretty far from idiomatic kotlin. It looks like a frustrated C programmer 😁
😂 1
but that's not what your asking...
here is my solution to the problem. it's more idiomatic kotlin and passes all the test cases : https://pl.kotl.in/9cjB7l5k4
your checking way to many numbers. You know the #s your looking for have to be a multiple of the largest # in the a list. So increment by that instead of 1. cut out a lot of divisions.
x
That's a brilliant code, @TwoClocks Is
val lrgFac = b.fold(Int.MAX_VALUE) {acc,i-> Math.min(acc,i)}
some form of the
Array.minXXX
method? And
val maxA = a.reduce { acc, i ->  Math.max(acc,i)}
is some form of
Array.maxXXX
? IMO, the
findNumbers
function still refers to
lrgFac
and
b
outside of that function which makes it a little bit difficult to read. Thank you very much for sharing your code. I learned a lot from it.
t
@Xuc Xiem thanks. The HackerRank enviroment is strange. I'm not sure what version of kotlin they run, and it looks like the restrict the APIs you can use. I couldn't figure out which max/min was on there version of list (kotlin has changed the max/min api a few times over various version). so I just did it w/ fold / reduce... but yeah I would rather use some version of maxXXX. I use
tailrec fun()
instead of while loops. If you think about it like that it becomes easier to read. Yeah, it's using var/val's out side the function, like a
while()
loop would.
@Xuc Xiem using a
while()
loop means using a var. w/
tailrec
everything is all val. Also the
tailrec
tells the compiler more about what's happening. In combo w/ only vals, in theory the compiler should be better able to optomize the loop (or at the least not have to check for normal while() for() edge cases). But I've never actually tested to see if that's true. The one times I did look the compiled byte code was very similar. So mostly that's just a story I tell myself 🙂 But I do like it better aesthetically. having no vars makes me happy for some reason.