Siretu
BAN USERI think you're thinking of a divisibility rule. First of all, the digit sum doesn't have to equal 9, it just have to be divisible by 9.
However, there are a bunch of other problems. First of all, you're not checking for a power of three, you're checking for a multiple of 9. For example, 18 is not a power of 3 but 1+8 = 9, and will therefore return true with your way.
In addition, even if this worked, it would miss the initial ones since the first power it can detect is 9. It will miss 3^0 (=1) and 3^1 (=3)
Your code doesn't really work properly. First of all, you change around the order of the arrays in minArrayElem, which is against the project specification.
Also, it doesn't always create the correct result. Here's the output from one of the runs of your program:
ax1[47 22 26 31 89 50 12]
ax2[71 68 57 98 0 24]
ax3[45]
minimum from array but max for other 2 arrays = 89
89 is not larger than all the elements in ax2 and ax3. In ax2, there's 98 which is larger, so the output is wrong.
Also, sorting them in this case is unnecessary since it has the time complexity O(n*log(n)) at best, while finding the maximum element is O(n). In this case, your algorithm seems to get the time complexity O(A*log(A) + B*log(B) + C*log(C)), where a more optimal (and easier) algorithm would have O(A+B+C)
I think this approach would work if the elements in the array are known to be unique. In that case, we know that at most two out of the first three elements are either max/min. Hence one of them must be neither.
If there can be duplicates however, you could have the array: [1,1,1,2,3,4], in which case the solution is not among the first three elements.
Honestly, it's something I would have asked and had specified in an interview.
For your first example, it wasn't specified how it would handle the case where there's no proper answer. -1 is often a better answer, but should be clarified in the interview. If you do, just add an if at the end.
For the second one, it wasn't clear in your question that you wanted the maximum to be inclusive. The clarification I asked specified that the element in A should be strictly greater than all elements in B and C. In any case, you just have to change it to "and x >= maxi".
I just threw something together here, maybe I forgot something...
import sys
A = [126, 110, 130]
B = [125]
C = [105, 115]
maxi = max(max(B),max(C))
mini = sys.maxint
for x in A:
if x < mini and x > maxi:
mini = x
print mini
It assumes that B and C are not empty. If they can be, you can just add checks for that.
- Siretu February 01, 2015
Unfortunately, this solution does not work. For input: [1,2,3,6,7,8,9] it returns ([1, 9, 3, 6], [7, 8, 2]). This solution has a difference of 19 - 17 = 2, however ([1,8,9],[2,3,6,7]) would be a better solution with the difference 18 - 18 = 0.
- Siretu August 29, 2015