Given a row vector of numbers, find the indices of the two nearest numbers.
Examples:
[index1 index2] = nearestNumbers([2 5 3 10 0 -3.1])
index1 = 1 index2 = 3
[index1 index2] = nearestNumbers([-40 14 22 17])
index1 = 2 index2 = 4
Notes
Best score without using Neural Net Toolbox is 38
123 lol.
It took me half an hour to solve test 4.
Worlds longest solution winner please, cody!
This Solution works perfectly on MATLAB. Why not here? Need help
my solution passes all the tests in matlab R2011a
function [index1, index2] = nearestNumbers(A)
s=[1,1,Inf];
for i = 1:length(A)-1
for j= 1:length(A)-i
s1=[i,i+j,abs(A(i)-A(i+j))];
if s1(3)<s(3)
s=s1
end
end
end
index1 = s(1);
index2 = s(2);
end
This passed all of the test cases when using MATLAB on my computer, yet it fails here. Anyone know what's wrong with it? I'd appreciate it!
Hard, but doable!
I really liked this problem and although I provided a lot of code to solve it I am pleased with the thought process that took me to it.
This solution is working on my computer.
I don't understand why it is not working
why is this not working? gives correct solution to all test cases on my computer
Another unreadable statement. Does anyone else have trouble using backspace in the "comment on solution box"?
This only works if you have the Neural Network Toolbox
Until they are banned like other functions, many people are using these. It is interesting that only a subset of functions work.
I am partial to dist, it's such a useful function...
All your base are belong to us
414 Solvers
319 Solvers
424 Solvers
387 Solvers
418 Solvers