✅Simple Java Solutions in O(N) || ✅Runtime 0ms || ✅ Beats 100%
  \n# Intuition\nDescribe your first thoughts on how to solve this problem. \n\n\n\nThis code defines a `Solution` class with a method called `applyOperations`,
<!--  \n# Intuition\nDescribe your first thoughts on how to solve this problem. \n\n\n\nThis code defines a `Solution` class with a method called `applyOperations`, which takes an integer array `nums` as input and returns another integer array as output. The goal of this code is to perform certain operations on the input array `nums` and return the result in a new array.\n-->\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n1. It first determines the length of the input array `nums` and creates a new integer array `arr` of the same length to store the result.\n\n2. The code uses two pointers, `i` and `j`, to iterate through the input array `nums` and the result array `arr`, respectively. Initially, both pointers are set to 0.\n\n3. The code enters a loop that iterates over the elements of the input array `nums`.\n\n4. Inside the loop, it checks the current element `nums[i]`:\n   - If `nums[i]` is equal to 0, it continues to the next iteration (skips the current element).\n   - If `nums[i]` is not equal to 0, it checks whether the next element (`nums[i+1]`) is equal to the current element (`nums[i]`) and whether it\'s within the bounds of the array (`i < length-1`).\n   \n   - If both conditions are true, it means there\'s a pair of consecutive elements with the same value. In this case, it performs the following actions:\n     - It doubles the value of the current element and stores the result in the corresponding position of the result array `arr` (i.e., `arr[j++] = 2 * nums[i]`).\n     - It sets the next element (`nums[i+1]`) to 0 to indicate that it has been used in the operation.\n     - It increments both `i` and `j` to move to the next elements in both arrays.\n   \n   - If the conditions are not met, it means the current element is not part of a pair with the next element, so it simply stores the current element\'s value in the result array `arr` (i.e., `arr[j++] = nums[i]`).\n\n5. After processing all elements in the input array `nums`, the loop ends, and the result array `arr` contains the values after applying the specified operations.\n\n6. The code returns the result array `arr` as the output of the `applyOperations` method.\n\nIn summary, this code processes an input array, doubles the values of consecutive equal elements, and returns a new array with the result of these operations, eliminating the intermediate zeros generated by the doubling operation.\n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code1\n```Java\nclass Solution {\n    public int[] applyOperations(int[] nums) {\n        int length = nums.length;\n        int [] arr = new int[length];\n\n        for (int i = 0 , j = 0; i < length; i++)\n        {\n            if(nums[i] == 0) continue;\n            \n            if (i < length-1 && nums[i] == nums[i+1])\n            {\n                arr[j++] = 2 * nums[i];\n                nums[++i] = 0;  // or i++;  only\n            }else{\n                arr[j++] = nums[i];\n            }\n        }\n        return arr;\n     }\n}\n```\n\n# Code2\nin Runtime 1ms\nCheck out the `moveZeroes` method [here](https://leetcode.com/problems/move-zeroes/solutions/3960034/three-simple-java-solutions-runtime-0ms-beats100/) for a solution to efficiently move all zeros to the end of an array\n```Java\nclass Solution {\n    public int[] applyOperations(int[] nums) {\n        for (int i = 0; i < nums.length-1; i++)\n        {\n            if (nums[i] == nums[i+1])\n            {\n                nums[i] = 2 * nums[i];\n                nums[++i] = 0;\n            }\n        }\n\n       moveZeroes(nums);\n       \n        return nums;\n    }\n\n     public static void moveZeroes(int[] nums) \n     {\n        int i = 0;\n        for (int num : nums)\n        {\n            if (num != 0)\n            {\n                nums[i++] = num;\n            }\n        }\n\n        while (i <= nums.length - 1)\n        {\n            nums[i++] = 0;\n        }\n\n     }\n}\n```\n\n\n## For additional problem-solving solutions, you can explore my repository on GitHub: [GitHub Repository](https://github.com/ahmedna126/java_leetcode_challenges)\n\n\n\n![abcd1.jpeg](https://assets.leetcode.com/users/images/42ec05f0-affe-4425-bdc4-34eebe5401a6_1695832051.1396768.jpeg)\n
['Java']