Given an array of integers of size **N**. Assume ‘0’ as invalid number and all other as valid number. Write a program that modifies the array in such a way that if next number is valid number and is same as current number, double the current number value and replace the next number with 0. After the modification, rearrange the array such that all 0’s are shifted to the end and the sequence of the valid number or new doubled number is maintained as in the original array.

Examples:

```
Input : arr[] = {2, 2, 0, 4, 0, 8}
Output : 4 4 8 0 0 0
Input : arr[] = {0, 2, 2, 2, 0, 6, 6, 0, 0, 8}
Output : 4 2 12 8 0 0 0 0 0 0
```

**Input:**

The first line of the input contains an integer **T**, denoting the number of test cases. Then **T** test case follows. First line of each test contains an integer **N** denoting the size of the array. Then next line contains **N **space separated integers denoting the elements of the array.

**Output:**

For each test case print space separated elements of the new modified array on a new line.

**Constraints:**

1<=T<=10^{3}

1<=N<=10^{5}

**Example:
Input:**

2

5

2 2 0 4 4

5

0 1 2 2 0

4 8 0 0 0

1 4 0 0 0

Author: harshitsidhwa

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

preparecodinginterview_dot_com | 190 |

hari32147 | 162 |

JonSnow_IKnowNothing | 160 |

AdityaRaj9 | 112 |

shivamsingh00236 | 111 |

abducodes | 906 |

harshtyagi36 | 447 |

arpit_anshuman | 335 |

vamsikrishna55 | 323 |

sandipbhaumik | 321 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.