yet another simple example
Code\n\nimport kotlin.math.max\n\nclass Solution {\n    fun sumImbalanceNumbers(nums: IntArray): Int {\n        val position = IntArray(nums.size + 2)\n        
# Code\n```\nimport kotlin.math.max\n\nclass Solution {\n    fun sumImbalanceNumbers(nums: IntArray): Int {\n        val position = IntArray(nums.size + 2)\n        position.fill(-1)\n        val left = nums\n                        .withIndex()\n                        .map{ (index,num) ->\n                            val l = max(position[num-1], position[num])\n                                position[num] = index\n                                l\n                            }\n        position.fill(nums.size)\n        val result = nums\n                        .foldRightIndexed(0) { index, num, acc ->\n                            val result = acc + (index - left[index]) * (position[num-1] - index)\n                                position[num] = index\n                                result\n                        }\n        return result - nums.size*(nums.size+1)/2\n    }\n}\n```
['Kotlin']