I am traverse the arrays from the right to left and store the elements which is in the right side of the current elements in stacks. But the problems says we have given the circular arrays so how to traverse it circulars can you make some clarifications on circular arrays iteration??
How to approch this problems
hello @Vikaspal
This approach makes use of a stack. This stack stores the indices of the appropriate elements from nums array. The top of the stack refers to the index of the Next Greater Element found so far. We store the indices instead of the elements since there could be duplicates in the nums array. The description of the method will make the above statement clearer.
We start traversing the numsnums array from right towards the left. For an element nums[i] encountered, we pop all the elements stack[top] from the stack such that nums[stack[top]] ≤ nums[i]. We continue the popping till we encounter a stack[top] satisfying nums[stack[top]]>nums[i]. Now, it is obvious that the current stack[top] only can act as the Next Greater Element for nums[i](right now, considering only the elements lying to the right of nums[i]).
If no element remains on the top of the stack, it means no larger element than nums[i] exists to its right. Along with this, we also push the index of the element just encountered(nums[i]), i.e. ii over the top of the stack, so thatnums[i](or stack[topstack[top) now acts as the Next Greater Element for the elements lying to its left.
We go through two such passes over the complete nums array. This is done so as to complete a circular traversal over the nums array. The first pass could make some wrong entries in the res array since it considers only the elements lying to the right of nums[i], without a circular traversal. But, these entries are corrected in the second pass
yeah right. . . . . . .
@aman212yadav instead of storing the index in stack we can store the values it will leads to the same results right…
yeah that should also work
https://ide.codingblocks.com/s/356939 how i can remove the redundant code like in else block i have to perform the same task again and also why we can’t use the vectors instead of arrays because in vectors i can’t access any index directly, So can please helpmeout to resolve this issue
Hello @Vikaspal
u can use vector as well. declare an n size vector and use it like u use any array
no that code is not redundant , we cannt remove it.
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.
@aman212yadav when we say we have 32 bits integer then we usually represent the bit in 8 bit??
in trie section problem xor pair, so while right shift bhaiya is doing like number >> 31 right shift… So it means we have 32 binary digits or 8 … i m bit confused…
@Vikaspal
number>>31 this line will bring 32th bit at 1st position.
integer is 32 bit in most of the cases
@aman212yadav let suppose a = 5(00000101) now i have to represent this is 64bit integer…
and also 32bit integer means our cpu is 32bit na?? and same goes with 64bits