0

I've solved one of Leetcode problems in codepen, but when I try to submit it on leetcode I get a different result than what I get in codepen.

The problem is:

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

Input: nums = [1,2,3,4,5,6,7], k = 3
Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Link (requires login) https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/646/

My solution:

var rotate = function(nums, k) {
  var a = nums.splice(0 , nums.length-k);
  var b = nums.splice(-k);
  var c = [...b , ...a ];
  return(c);
};

Using the above example, running this code on codepen returns (or console.logs) [5,6,7,1,2,3,4].

But when I run this code in leetcode, I get an empty array [].

Any ideas why this could be the happening?

4
  • Read carefully: @return {void} Do not return anything, modify nums in-place instead. Commented Dec 22, 2022 at 17:11
  • Also .slice and .splice are completely different. Commented Dec 22, 2022 at 17:12
  • Using splice mutates the array, using slice doesn't. Take a look at which one you need as the first nums.splice is changing the content of your original array. Take a look at the docs here: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 22, 2022 at 17:13
  • Hint: you need exactly one .splice to solve the problem ;) Commented Dec 22, 2022 at 17:15

2 Answers 2

1

It looks like you're supposed to rotate the original array, not return a new rotated array. So you need to set nums = [...b, ...a] instead.

EDIT: Since JavaScript passes by value, the nums parameter just holds a reference to the original array, so doing nums = [] will only change the nums variable to reference a different array, without changing the original array. You'll want to call methods of the original array to mutate it. E.g. .splice

/EDIT

Also, have you thought about what happens when k is greater than the length of the array? E.g. nums = [1, 2, 3], k = 5

Also also, your link to leetcode requires login, so not everyone will be able to view it.

Sign up to request clarification or add additional context in comments.

3 Comments

I did: nums = [...b , ...a ]; return(nums); and got the same results
Assigning to nums doesn't modify the original array. JS parameters are passed by value, not by reference.
@Barmar Thanks, that always gets me.
1

Example A

.pop() removes the last element of an array and returns it. .unshift() the returned value of .pop() to the first index of the array. Do that k times in a for loop. .pop() and .unshift() changes (aka mutates) the original array.

let arr = [1, 2, 3, 4, 5, 6, 7], k = 3;

function rotate(array, times) {
  for (let i=0; i < times; i++) {
    let last = array.pop();
    array.unshift(last);
  }
  return array;
}

console.log(rotate(arr, k));
  

Example B

By changing .splice() to .slice() you can implement your logic since .slice() creates a shallow copy of the array and does not mutate the original array like .splice(). Since the original array is unchanged, any references to said array are consistent. Also, concerning the case mentioned in LawrenceWebDev's answer -- if k is greater than the length of the array -- k (times) becomes the remainder of k/array.length (times % size).

let arr = [1, 2, 3, 4, 5, 6, 7], k = 3;

function rotate(array, times) {
  const size = array.length;
  if (times > size) {
    times = times % size;
  }
  let a = array.slice(-times);
  let b = array.slice(0, size - times);
  return [...a, ...b];
}

console.log(rotate(arr, k));

7 Comments

This is an O(N^2) algorithm because unshift() is O(N), so it probably will get a TLE at LeetCode.
@Barmar yes, this does give me the correct output when I "run code" on leetcode, but I get a TLE error when I "submit" the solution.
O(N) is how I think -- methodically slow 😄
@Baslki just updated answer, try Example B which goes along with the OP logic.
@zer00ne I appreciate your time, however Example B returns [5,6,7,1,2,3,4] in CodePen, but return [1,2,3,4,5,6,7] in Leetcode.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.