This will do the work.
or you can do stuff like
if(n==1 || n==2 || n==4 || n== 8 || ...)
if(n==3 || n==5 || n==6)
but the point is to come up with the algorithm!
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.