Reversing array without changing the position of certain elements JavaScript


In this problem statement, our aim is to write the function for reversing the array without changing the position of certain elements with the help of Javascript. So for doing this task we will be keeping track of the indexes of the preserved elements. .

Understanding the problem statement

The problem is for creating a function to reverse an array in Javascript but with certain elements in the array must not be changed their position. So the positions of these items should be the same in both the original and reversed arrays.

For example let's say we have an array [1 ,2 ,3 ,4 ,5, 6]. If we want to reverse this array using the built -in function reverse in Javascript, we will get [6, 5, 4, 3, 2, 1]. But if we have to preserve the positions of the elements [2, 4, 6], then the reversed array will look like this [6, 5, 3, 4, 2, 1].

So our aim is to find the algorithm which can reverse the array by fulfilling the above requirements.

Logic for the above problem

For implementing the above problem there are different ways. Here the key idea is to keep track of the positions of the preserved items and swap their positions with their counterparts in the reversed array if needed.

The function will take the argument of array and an array of elements to preserve as inputs and return the new array that is the reverse of the mentioned array but with preserved elements. After doing this we will create a reversed copy of the array using the reverse method. Then iterates over the indexes of the original array and for every index check of the index refer to the preserved item's index. If the condition is true then simply go to the next elements without changing the position. If the condition is false then simply swap it to reverse it.

Algorithm

Step 1 − Create a function to reverse the elements of the array without changing the position of the preserved array.

Step 2 − Use a variable to store the indexes of the preserved elements.

Step 3 − Reverse the elements of the actual input array with the usage of reverse method.

Step 4 − Use a for loop to traverse the items of the input array using i. And inside the loop check the condition if the preserved index has the i index then continue.

Step 5 − If the above condition is not true then calculate the index in the reversed array with the help of arr.length - 1 -i and check if it belongs to a preserved item.

Step 6 − If it does not belong to then the algorithm will swap the items in their original positions with the help of destructuring assignment.

Code for the algorithm

function reverseArray(arr, preservePosition) {
   const preservedIndex = new Set(preservePosition.map((el) => arr.indexOf(el)));
   const reversedArr = arr.reverse();
   for (let i = 0; i < arr.length; i++) {
      if (preservedIndex.has(i)) {
      continue;
      }
      const reversedIndex = arr.length - 1 - i;
      if (preservedIndex.has(reversedIndex)) {
         // swap the elements in their original positions
         [reversedArr[i], reversedArr[reversedIndex]] = [arr[i], arr[reversedIndex]];
      }
   }
   return reversedArr;
}
const arr = [12, 22, 35, 44, 52, 35, 68];
const preservePosition = [22, 44, 68];
const reversedArr = reverseArray(arr, preservePosition);
console.log(reversedArr);

Complexity

The time complexity for the implemented algorithm is O(n) in which n is the length of the array. Because the algorithm performs a constant amount of work for every element in the array. The space complexity is also O(n) as we are only storing the reversed array of size n.

Conclusion

The above code provides an efficient solution to reverse an array without changing the actual position of some items in Javascript. So it has an O(n) time complexity and O(n) space complexity.

Updated on: 18-May-2023

440 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements