fork download
  1. #include <iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5. class Solution {
  6. public:
  7. void moveZeroes(vector<int>& nums) {
  8. // create a vector which store all the elements rather than 0
  9. vector<int> v1;
  10. int count;
  11. // Run the loop for the given problems
  12. for(int i=0;i<nums.size();i++){
  13. if(nums[i]!=0){
  14. v1.push_back(nums[i]);
  15.  
  16. }
  17. else{
  18. count=count+1;
  19.  
  20. }
  21. }
  22. // The left over elelments should be stored after the non zero elements
  23. // Run the loop for the given elements
  24. for(int i=0;i<count;i++){
  25. v1.push_back(0);
  26.  
  27. }
  28.  
  29.  
  30. for(int i = 0; i < v1.size(); ++i){
  31.  
  32. nums[i] = v1[i];
  33. }
  34. // print the given o/p
  35. for(int i=0;i<nums.size();i++){
  36. cout<<nums[i]<<" ";
  37. }
  38.  
  39.  
  40. }
  41. };
  42.  
  43.  
  44.  
  45.  
  46. int main() {
  47. // your code goes here
  48. Solution s1;
  49. vector<int> v2={0,1,0,3,12};
  50. s1.moveZeroes(v2);
  51. //cout<<v2;
  52. return 0;
  53. }
Success #stdin #stdout 0s 5264KB
stdin
Standard input is empty
stdout
1 3 12 0 0