Created
February 21, 2020 08:50
-
-
Save bugrym/3d81f4e0c73007894cfddd9928ff1a4b to your computer and use it in GitHub Desktop.
1281. Subtract the Product and Sum of Digits of an Integer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func subtractProductAndSum(_ n: Int) -> Int { | |
var givenNum = n | |
var num:[Int] = [] | |
if n >= 1 && n <= 100000 { | |
repeat { | |
let digit = givenNum%10 | |
givenNum /= 10 | |
num.append(digit) | |
} while (givenNum != 0) | |
let product = num.map({$0}).reduce(1, *) | |
let sum = num.map({$0}).reduce(0, +) | |
return product - sum | |
} | |
return 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment