Duplicate Zeros LeetCode solution in Java
1class Solution {23// Time Complexity = O(n)4// Space Complexity = O(1)56public void duplicateZeros(int[] arr) {78// Loop through the array9for (int i = 0; i < arr.length; i++) {1011// Trigger Condition12if (arr[i] == 0) {13int j; // auxilliary variable for swapping14for (j = arr.length - 2; j >= i + 1; j--) {15arr[j + 1] = arr[j]; // Shift each element by one space16}17arr[j + 1] = 0; // Duplicating the zero on the consecutive index of i18i++; // Skipping the duplicated zero index in the array19}20}21}22}