B
B
badman42352020-08-01 08:02:22
JavaScript
badman4235, 2020-08-01 08:02:22

How to rearrange the elements of an array from the end to the beginning?

There is a function that takes an array and a number n. The function must rearrange nthe elements of the array from the end to the beginning.
Example:

console.log(
  moveToStart ([1, 2, 3, 4, 5], 3)
); // [3, 4, 5, 1, 2]

Answer the question

In order to leave comments, you need to log in

4 answer(s)
P
Pavel, 2020-08-01
@badman4235

function moveToStart(array, n) {
    return [...array.slice(-n),...array.slice(0, array.length - n)]
}

I
Ivan Solomennikov, 2020-08-01
@ivsol

function moveToStart(arr, n) {

  if(n && n < arr.length) {

    while(n--)
      arr.unshift(arr.pop());
  }

  return arr;
}

console.log(moveToStart([1, 2, 3, 4, 5], 3)); // [3, 4, 5, 1, 2]

UPD : in this version, instead of iterations, the method is appliedslice
function moveToStart(arr, n) {

  const amount = arr.length - n;

  return arr.slice(amount).concat(arr.slice(0, amount));
}

console.log(moveToStart([1, 2, 3, 4, 5], 3)); // [3, 4, 5, 1, 2]

T
Tigran Abrahamyan, 2020-08-01
@TAbrahamyan

const moveToStart = (arr, n) => [ ...arr.splice(-n), ...arr ];

W
Wataru, 2020-08-01
@wataru

Most of the solutions already proposed here work for a square and already for n=10000 amount=5000 they will work for a noticeably long time. Of course, it is unlikely that such a large amount of data will be on the client, but even with n=100, if the function is executed often, why make it ~50 times slower?
The solution suggested by Paul is the best. It works fast and, like many algorithmically correct solutions, it is much shorter and more readable than other solutions.
If you want without splice(), then you need to create a new array and then copy the elements n-amount..n-1 into it at the beginning with one cycle, then copy the elements 0..n-amount-1 further with the second cycle.
The interface you fixed suggests using a new array for the results each time. But it could be done without using a lot of additional memory, hanging the array itself in place, if we allow the interface to change.
True, splice would no longer be usable. Still it would be necessary to know the theory of permutations a little. In short, here is a solution that works behind the line and shuffles the array in place:

function gcd(a,b) {
 if (a>b) return gcd(b,a);
 return a == 0? b : gcd(b%a, a);
}

function next(i, k) {
  return i >= k ? i-k : i+n-k
}

function moveToStartInPlace(arr, k) {
  n = arr.length;
  num_cycles = gcd(k,n)
  for (i=0; i < num_cycles; ++i) {
   saved_value = arr[i];
   j = i;
   nxt = next(j, k);
   while (nxt != i) { 
     arr[j] = arr[nxt];
     j = nxt;
     nxt = next(j, k);
   }
   arr[j] = saved_value;
  }
}

It might not be the best implementation - I'm not a JS expert. The essence of this solution is that we take some element and put the desired element in its place after the permutation. We put the next element in the vacated place, and so on, until we return to the starting position (and we will always return, because permutations only work that way).
The next() function returns which element should replace the i-th element in the resulting array.
If you think a little, it becomes clear that there are exactly gcd(n,k) cycles in the permutation, because in it all jumps are made on +(nk) or -k. Those. actually only jumps -k(mod n) are done. And this is already a well-known fact: if we jump along the array by +k, turning around from the end to the beginning, then we will return to the same point from which we started, and there will be gcd(n,k) of such cycles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question