InterviewBit Academy is now Scaler!
InterviewBit Academy is now Scaler Academy!

Next Greater Circular Array!

Problem Description

Given a 1D integer array A containing N integers, the array A is circular.

A circular array is one where the next element of the last element is the first element.

You need to find next immediate larger number for every element in the array A.


  • For any element within the circular array, the next immediate larger number could be found circularly past the end and before it.
  • If there is no number greater, return -1.

Problem Constraints

1 <= N <= 105

1 <= A[i] <= 106

Input Format

First argument is an integer array A.

Output Format

Return a 1D integer array of size N representing next greater element for each element in the array A.

Example Input

Input 1:

 A = [3, 1, 3, 4]

Input 2:

 A = [3, 2]

Example Output

Output 1:

 [4, 3, 4, -1]

Output 2:

 [-1, 3]

Example Explanation

Explanation 1:

 3's next greater is 4
 1's next greater is 3
 3's next greater is 4 again
 4 will look to the beginning, but find nothing, so -1

Explanation 2:

 3 will look to the end, but find nothing, so -1
 2 will look to the beginning, and find 3

NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Next Greater Circular Array! on Interview Code Editor
Sign Up
to access hints and editorial solutions for Next Greater Circular Array!
Asked In:


Click here to start solving coding interview questions