[Programmers / JAVA] Level 1 Adding Missing Numbers (86051)
[Programmers / JAVA] Level 1 Adding Missing Numbers (86051)
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
You're given an array numbers containing some of the digits from 0 to 9 as a parameter. Complete the solution function so that it finds all the digits from 0 to 9 that don't appear in numbers, and returns their sum.
- 1 ≤ length of numbers ≤ 9
- 0 ≤ every number in numbers ≤ 9
- Every number in numbers is distinct.
| numbers | result |
|---|---|
| { 1, 2, 3, 4, 6, 7, 8, 0 } | 14 |
| { 5, 8, 4, 0, 6, 7, 9 } | 6 |
Input/Output Example #1
Since 5 and 9 are not in numbers, it should return 5 + 9 = 14.
Input/Output Example #2
Since 1, 2, 3 are not in numbers, it should return 1 + 2 + 3 = 6.
We're given an array containing digits from 0 to 9 without duplicates. Our goal is to find the sum of the numbers not included in this array.
Since the digits are limited to single digits and there are no duplicates, this can be solved very simply.
The total sum of 0 to 9 is 45. If we subtract all the values of numbers from 45, we can easily find the sum of the numbers not included.
JAVA
/** * Adding Missing Numbers class * * @author RWB * @since 2021.12.10 Fri 00:04:47 */ class Solution { /** * Method that returns the answer * * @param numbers: [int[]] The number array * * @return [int] The answer */ public int solution(int[] numbers) { int answer = 45; for (int number : numbers) { answer -= number; } return answer; } }
