Is there a more efficient way to manipulate array A to get array B, considering the given variable "k"? I have come up with the following logic but believe there may be a better approach.
The length of the array is always divisible by k
This part is not the code. It doesn't let me submit it otherwise.
k=3
Given: A = [0,1,2,3,4,5,6,7,8,9,10,11]
Result: B = [9,10,11,6,7,8,3,4,5,0,1,2]
k = 4
Given: A = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
Result: B = [12,13,14,15,8,9,10,11,4,5,6,7,0,1,2,3]
The number of elements in A changes when k changes. The number of elements for A is always k*4. For example, if k is 2, our A array would be [0,1,2,3,4,5,6,7], and the resulting B array should be B = [6,7,4,5,2,3,0,1]
let A = [0,1,2,3,4,5,6,7,8,9,10,11]
k = 3
let B = []
for (let i=A.length - (k-1); i >=0; i=i-k) {
if (A.length - 1 <= i + k +1) {
B = [
...B,
...(A.slice(i+1, i+k+1))
]
}
}
B = [
...B,
...(A.slice(0,k))
]