Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Ninja’s Circular Array

Moderate
0/80
profile
Contributed by
24 upvotes
Asked in companies
MicrosoftGoldman Sachs

Problem statement

Ninja has a circular array ‘Nums’ containing ‘N’ positive integers. An array is called circular if we consider the first element as next of the last element.

Ninja wants you to find the first greater number to the right of each element in the array, if there is no greater element to the right of an element, then output -1 for this element.

Example :
If N = 5 and the array is: { 1, 6, 4, 3, 5 }

We will return { 6, -1, 5, 5, 6 }
because 6 is the first element to the right of 1 that is greater than 1,
no element exists that is greater than 6,
5 is the first element to the right of 4 that is greater than 4,
5 is the first element to the right of 3 that is greater than 3,
6 is the first element to the circular-right of 5 that is greater than 5.
Detailed explanation ( Input/output format, Notes, Images )
Constraints :
1 <= T <= 10      
1 <= N <= 200
1 <= Arr[i] <= 10^5

Time limit: 1 sec
Sample Input 1 :
2
5
1 6 4 3 5
3
1 2 1
Sample Output 1 :
6 -1 5 5 6
2 -1 2
Explanation For Sample Input 1 :
For test case 1 : 
We will return { 6, -1, 5, 5, 6 } because 6 is the first element to the right of 1 that is greater than 1, no element exists that is greater than 6, 5 is the first element to the right of 4 that is greater than 4, 5 is the first element to the right of 3 that is greater than 3, 6 is the first element to the circular-right of 5 that is greater than 5.

For test case 2 :
We will return { 2, -1, 2 } because 2 is the first element to the right of 1 that is greater than 1, no element exists that is greater than 2, 2  is the first element to the circular-right of 1 that is greater than 1.
Sample Input 2 :
2
1
500
4
1 2 3 4
Sample Output 2 :
-1
2 3 4 -1
Full screen
Console