Skip to content

Instantly share code, notes, and snippets.

@teacons
Last active October 16, 2021 07:52
Show Gist options
  • Save teacons/f1d49f90100b22f6e56af0101a0406bc to your computer and use it in GitHub Desktop.
Save teacons/f1d49f90100b22f6e56af0101a0406bc to your computer and use it in GitHub Desktop.
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