Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 149fc26

Browse files
authored
Create 1846-maximum-element-after-decreasing-and-rearranging.kt
1 parent 5bb0e84 commit 149fc26

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
fun maximumElementAfterDecrementingAndRearranging(arr: IntArray): Int {
3+
arr.sort()
4+
var prev = 0
5+
6+
for (n in arr) {
7+
prev = minOf(prev + 1, n)
8+
}
9+
10+
return prev
11+
}
12+
}
13+
14+
// same as above but using Kotlin's Aggregate operation fold()
15+
class Solution {
16+
fun maximumElementAfterDecrementingAndRearranging(arr: IntArray) = arr.sorted()
17+
.fold(0) { acc, num -> minOf(acc + 1, num) }
18+
}

0 commit comments

Comments
 (0)