Last active
October 16, 2021 07:52
-
-
Save teacons/f1d49f90100b22f6e56af0101a0406bc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.fbear.arithmetic | |
import kotlin.math.abs | |
class Find { | |
fun findNearNum(nums: List<Int>, givenNum: Int): Int { | |
var near = -1 | |
var minDiff = Int.MAX_VALUE | |
for (num in nums) { | |
if (num == givenNum) return num | |
else { | |
val diff = abs(givenNum - num) | |
if (diff < minDiff) { | |
minDiff = diff | |
near = num | |
} | |
} | |
} | |
return near | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment