Changes for circular array

the link above contains the code of next greater element question please help me to change it enough to make it work in case of a circular array.

hello @we_kaash

to handle circular case u need to again run while loop.

check this->

sorry sir, actually i am not much familiar with vectors can you please
help me again with this code
you can tell me the necessary changes in this only its getting a bit confusing for me to do it with vectors.

initialise ur nge array with -1.

after that do this->

	// greater elements towards the right
	for(int i = 0; i < 2*n; i++){
		int element = arr[i%n];
		while(s.size() and arr[s.top()] < element ){ //  if element is greater than the top of the stack then pop elements
			nge[s.top()] = element;  //  [1 2 1] | right[1] = 2 
			s.pop();
		}
		s.push(i%n);
	}

If this is what you are saying to do then the result is this…

a)
u r not initialising nge correctly.
use loop to initilaise all entirs with -1 . otherwise only first entry will be -1 rest will remain 0
b) while loop of line 18 us not required.

c) DIreclty print nge array ,becuase in nge we are storing values and not index

thank you sir it was a great mental relief…
sir i have seen the videos, even prepared their notes of stacks , queues and previous topics then
what should i do so that i am able think of the approach…sometime its depressing that after studyingthis way and still i am not able to think the logics of the questions…if you could help me with some of your guidance i would be really greatful to you

…

its very much common , everyone face this issue in the beginning of their programming journey. so nothing to worry about.
all i would advice is to practice ds-algo every day (consistency is key for evrything), solve problems that makes u uncomfortable becuase that will force u to come out of ur comfort zone and learn something new.

1 Like

thank you sir, I’ll definitly follow your words.

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.