Created
April 20, 2016 10:49
move zeros to the end of array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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