Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ slug.

| [1 kyu](/kata/1-kyu/index.md) | [2 kyu](/kata/2-kyu/index.md) | [3 kyu](/kata/3-kyu/index.md) | [4 kyu](/kata/4-kyu/index.md) | [5 kyu](/kata/5-kyu/index.md) | [6 kyu](/kata/6-kyu/index.md) | [7 kyu](/kata/7-kyu/index.md) | [8 kyu](/kata/8-kyu/index.md) | [beta](/kata/beta/index.md) | [retired](/kata/retired/index.md) |
|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:-----------------------------:|:---------------------------:|:---------------------------------:|
| 0 | 1 | 2 | 26 | 48 | 441 | 605 | 227 | 56 | 82 |
| 0 | 1 | 2 | 26 | 48 | 442 | 605 | 227 | 56 | 82 |

**Note:** The source code is written in Java 17 and may use language features that are incompatible
with Java 8, 11.
Expand Down
1 change: 1 addition & 0 deletions kata/6-kyu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
- [Simple ROT13.5 cypher](simple-rot13-dot-5-cypher)
- [Simple square numbers](simple-square-numbers)
- [Simple string indices](simple-string-indices)
- [Simple sum of pairs](simple-sum-of-pairs)
- [Simple time difference](simple-time-difference)
- [Simpson's Rule - Approximate Integration](simpsons-rule-approximate-integration)
- [Single character palindromes](single-character-palindromes)
Expand Down
20 changes: 20 additions & 0 deletions kata/6-kyu/simple-sum-of-pairs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# [Simple sum of pairs](https://www.codewars.com/kata/simple-sum-of-pairs "https://www.codewars.com/kata/5bc027fccd4ec86c840000b7")

Given an integer `n`, find two integers `a` and `b` such that:

```
A) a >= 0 and b >= 0
B) a + b = n
C) DigitSum(a) + Digitsum(b) is maximum of all possibilities.
```

You will return the digitSum(a) + digitsum(b).

```
For example:
solve(29) = 11. If we take 15 + 14 = 29 and digitSum = 1 + 5 + 1 + 4 = 11. There is no larger outcome.
```

`n` will not exceed `10e10`.

More examples in test cases.
9 changes: 9 additions & 0 deletions kata/6-kyu/simple-sum-of-pairs/main/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import java.util.function.LongToIntFunction;

interface Solution {
static int solve(long n) {
LongToIntFunction sum = l -> (l + "").chars().map(c -> c - 48).sum();
long a = (long) Math.pow(10, Math.floor(Math.log10(n))) - 1;
return (int) (n > 1 ? sum.applyAsInt(a) + sum.applyAsInt(n - a) : n);
}
}
21 changes: 21 additions & 0 deletions kata/6-kyu/simple-sum-of-pairs/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

class SolutionTest {
@ParameterizedTest
@CsvSource(textBlock = """
0, 0
1, 1
18, 18
29, 11
1140, 33
50000000, 68
458453062, 109
8130375074, 119
""")
void sample(long n, int sum) {
assertEquals(sum, Solution.solve(n));
}
}