Move All Zeroes to End of Array

Given an array of integers, write a code to move all zeroes to end of array. The order of all other elements of an array should be same.

Now, In this question we have to write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

NOTE : We have to solve this problem inplace without making a copy of the array.

For example –

Example 1:

Input : {1, 2, 0, 4, 0, 5, 3, 8}

Output: {1, 2, 4, 5, 3, 8, 0, 0}

In this example, All zeros are moved to the end of array and the relative order of non-zero elements is also maintained.

Example 2:

Input : {1, 0, 2, 3, 0, 0, 0, 2}

Output : {1, 2, 3, 2, 0, 0, 0, 0}

Next Greater Element in Array

Find next greater element in array or find next greater element to right.

Given an input array, find the next greater element for every element of an array. The next greater element x is the first greater element on the right side of x in an array.

For example:

Example 1:

Input:     {4,   2,   6,   8,  1,   0}

Output:  {6,   6,   8, -1, -1,  -1}

4 => 6 (The first next greater element of 4 is 6)
2 => 6 (Next greater element of 2 is 6)
6 => 8 (Next greater element of 6 is 8)
8 => -1 (No next greater element found)
1 => -1 (No next greater element found)
0 => -1

Example 2:

Input:     {7,   8,   1,   4}

Output:  {8, -1,  4,  -1}