Easy
You are given an integer array nums
.
Return the smallest absent positive integer in nums
such that it is strictly greater than the average of all elements in nums
.
The average of an array is defined as the sum of all its elements divided by the number of elements.
Example 1:
Input: nums = [3,5]
Output: 6
Explanation:
nums
is (3 + 5) / 2 = 8 / 2 = 4
.Example 2:
Input: nums = [-1,1,2]
Output: 3
Explanation:
nums
is (-1 + 1 + 2) / 3 = 2 / 3 = 0.667
.Example 3:
Input: nums = [4,-1]
Output: 2
Explanation:
nums
is (4 + (-1)) / 2 = 3 / 2 = 1.50
.Constraints:
1 <= nums.length <= 100
-100 <= nums[i] <= 100
class Solution {
fun smallestAbsent(nums: IntArray): Int {
var sum = 0
for (j in nums) {
sum += j
}
val avg = sum.toDouble() / nums.size
var num: Int
if (avg < 0) {
num = 1
} else {
num = avg.toInt() + 1
}
while (true) {
var flag = false
for (j in nums) {
if (num == j) {
flag = true
break
}
}
if (!flag && num > avg) {
return num
}
num++
}
}
}