I'm creating all possible combinations of 1D array to 2D array, but it seems my code is too slow to execute. I have a feeling that I could still speed it up.
let arr = [2, 2, 1]
let k = 5
var tempArray = [[Int]]()
for i in 0..<arr.count {
for j in 0..<arr.count {
tempArray.append([arr[i], arr[j]])
}
}
let sortedArray = tempArray.sorted {
$0[0] == $1[0] ? $0[1] < $1[1] : $0[0] < $1[0]
}
print(sortedArray[k-1])