You are viewing a single comment's thread. Return to all comments →
Swift solution: There are only 2 cases: k == 0 n%(2*k) == 0
k == 0
n%(2*k) == 0
Exp n=12 k=3: 4 5 6 1 2 3 10 11 12 7 8 9
n=12
k=3
4 5 6 1 2 3 10 11 12 7 8 9
It will loop in 2 sub: [4 5 6 1 2 3] & [10 11 12 7 8 9]
[4 5 6 1 2 3] & [10 11 12 7 8 9]
func absolutePermutation(n: Int, k: Int) -> [Int] { // Write your code here if k == 0 { var results:[Int] = [] for i in 1...n { results.append(i) } return results } else if n%(2*k) == 0 { var results:[Int] = [] let twoK = 2*k // Exp 12 3: 4 5 6 1 2 3 10 11 12 7 8 9 for i in 0..<n/twoK { let start = i*twoK + 1 let end = (i+1)*twoK let mid = (end + start)/2 + 1 if mid <= end { for j in mid...end { results.append(j) } } for j in start..<mid { results.append(j) } } return results } return [-1] }
Seems like cookies are disabled on this browser, please enable them to open this website
Absolute Permutation
You are viewing a single comment's thread. Return to all comments →
Swift solution: There are only 2 cases:
k == 0
n%(2*k) == 0
Exp
n=12
k=3
:4 5 6 1 2 3 10 11 12 7 8 9
It will loop in 2 sub:
[4 5 6 1 2 3] & [10 11 12 7 8 9]