|
| 1 | +--- |
| 2 | +id: Apply-Operations-to-an-Array |
| 3 | +title: Apply Operations to an Array |
| 4 | +sidebar_label: 2460-Apply Operations to an Array |
| 5 | +tags: [dsa, leetcode] |
| 6 | +description: Problem solution of Apply Operations to an Array |
| 7 | +--- |
| 8 | + |
| 9 | +## Problem Statement |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +You are given a 0-indexed array nums of size n consisting of non-negative integers. |
| 14 | + |
| 15 | +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: |
| 16 | + |
| 17 | +If nums[i] == nums[i + 1], then multiply nums[i] by 2 and set nums[i + 1] to 0. Otherwise, you skip this operation. |
| 18 | +After performing all the operations, shift all the 0's to the end of the array. |
| 19 | + |
| 20 | +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]. |
| 21 | +Return the resulting array. |
| 22 | + |
| 23 | +Note that the operations are applied sequentially, not all at once. |
| 24 | + |
| 25 | +### Examples |
| 26 | + |
| 27 | +#### Example 1 |
| 28 | +``` |
| 29 | +Input: nums = [1,2,2,1,1,0] |
| 30 | +Output: [1,4,2,0,0,0] |
| 31 | +Explanation: We do the following operations: |
| 32 | +- i = 0: nums[0] and nums[1] are not equal, so we skip this operation. |
| 33 | +- 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]. |
| 34 | +- i = 2: nums[2] and nums[3] are not equal, so we skip this operation. |
| 35 | +- 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]. |
| 36 | +- 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]. |
| 37 | +After that, we shift the 0's to the end, which gives the array [1,4,2,0,0,0]. |
| 38 | +
|
| 39 | +``` |
| 40 | + |
| 41 | +### Example 2 |
| 42 | +``` |
| 43 | +Input: nums = [0,1] |
| 44 | +Output: [1,0] |
| 45 | +Explanation: No operation can be applied, we just shift the 0 to the end. |
| 46 | +
|
| 47 | +``` |
| 48 | +### Constraints |
| 49 | + |
| 50 | +- `2 <= nums.length <= 2000` |
| 51 | +- `0 <= nums[i] <= 1000` |
| 52 | + |
| 53 | +## Solution of Given Problem |
| 54 | + |
| 55 | +### Intuition and Approach |
| 56 | + |
| 57 | +The problem can be solved using a brute force approach or an optimized Technique. |
| 58 | + |
| 59 | +<Tabs> |
| 60 | +<tabItem value="Brute Force" label="Brute Force"> |
| 61 | + |
| 62 | +### Approach 1:Brute Force (Naive) |
| 63 | + |
| 64 | + |
| 65 | +Brute Force Approach: |
| 66 | +Iterate through the array and for each i, check if nums[i] is equal to nums[i + 1]. |
| 67 | +If they are equal, multiply nums[i] by 2 and set nums[i + 1] to 0. |
| 68 | +After all operations are performed, create a new array by collecting all non-zero elements followed by all zero elements. |
| 69 | +#### Codes in Different Languages |
| 70 | + |
| 71 | +<Tabs> |
| 72 | +<TabItem value="C++" label="C++" default> |
| 73 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 74 | + |
| 75 | +```cpp |
| 76 | +#include <iostream> |
| 77 | +#include <vector> |
| 78 | + |
| 79 | +std::vector<int> applyOperations(std::vector<int>& nums) { |
| 80 | + int n = nums.size(); |
| 81 | + |
| 82 | + for (int i = 0; i < n - 1; ++i) { |
| 83 | + if (nums[i] == nums[i + 1]) { |
| 84 | + nums[i] *= 2; |
| 85 | + nums[i + 1] = 0; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + std::vector<int> result; |
| 90 | + for (int num : nums) { |
| 91 | + if (num != 0) { |
| 92 | + result.push_back(num); |
| 93 | + } |
| 94 | + } |
| 95 | + while (result.size() < n) { |
| 96 | + result.push_back(0); |
| 97 | + } |
| 98 | + |
| 99 | + return result; |
| 100 | +} |
| 101 | + |
| 102 | +int main() { |
| 103 | + std::vector<int> nums = {1, 2, 2, 1, 1, 0}; |
| 104 | + std::vector<int> result = applyOperations(nums); |
| 105 | + for (int num : result) { |
| 106 | + std::cout << num << " "; |
| 107 | + } |
| 108 | + std::cout << std::endl; // Output: 1 4 2 0 0 0 |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +``` |
| 113 | +</TabItem> |
| 114 | +<TabItem value="Java" label="Java"> |
| 115 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 116 | +
|
| 117 | +```java |
| 118 | +import java.util.*; |
| 119 | +
|
| 120 | +public class ApplyOperations { |
| 121 | + public static int[] applyOperations(int[] nums) { |
| 122 | + int n = nums.length; |
| 123 | + |
| 124 | + for (int i = 0; i < n - 1; i++) { |
| 125 | + if (nums[i] == nums[i + 1]) { |
| 126 | + nums[i] *= 2; |
| 127 | + nums[i + 1] = 0; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + int[] result = new int[n]; |
| 132 | + int idx = 0; |
| 133 | + for (int num : nums) { |
| 134 | + if (num != 0) { |
| 135 | + result[idx++] = num; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + return result; |
| 140 | + } |
| 141 | +
|
| 142 | + public static void main(String[] args) { |
| 143 | + int[] nums = {1, 2, 2, 1, 1, 0}; |
| 144 | + int[] result = applyOperations(nums); |
| 145 | + System.out.println(Arrays.toString(result)); // Output: [1, 4, 2, 0, 0, 0] |
| 146 | + } |
| 147 | +} |
| 148 | +
|
| 149 | +``` |
| 150 | + |
| 151 | + |
| 152 | +</TabItem> |
| 153 | +<TabItem value="Python" label="Python"> |
| 154 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 155 | + |
| 156 | +```python |
| 157 | +def apply_operations(nums): |
| 158 | + n = len(nums) |
| 159 | + |
| 160 | + for i in range(n - 1): |
| 161 | + if nums[i] == nums[i + 1]: |
| 162 | + nums[i] *= 2 |
| 163 | + nums[i + 1] = 0 |
| 164 | + |
| 165 | + result = [num for num in nums if num != 0] |
| 166 | + result.extend([0] * (n - len(result))) |
| 167 | + |
| 168 | + return result |
| 169 | + |
| 170 | +nums = [1, 2, 2, 1, 1, 0] |
| 171 | +print(apply_operations(nums)) # Output: [1, 4, 2, 0, 0, 0] |
| 172 | + |
| 173 | +``` |
| 174 | + |
| 175 | +</TabItem> |
| 176 | +</Tabs> |
| 177 | + |
| 178 | + |
| 179 | +### Complexity Analysis |
| 180 | + |
| 181 | +- Time Complexity: $O(n)$ |
| 182 | +- for the operations and for shifting zeros. |
| 183 | +- Space Complexity: $O(n)$ |
| 184 | +- for the additional array. |
| 185 | +</tabItem> |
| 186 | +<tabItem value="Optimized approach" label="Optimized approach"> |
| 187 | + |
| 188 | +### Approach 2: Optimized approach |
| 189 | + |
| 190 | +Optimized Approach: |
| 191 | +Iterate through the array and perform the operations in place. |
| 192 | +After performing all operations, use two pointers to move all non-zero elements to the front and fill the remaining elements with zeros. |
| 193 | +#### Code in Different Languages |
| 194 | + |
| 195 | +<Tabs> |
| 196 | +<TabItem value="C++" label="C++" default> |
| 197 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 198 | + |
| 199 | +```cpp |
| 200 | +#include <iostream> |
| 201 | +#include <vector> |
| 202 | + |
| 203 | +std::vector<int> applyOperations(std::vector<int>& nums) { |
| 204 | + int n = nums.size(); |
| 205 | + |
| 206 | + for (int i = 0; i < n - 1; ++i) { |
| 207 | + if (nums[i] == nums[i + 1]) { |
| 208 | + nums[i] *= 2; |
| 209 | + nums[i + 1] = 0; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + int index = 0; |
| 214 | + for (int i = 0; i < n; ++i) { |
| 215 | + if (nums[i] != 0) { |
| 216 | + nums[index++] = nums[i]; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + while (index < n) { |
| 221 | + nums[index++] = 0; |
| 222 | + } |
| 223 | + |
| 224 | + return nums; |
| 225 | +} |
| 226 | + |
| 227 | +int main() { |
| 228 | + std::vector<int> nums = {1, 2, 2, 1, 1, 0}; |
| 229 | + std::vector<int> result = applyOperations(nums); |
| 230 | + for (int num : result) { |
| 231 | + std::cout << num << " "; |
| 232 | + } |
| 233 | + std::cout << std::endl; // Output: 1 4 2 0 0 0 |
| 234 | + return 0; |
| 235 | +} |
| 236 | + |
| 237 | +``` |
| 238 | +</TabItem> |
| 239 | +<TabItem value="Java" label="Java"> |
| 240 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 241 | +
|
| 242 | +```java |
| 243 | +import java.util.*; |
| 244 | +
|
| 245 | +public class ApplyOperations { |
| 246 | + public static int[] applyOperations(int[] nums) { |
| 247 | + int n = nums.length; |
| 248 | + |
| 249 | + for (int i = 0; i < n - 1; i++) { |
| 250 | + if (nums[i] == nums[i + 1]) { |
| 251 | + nums[i] *= 2; |
| 252 | + nums[i + 1] = 0; |
| 253 | + } |
| 254 | + } |
| 255 | + |
| 256 | + int index = 0; |
| 257 | + for (int i = 0; i < n; i++) { |
| 258 | + if (nums[i] != 0) { |
| 259 | + nums[index++] = nums[i]; |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + while (index < n) { |
| 264 | + nums[index++] = 0; |
| 265 | + } |
| 266 | + |
| 267 | + return nums; |
| 268 | + } |
| 269 | +
|
| 270 | + public static void main(String[] args) { |
| 271 | + int[] nums = {1, 2, 2, 1, 1, 0}; |
| 272 | + int[] result = applyOperations(nums); |
| 273 | + System.out.println(Arrays.toString(result)); // Output: [1, 4, 2, 0, 0, 0] |
| 274 | + } |
| 275 | +} |
| 276 | +
|
| 277 | +``` |
| 278 | + |
| 279 | + |
| 280 | +</TabItem> |
| 281 | +<TabItem value="Python" label="Python"> |
| 282 | +<SolutionAuthor name="@AmruthaPariprolu"/> |
| 283 | + |
| 284 | +```python |
| 285 | +def apply_operations(nums): |
| 286 | + n = len(nums) |
| 287 | + |
| 288 | + for i in range(n - 1): |
| 289 | + if nums[i] == nums[i + 1]: |
| 290 | + nums[i] *= 2 |
| 291 | + nums[i + 1] = 0 |
| 292 | + |
| 293 | + index = 0 |
| 294 | + for i in range(n): |
| 295 | + if nums[i] != 0: |
| 296 | + nums[index] = nums[i] |
| 297 | + index += 1 |
| 298 | + |
| 299 | + while index < n: |
| 300 | + nums[index] = 0 |
| 301 | + index += 1 |
| 302 | + |
| 303 | + return nums |
| 304 | + |
| 305 | +nums = [1, 2, 2, 1, 1, 0] |
| 306 | +print(apply_operations(nums)) # Output: [1, 4, 2, 0, 0, 0] |
| 307 | + |
| 308 | + |
| 309 | +``` |
| 310 | + |
| 311 | +</TabItem> |
| 312 | +</Tabs> |
| 313 | + |
| 314 | +#### Complexity Analysis |
| 315 | + |
| 316 | +- Time Complexity: $O(n)$ |
| 317 | +- for both operations and shifting. |
| 318 | +- Space Complexity: $O(1)$ |
| 319 | +- as no extra space is required. |
| 320 | +</tabItem> |
| 321 | +</Tabs> |
| 322 | + |
| 323 | + |
| 324 | +## Video Explanation of Given Problem |
| 325 | + |
| 326 | + <LiteYouTubeEmbed |
| 327 | + id="GLHChPK8VQ8" |
| 328 | + params="autoplay=1&autohide=1&showinfo=0&rel=0" |
| 329 | + title="Problem Explanation | Solution | Approach" |
| 330 | + poster="maxresdefault" |
| 331 | + webp |
| 332 | + /> |
| 333 | + |
| 334 | +--- |
| 335 | + |
| 336 | +<h2>Authors:</h2> |
| 337 | + |
| 338 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 339 | +{['AmruthaPariprolu'].map(username => ( |
| 340 | + <Author key={username} username={username} /> |
| 341 | +))} |
| 342 | +</div> |
| 343 | + |
0 commit comments