Search Your Question

Write a program to find missing number of array of 1 to n.

Ans : 


      import Foundation
        
        let arr = [1,2,4,5,8,6,9,7]
        let n = arr.count
        let total = ((n + 1)*(n + 2))/2
        var missingNum = total
        for item in arr {
            missingNum = missingNum - item
        }
        print(missingNum)

No comments:

Post a Comment

Thanks