Beats 100% | Kotlin | Sliding Window
Approach\nSliding window\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n\nclass Solution {\n    fun minSumOfLengths(arr: IntA
# Approach\nSliding window\n\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(n)\n\n# Code\n```\nclass Solution {\n    fun minSumOfLengths(arr: IntArray, target: Int): Int {\n        var sum = 0\n        var left = 0\n        var right = 0\n        var ans = Int.MAX_VALUE\n        var minTillNow = Int.MAX_VALUE\n        val resArr = IntArray(arr.size)\n        while (right < arr.size) {\n            sum += arr[right++]\n            while (sum > target) {\n                sum -= arr[left++]\n            }\n            if (sum == target) {\n                if (left > 0 && resArr[left - 1] != Int.MAX_VALUE) {\n                    ans = Math.min(ans, right - left + resArr[left - 1])\n                }\n                minTillNow = Math.min(minTillNow, right - left)\n            }\n            resArr[right - 1] = minTillNow\n        }\n       \n        if (ans == Int.MAX_VALUE) return -1\n        return ans\n    }\n}\n```
['Kotlin']