Skip to content

Instantly share code, notes, and snippets.

@microhobby
Created March 14, 2018 19:34
Show Gist options
  • Save microhobby/64d87aa30d9ebeaf735b009af6f976a3 to your computer and use it in GitHub Desktop.
Save microhobby/64d87aa30d9ebeaf735b009af6f976a3 to your computer and use it in GitHub Desktop.
Solution with 0(n), but with a while to clear the trash
#include <iostream>
using namespace std;
void writeOnConsole(int * array, int arraySize) {
for (int i = 0; i < arraySize;i++)
cout << array[i] << ", ";
cout << array[arraySize] << endl;
}
int main() {
int array[] = {3, 2, 0, 4, 8, 0, 5, 9, 0, 11, 0, 45, 0, 23, 21, 0, 36};
int arraySize = sizeof(array)/sizeof(int) - 1;
int write = arraySize;
for (int read = arraySize; read >= 0; read--)
if(array[read] == 0)
continue;
else
array[write--] = array[read];
// clear trash
while ( write >= 0 ) {
array[write] =0;
write--;
}
writeOnConsole(array, arraySize);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment