Set Mismatch

To solve this problem, you need to know the arithmetic progression formula.

class Solution {
    fun findErrorNums(nums: IntArray): IntArray {
        val n = nums.size
        val sum = (n * (n + 1))/2
        val sortedNum = nums.sorted()
        var dup = 0
        var errorSum = sortedNum[0]
        for(i in 1 until sortedNum.size){
            if(sortedNum[i] == sortedNum[i-1]) {
                dup = sortedNum[i]
            }else errorSum += sortedNum[i]            
        }
        
        return intArrayOf(dup, sum - errorSum)
    }
}
Runtime: 364 ms, faster than 25.00% of Kotlin online submissions for Set Mismatch.
Memory Usage: 39.5 MB, less than 100.00% of Kotlin online submissions for Set Mismatch.
comments powered by Disqus