The output for an array of elements [0,1,0,3,12] is [1,3,12,0,0]
<h3>What is the function to get the desired output?</h3>
- Let us consider an array of elements,
Input = [0,1,0,3,12]
- The function code is given by,
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int count=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]==0)
{
nums.erase(nums.begin()+i);
++count; //This is to count number of zeroes.
}
}
for(int i = 0 ; i<count ; i ++)
nums . push_back(0); //To input zero at the end of the vector count times.
}
};
The output for an array of elements [0,1,0,3,12] is [1,3,12,0,0] where all 0's are moved to the end, while maintaining the relative order of the non-zero elements,
<h3>What is an array?</h3>
- A collection of items that are either values or variables is referred to as an array in computer science.
- Each element is identifiable by at least one array index or key.
- Each element of an array is recorded such that a mathematical formula can be used to determine its position from its index tuple.
- A linear array, often known as a one-dimensional array, is the simplest sort of data structure.
To learn more about array, refer:
brainly.com/question/19634243
#SPJ4