Skip to content

Instantly share code, notes, and snippets.

@PeterStayPool
Created April 20, 2016 10:49
move zeros to the end of array
#include <iostream>
using namespace std;
void move_zeros_to_end(int* input, int len)
{
int zero = INT_MIN;
for(int i = 0; i < len; i++)
{
if (input[i] == 0)
{
if (zero == INT_MIN)
zero = i;
} else if (zero != INT_MIN)
{
input[zero] = input[i];
input[i] = 0;
zero = i;
}
}
}
int main()
{
int input[7] = {1,2,0,4,0,0,8};
move_zeros_to_end(input, 7);
for(int i = 0; i < 7; i++)
cout<<input[i];
cout<<endl;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment