Given two arrays a and b write a function comp(a,b) that checks whether the two arrays have the “same” elements, with the same multiplicities. “Same” means, here, that the elements in b are the elements in a squared, regardless of the order.
5/22/2017 3:08:04 PM
Sebastián Abarca1 Answer
New AnswerHello :> Offhand I can think of two possible solutions: 1) simply iterating through all the elements in a. for each element in a, you search the whole array b to check if a^2 is in b. time taken: O(n^2) 2) sort the elements in array b first, then conduct binary search for a^2 each time. time taken: O(n log n + n log n) = O(n log n)
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message