In this post, we have Swift 5.0 solution for Hackerrank exercise - Utopian Tree. We have find out the height of Utopian Tree, which goes through 2 cycles of growth every year. Each spring, it doubles in height. Each summer, its height increases by 1 meter.
Full details of exercise link hackerrank.com/challenges/utopian-tree/problem
Here is the working solution:
func utopianTree(n: Int) -> Int {
var height = 0
for cycle in 0...n {
height = (cycle - 1) % 2 == 0 ? height * 2 : height + 1
}
return height
}