From 5a0f1a0a6fde31bda63dccac8105f3816e045a05 Mon Sep 17 00:00:00 2001 From: aadil42 <77232799+aadil42@users.noreply.github.com> Date: Sat, 1 Mar 2025 16:22:33 +0530 Subject: [PATCH] Create 2460-apply-operations-to-an-array.js Solved apply-operations-to-an-array --- .../2460-apply-operations-to-an-array.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 javascript/2460-apply-operations-to-an-array.js diff --git a/javascript/2460-apply-operations-to-an-array.js b/javascript/2460-apply-operations-to-an-array.js new file mode 100644 index 000000000..0fd441899 --- /dev/null +++ b/javascript/2460-apply-operations-to-an-array.js @@ -0,0 +1,19 @@ +/** + * Array | Simulation + * Time O(n) | Space O(n) + * https://leetcode.com/problems/apply-operations-to-an-array + * @param {number[]} nums + * @return {number[]} + */ +var applyOperations = function(nums) { + + for (let i = 1; i < nums.length; i++) { + if (nums[i] === nums[i-1]) { + nums[i-1] = nums[i-1] * 2; + nums[i] = 0; + } + } + + const nonZeros = nums.filter((num) => num !== 0); + return [...nonZeros, ...new Array(nums.length - nonZeros.length).fill(0)]; +};