Cody

# Problem 29. Nearest Numbers

Solution 1405716

Submitted on 5 Jan 2018 by Vincent Kroeze
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Pass
A = [30 46 16 -46 35 44 18 26 25 -10]; correct = [8 9]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

B = 46 16 -14 -76 5 14 -12 -4 -5 -40 -16 46 -30 -92 -11 -2 -28 -20 -21 -56 14 30 46 -62 19 28 2 10 9 -26 76 92 62 46 81 90 64 72 71 36 -5 11 -19 -81 46 9 -17 -9 -10 -45 -14 2 -28 -90 -9 46 -26 -18 -19 -54 12 28 -2 -64 17 26 46 8 7 -28 4 20 -10 -72 9 18 -8 46 -1 -36 5 21 -9 -71 10 19 -7 1 46 -35 40 56 26 -36 45 54 28 36 35 46 a = 9 8 a = 9 8 a = 8 9 index1 = 8 index2 = 9

2   Pass
A = [1555 -3288 2061 -4681 -2230 -4538 -4028 3235 1949 -1829]; correct = [3 9]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

B = 3235 -4843 506 -6236 -3785 -6093 -5583 1680 394 -3384 4843 3235 5349 -1393 1058 -1250 -740 6523 5237 1459 -506 -5349 3235 -6742 -4291 -6599 -6089 1174 -112 -3890 6236 1393 6742 3235 2451 143 653 7916 6630 2852 3785 -1058 4291 -2451 3235 -2308 -1798 5465 4179 401 6093 1250 6599 -143 2308 3235 510 7773 6487 2709 5583 740 6089 -653 1798 -510 3235 7263 5977 2199 -1680 -6523 -1174 -7916 -5465 -7773 -7263 3235 -1286 -5064 -394 -5237 112 -6630 -4179 -6487 -5977 1286 3235 -3778 3384 -1459 3890 -2852 -401 -2709 -2199 5064 3778 3235 a = 9 3 a = 9 3 a = 3 9 index1 = 3 index2 = 9

3   Pass
A = [-1 1 10 -10]; correct = [1 2]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

B = 10 2 11 -9 -2 10 9 -11 -11 -9 10 -20 9 11 20 10 a = 1 2 a = 1 2 a = 1 2 index1 = 1 index2 = 2

4   Pass
A = [0 1000 -2000 1001 0]; correct = [1 5]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

B = 1001 1000 -2000 1001 0 -1000 1001 -3000 1 -1000 2000 3000 1001 3001 2000 -1001 -1 -3001 1001 -1001 0 1000 -2000 1001 1001 a = 5 1 a = 5 1 a = 1 5 index1 = 1 index2 = 5

5   Pass
A = [1:1000 0.5]; correct = [1 1001]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

B = 1.0e+03 * Columns 1 through 18 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 0.0110 0.0120 0.0130 0.0140 0.0150 0.0160 0.0170 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 0.0110 0.0120 0.0130 0.0140 0.0150 0.0160 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 0.0110 0.0120 0.0130 0.0140 0.0150 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 0.0110 0.0120 0.0130 0.0140 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 0.0110 0.0120 0.0130 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 0.0110 0.0120 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 0.0110 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 0.0100 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 0.0090 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 0.0080 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 0.0070 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 0.0060 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 0.0050 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 0.0040 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 0.0030 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 0.0020 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 0.0010 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 1.0000 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0010 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0020 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0030 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0040 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0050 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0060 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0070 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0080 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0090 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0100 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0110 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0120 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0130 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0140 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0150 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0160 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0170 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0180 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0190 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0200 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0210 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0220 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0230 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0240 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0250 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0260 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0270 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0280 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0290 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0300 -0.0480 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0310 -0.0490 -0.0480 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0320 -0.0500 -0.0490 -0.0480 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0330 -0.0510 -0.0500 -0.0490 -0.0480 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0340 -0.0520 -0.0510 -0.0500 -0.0490 -0.0480 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0350 -0.0530 -0.0520 -0.0510 -0.0500 -0.0490 -0.0480 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 -0.0360 -0.0540 -0.0530 -0.0520 -0.0510 -0.0500 -0.0490 -0.0480 -0.0470 -0.0460 -0.0450 -0.0440 -0.0430 -0.0420 -0.0410 -0.0400 -0.0390 -0.0380 -0.0370 ...

6   Pass
% Area codes A = [847 217 508 312 212]; correct = [2 5]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

B = 847 -630 -339 -535 -635 630 847 291 95 -5 339 -291 847 -196 -296 535 -95 196 847 -100 635 5 296 100 847 a = 5 2 a = 5 2 a = 2 5 index1 = 2 index2 = 5

7   Pass
% Zip codes A = [60048 61802 01702 60601 10001]; correct = [1 4]; [i1 i2] = nearestNumbers(A); assert(isequal([i1 i2],correct))

B = 61802 1754 -58346 553 -50047 -1754 61802 -60100 -1201 -51801 58346 60100 61802 58899 8299 -553 1201 -58899 61802 -50600 50047 51801 -8299 50600 61802 a = 1 4 a = 1 4 a = 1 4 index1 = 1 index2 = 4