Associated Course(s):
Must Do Interview Preparation
Sudo Placement 2019

Given an array **A** of size **N** having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array. If no such element exists, output **-1 **

**Input:**

The first line of input contains a single integer **T** denoting the number of test cases.Then **T** test cases follow. Each test case consists of two lines. The first line contains an integer **N** denoting the size of the array. The Second line of each test case contains** N **space separated positive integers denoting the values/elements in the array **A**.

**Output:**

For each test case, print in a new line, the next greater element for each array element separated by space in order.

**Constraints:**

1** **<= T <= 100

1 <= N <= 10^{7}

1 <= A_{i} <= 10^{18}

**Example:
Input**

2

4

1 3 2 4

4

4 3 2 1

3 4 4 -1

-1 -1 -1 -1

**Explanation**:

**Testcase1:** In the array, the next larger element to 1 is 3 , 3 is 4 , 2 is 4 and for 4 ? since it doesn't exist hence -1.

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.

the_coder95 | 465 |

thanuvinu94 | 388 |

adityasuman2025 | 305 |

Senbonzakura_Kageyoshi | 297 |

PrakharBansal1 | 274 |

the_coder95 | 1691 |

RishabhTanwar1 | 1110 |

thanuvinu94 | 1064 |

samay_kam_hai | 724 |

themanhasnoname | 620 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4897 |

Quandray | 4547 |

Login to report an issue on this page.