Https://ide.codingblocks.com/s/656067

can you please help me out with this code.
it is failing one test case and i am not able to understand concept of circular array

@hittasehgal,
assume circular array as circular table and chairs as element now there’s no start no end you just need to find what’s greater element of each number.

say a[] = 1,2,0
so here for 1 its 2
for 2 we can’t find any so -1
for 0 we can see the array ended but its circular so we can find if we traverse again from start so we find 1
but for 2 even if we traverse back and forth we wont find any so -1 for 2

you can do this by doubling the array
in this way you again come to question where array is not circular see the same eg:

same array when duplicated becomes 1,2,0,1,2,0
now we know we have duplicated and we need to find next greater for first 3 element only for 1 its 2 for 2 we wont find any even if we continue for 0 its 1

@hittasehgal,
u may refer to https://ide.codingblocks.com/s/656071 ive commented
small change in your code

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

thanku sir for great explanation

1 Like

but why did you duplicate the vector

@hittasehgal, just to make implementation easier else you can use modulo also to go back to the first element…as now u don’t have to go to start position after last index u can continue searching in linear fashion