Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- using namespace std;
- int main()
- {
- const int arrSize = 10;
- int arr[arrSize] = { 0,1,2,0,0,3,0,4,0,0 };
- // int arr[arrSize] = {1,2,3,0,0,4,0,5,0,6};
- // int arr[arrSize]={0};
- // int arr[arrSize]={1,2,3,4,5,6,7,8,9,10};
- for (int i = 0; i < arrSize; i++) { cout << arr[i] << " "; } cout << endl;
- for (int tail = arrSize - 1, head = tail - 1; head >= 0; tail--, head--)
- {
- for (; arr[tail] && tail > 1; tail--);
- for (head = tail - 1; !arr[head] && head > 0; head--);
- if (!arr[tail] && arr[head])
- {
- arr[tail] = arr[head];
- arr[head] = 0;
- for (int i = 0; i < arrSize; i++) { cout << arr[i] << " ";} cout << endl;
- }
- }
- for (int i = 0; i < arrSize; i++) { cout << arr[i] << " "; } cout << endl;
- }
Add Comment
Please, Sign In to add comment