Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const rotate = (array, a, b, c) => {
- for (;;) {
- const n1 = b - a;
- const n2 = c - b;
- if (n1 === 0 || n2 === 0) break;
- const len = c - a;
- const min = n1 < n2? n1: n2;
- const max = len - min;
- if (n1 === max) {
- const end = c - 1 - n1;
- for (let i=c-1; i>end; --i) {
- const j = i - n2;
- const aux = array[i];
- array[i] = array[j];
- array[j] = aux;
- }
- } else {
- const end = a + n2;
- for (let i=a; i<end; ++i) {
- const j = i + n1;
- const aux = array[i];
- array[i] = array[j];
- array[j] = aux;
- }
- }
- const rotations = max % min;
- if (rotations === 0) break;
- if (n1 === max) {
- c = a + min;
- b = a + rotations;
- } else {
- a += max;
- b = c - rotations;
- }
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement