Add sum of two and its solution

This commit is contained in:
p1ng0ut 2023-02-06 20:45:35 +01:00
parent 18c1cccac1
commit 08a62d283e
3 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,14 @@
package space.dezentrale.prgrnd
class SumOfTwo {
fun twoSum(nums: IntArray, target: Int): IntArray {
nums.forEachIndexed { idxA, numA ->
nums.forEachIndexed { idxB, numB ->
if (idxA != idxB && numA + numB == target) {
return intArrayOf(idxA, idxB)
}
}
}
return intArrayOf(-1, -1)
}
}

View File

@ -4,7 +4,7 @@ import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class ContainsDuplicateTest {
class DuplicatesTest {
@Test
fun `it returns true for '1_2_3_1'`() {
assertTrue(containsDuplicate(intArrayOf(1, 2, 3, 1)))

View File

@ -0,0 +1,33 @@
package space.dezentrale.prgrnd
import kotlin.test.Test
import kotlin.test.assertContentEquals
class SumOfTwoTest {
@Test
fun `it returns an array with 0 and 1 for 2_7_11_15 and 9`() {
val sumOfTwo = SumOfTwo()
val twoSum = sumOfTwo.twoSum(intArrayOf(2, 7, 11, 15), 9)
assertContentEquals(twoSum, intArrayOf(0, 1))
}
@Test
fun `it returns an array with 1 and 2 for 3_2_4 and 6`() {
val sumOfTwo = SumOfTwo()
val twoSum = sumOfTwo.twoSum(intArrayOf(3, 2, 4), 6)
assertContentEquals(twoSum, intArrayOf(1, 2))
}
@Test
fun `it returns an array with 0 and 1 for 3_3 and 6`() {
val sumOfTwo = SumOfTwo()
val twoSum = sumOfTwo.twoSum(intArrayOf(3, 3), 6)
assertContentEquals(twoSum, intArrayOf(0, 1))
}
}