|
| 1 | +=begin |
| 2 | +Codewars. 28/04/20. 'Number Zoo Patrol'. 6kyu. Here we create a method that |
| 3 | +takes an array of numbers from 1 to n, with one missing element, and returns |
| 4 | +the missing number. The missing number is not necessarily in between the numbers |
| 5 | +in the array, it can be before position [0] or after position [-1]. Here is the |
| 6 | +solution I developed to solve the challenge. |
| 7 | +1) We define our method find_missing_number_ms, which takes nums, an array of |
| 8 | + numbers from 1 to n - with one number missing - as its argument. |
| 9 | +2) We create a range from 1 to the size of nums plus 1, then sum this range, we |
| 10 | + sum nums, then we subtract the difference, the difference is the missing |
| 11 | + number. |
| 12 | +3) This algorithm is doable because nums always starts at 1 and the gap |
| 13 | + between values is always 1, so the last value in nums - with the missing |
| 14 | + number placed in - will always be the size without the missing + 1. |
| 15 | +=end |
| 16 | + |
| 17 | +def find_missing_number_ms(nums) |
| 18 | + (1..nums.size+1).sum - nums.sum |
| 19 | +end |
| 20 | + |
| 21 | +=begin |
| 22 | +Here is another algorithm that can be used to solve the challenge. |
| 23 | +1) If nums were [2,3,4], n would be 4. n plus 1 would be 5, divided by 2 would |
| 24 | + be 2.5. n multiplied by 2.5 would be 10. nums.sum would be 9. So 10 minus 9 |
| 25 | + would be 1 - the missing number. |
| 26 | +2) If nums were [1,2,3,4,5,6,7,8], n would be 9. 9 plus 1 would be 10, divided |
| 27 | + by 2 would be 5. n multiplied by 5 would be 45. nums.sum would be 36. So 45 |
| 28 | + minus 36 would give us the missing number, 9. |
| 29 | +=end |
| 30 | + |
| 31 | +def find_missing_number(nums) |
| 32 | + n = nums.size + 1 ; n * (n + 1) / 2 - nums.sum |
| 33 | +end |
0 commit comments