Lädt...

🔧 2460. Apply Operations to an Array


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

2460. Apply Operations to an Array

Difficulty: Easy

Topics: Array, Two Pointers, Simulation

You are given a 0-indexed array nums of size n consisting of non-negative integers.

You need to apply n - 1 operations to this array where, in the ith operation (0-indexed), you will apply the following on the ith element of nums:

  • If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation.

After performing all the operations, shift all the 0's to the end of the array.

  • For example, the array [1,0,2,0,0,1] after shifting all its 0's to the end, is [1,2,1,0,0,0].

Return the resulting array.

Note that the operations are applied sequentially, not all at once.

Example 1:

  • Input: nums = [1,2,2,1,1,0]
  • Output: [1,4,2,0,0,0]
  • Explanation: We do the following operations:
    • i = 0: nums[0] and nums[1] are not equal, so we skip this operation.
    • i = 1: nums[1] and nums[2] are equal, we multiply nums[1] by 2 and change nums[2] to 0. The array becomes [1,4,0,1,1,0].
    • i = 2: nums[2] and nums[3] are not equal, so we skip this operation.
    • i = 3: nums[3] and nums[4] are equal, we multiply nums[3] by 2 and change nums[4] to 0. The array becomes [1,4,0,2,0,0].
    • i = 4: nums[4] and nums[5] are equal, we multiply nums[4] by 2 and change nums[5] to 0. The array becomes [1,4,0,2,0,0].
    • After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0].

Example 2:

  • Input: nums = [0,1]
  • Output: [1,0]
  • Explanation: No operation can be applied, we just shift the 0 to the end.

Constraints:

  • 2 <= nums.length <= 2000
  • 0 <= nums[i] <= 1000

Hint:

  1. Iterate over the array and simulate the described process.

Solution:

We need to apply a series of operations on an array of non-negative integers and then shift all zeros to the end of the array. The operations are applied sequentially, and each operation affects subsequent steps. Let's break down the approach and solution step-by-step.

Approach

  1. Apply Operations Sequentially:

    • Iterate through the array from the first element to the second-to-last element.
    • For each element, check if it is equal to the next element. If they are equal, multiply the current element by 2 and set the next element to 0.
    • This step modifies the array in place, affecting subsequent operations.
  2. Shift Zeros to the End:

    • After applying all operations, collect all non-zero elements in their original order.
    • Append the required number of zeros to the end of the collected non-zero elements to maintain the original array length.

Let's implement this solution in PHP: 2460. Apply Operations to an Array

<?php
/**
 * @param Integer[] $nums
 * @return Integer[]
 */
function applyOperations($nums) {
    ...
    ...
    ...
    /**
     * go to ./solution.php
     */
}

// Example test cases
$nums1 = [1,2,2,1,1,0];
$nums2 = [0,1];

print_r(applyOperations($nums1)); // Output: [1, 4, 2, 0, 0, 0]
print_r(applyOperations($nums2)); // Output: [1, 0]
?>

Explanation:

  1. Applying Operations:

    • We loop through each element from index 0 to the second-to-last element. For each element, if it is equal to the next element, we double the current element and set the next element to 0. This ensures that the operations are applied in sequence, modifying the array as we go.
  2. Shifting Zeros:

    • After processing all operations, we create a new array to collect non-zero elements. This helps in maintaining the order of non-zero elements while efficiently moving zeros to the end.
    • We then append zeros to the end of the collected non-zero elements to match the original array's length, ensuring all zeros are shifted to the end.

This approach efficiently handles the operations and shifting in linear time, making it optimal for the given problem constraints.

Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

...

🔧 2460. Apply Operations to an Array


📈 58.76 Punkte
🔧 Programmierung

🔧 Apply Operations to an Array


📈 30.16 Punkte
🔧 Programmierung

🔧 Upcoming JavaScript Features: Simplifying Array Combinations with `Array.zip` and `Array.zipKeyed`


📈 29.27 Punkte
🔧 Programmierung

🕵️ CVE-2024-2460 | GamiPress Plugin up to 1.0.7 on WordPress Shortcode cross site scripting


📈 28.6 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2022-2460 | WPDating Plugin up to 7.1.9 on WordPress sql injection


📈 28.6 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2022-2460


📈 28.6 Punkte
🕵️ Sicherheitslücken

🕵️ CVE-2016-2460 | Google Android 4.4.3/5.0.1/5.1.0 Mediaserver information disclosure


📈 28.6 Punkte
🕵️ Sicherheitslücken

🔧 Turbo Array: Supercharge Your JavaScript Array Operations 🚀


📈 27.56 Punkte
🔧 Programmierung

🔧 Minimum operations required to Sort the Array using following operations


📈 25.84 Punkte
🔧 Programmierung

🐧 Apply Yaml Manifest Using “kubectl apply”


📈 24.73 Punkte
🐧 Linux Tipps

🔧 #2635 Leetcode Apply Transform Over Each Element in Array


📈 22.12 Punkte
🔧 Programmierung

🔧 2818. Apply Operations to Maximize Score


📈 20.4 Punkte
🔧 Programmierung

🕵️ FFmpeg Array Access MXF File Out-of-Array Pufferüberlauf


📈 19.52 Punkte
🕵️ Sicherheitslücken

🔧 Generate an Array from given Array according to given conditions


📈 19.52 Punkte
🔧 Programmierung

🔧 #123 — Convert One-Dimensional Array to Two-Dimensional Array


📈 19.52 Punkte
🔧 Programmierung

💾 Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


📈 19.52 Punkte
💾 IT Security Tools

🔧 JS Sum of an Array – How to Add the Numbers in a JavaScript Array


📈 19.52 Punkte
🔧 Programmierung

⚠️ Microsoft Edge Chakra JIT Array.prototype.reverse Array Type Confusion


📈 19.52 Punkte
⚠️ PoC

🔧 JavaScript Array Tutorial – Array Methods in JS


📈 19.52 Punkte
🔧 Programmierung

🔧 std::array in C++ isn't slower than array in C


📈 19.52 Punkte
🔧 Programmierung

⚠️ #0daytoday #Microsoft Edge Chakra JIT - Array.prototype.reverse Array Type Confusion Exploit [#0day #Exploit]


📈 19.52 Punkte
⚠️ PoC

🐧 ES6 Map an Array of Objects to Return an Array of Objects With New Keys


📈 19.52 Punkte
🐧 Linux Tipps

⚠️ [dos] Microsoft Edge Chakra JIT - 'Array.prototype.reverse' Array Type Confusion


📈 19.52 Punkte
⚠️ PoC

🔧 How to Create an Array in Java – Array Declaration Example


📈 19.52 Punkte
🔧 Programmierung

🔧 2022. Convert 1D Array Into 2D Array


📈 19.52 Punkte
🔧 Programmierung

🔧 JavaScript Sort Array - How to Sort an Array Accurately


📈 19.52 Punkte
🔧 Programmierung

🔧 Array.slice vs. Array.splice: Clearing Up the Confusion


📈 19.52 Punkte
🔧 Programmierung

🐧 Convert 1d Array to 2d Array Python


📈 19.52 Punkte
🐧 Linux Tipps

🔧 Find Array Element in the Array using JavaScript


📈 19.52 Punkte
🔧 Programmierung

🔧 How to pass array in another array of objects?


📈 19.52 Punkte
🔧 Programmierung

🔧 Find Array Element in the Array using JavaScript


📈 19.52 Punkte
🔧 Programmierung

matomo