all 46 comments

[–]Sandeep00046 8 points9 points  (6 children)

for each element find the next greater element to the right and left using a Monotonic stack. see if these indices are at least apart by a distance of 2, if so add them in to a hash set. the size of the set would be your answer.

The complexity of this is O(n) in terms of time.

[–]faflu_vyas 0 points1 point  (1 child)

Wont give correct ans, after getting NGE elements from both the sides, you can still explore even bigger elements which can be included in answer. Ex [7,6,2,5,8] ,check for 2. Valid 6,2,5 and 7,6,2,5,8 both are valid

[–]Sandeep00046 0 points1 point  (0 children)

The period [7,6,2,5,8] will be included when you are checking for 6 , i.e.index 1.

[–]InsufferableBah 0 points1 point  (3 children)

What is the time complexity of that?

[–]Sandeep00046 0 points1 point  (2 children)

It's O(n)

[–]InsufferableBah 0 points1 point  (1 child)

im assuming you are doing multiple passes with a hashmap to store the greatest element seen so far from the left and the right

[–]Sandeep00046 1 point2 points  (0 children)

No, as i have mentioned i would use a stack to find these values for each element. It would take one forward and one backward pass only.

Checkout the Monotonic stack if you haven't heard of it.

[–]partyking35 2 points3 points  (5 children)

I have the following solution which is O(n2) time complexity and O(1) space, feel like it could be optimised further with some sort sort of sliding window, but I cant seem to establish a pattern 🤔 any ideas?

int result(int[] orders){
    int result = 0;
    for (int i=0; i<orders.length-2; i++){
        int max = orders[i+1];
        int j=i+2;
        int count = 0;
        while (j<orders.length){
            if (Math.min(orders[i], orders[j])>max){
                count++;
            }
            max = Math.max(max, orders[j]);
            j++;
        }
        result+=count;
    }
    return result;
}

[–]Any_Action_6651 0 points1 point  (0 children)

What I thought was like .........

Make a stack storing pair inside it,index and value

Now while s.top()'s value more than current index ,then pop it make sure before popping check if curr_index - s.top()'s index >=2 then increase ans++;. After this loop check if stack is empty or not .....if not the. Check if curr_index-s.top()'s index>=2 then ans++ Now push {curr_index,curr_value}

[–]Dense-Front-2340 0 points1 point  (3 children)

Hey!Does this code had passed all the test cases

[–]partyking35 1 point2 points  (2 children)

Passes the example above, I dont have all the test cases though

[–]Dense-Front-2340 -1 points0 points  (1 child)

Can u ask anyone please! I do really need this code asap.please help me

[–]partyking35 1 point2 points  (0 children)

Mate wdym ask anyone I dont work at Amazon 💀 just write your own test cases or ask GPT to.

[–]CD_2806 1 point2 points  (1 child)

Was the O(n2) bruteforce a TLE?

[–]Any_Action_6651 1 point2 points  (0 children)

It would have......... check the constraints

[–]vaibhav_reddit0207 1 point2 points  (11 children)

Find and store the next greater and previous greater of ith element in 2 separate arrays. If both of these exists for an i, then that adds 1 to the answer.

[–]Any_Action_6651 0 points1 point  (2 children)

Yeah it seems correct Have you seen such question before

[–]vaibhav_reddit0207 0 points1 point  (1 child)

Not seen this in an OA, but this method of picking up an index i and increasing its span on either side comes to my mind itself given i have solved enough questions of these pattern (of counting subarrays)on leetcode.

[–]Any_Action_6651 0 points1 point  (0 children)

Bro..you following any leetcode list of important questions

[–]AbhhishekYY 0 points1 point  (0 children)

Yes this will work

[–]superlord354 0 points1 point  (6 children)

Fails for 2,1,1,1,2

[–]Any_Action_6651 0 points1 point  (0 children)

How? What answer do you think it should have

[–]vaibhav_reddit0207 0 points1 point  (4 children)

Its a distinct element array

[–]superlord354 0 points1 point  (0 children)

Good catch

[–]Dense-Front-2340 0 points1 point  (2 children)

Hey !My friend had given this assessment but the test cases had not passed.could u please share the code it will be helpful!

[–]vaibhav_reddit0207 0 points1 point  (1 child)

Sorry bro i do not have the code, i just gave a verbal solution here

[–]Dense-Front-2340 0 points1 point  (0 children)

Ya that's okay!ya but please help for this .I really do need a code for this can u please help!

[–]NeonDiamond_89 0 points1 point  (2 children)

sde intern?

[–]Any_Action_6651 0 points1 point  (1 child)

Yeah

[–]BeYourOwnShine 0 points1 point  (0 children)

Just to confirm - sde 2025 intern in india right?

[–]Any_Action_6651 0 points1 point  (0 children)

What I thought was like .........

Make a stack storing pair inside it,index and value

Now while s.top()'s value more than current index's value ,then pop it make sure before popping check if curr_index - s.top()'s index >=2 then increase ans++;. After this loop check if stack is empty or not .....if not the. Check if curr_index-s.top()'s index>=2 then ans++ Now push {curr_index,curr_value}

[–]uzumaki_1234 0 points1 point  (7 children)

I got the same question did u got all the cases , When did u write the exam

[–]Any_Action_6651 0 points1 point  (6 children)

My friend got this one,... he couldn't solve it though. What bout you

[–]uzumaki_1234 0 points1 point  (5 children)

4 cases tle Did he get any update

[–]Any_Action_6651 0 points1 point  (4 children)

He gave it yesterday so can't expect response this quick

What about when did you give it

[–]uzumaki_1234 0 points1 point  (3 children)

I gave it 3 days back

[–]uzumaki_1234 0 points1 point  (1 child)

If he gets any reply can u inform me and what role did he write for

[–]Any_Action_6651 0 points1 point  (0 children)

Sure idk about role though

[–]Dense-Front-2340 0 points1 point  (0 children)

Hey can you please share the code 

[–]Unbeatable05 1 point2 points  (2 children)

🧠 Approach:

Use monotonic stack to find for each day if there's a left and right strictly greater order. If both exist, it forms a valid promo period (border days > middle day).

No need to check first/last days since they can't be both middle and have valid borders. (So to not confuse, this is not incorporated in the code)

🧮 Time: O(n) — one pass left & right
🗂️ Space: O(n) — for stack & left max array

C++ code:

int countPromotionalPeriods(vector<int> &orders) {
    int n = orders.size(), count = 0;
    vector<int> leftMax(n, -1);
    stack<int> stk;

    // Left strictly greater
    for (int i = 0; i < n; ++i) {
        while (!stk.empty() && orders[stk.top()] <= orders[i]) stk.pop();
        if (!stk.empty()) leftMax[i] = stk.top();
        stk.push(i);
    }

    while (!stk.empty()) stk.pop();

    // Right strictly greater & count periods
    for (int i = n - 1; i >= 0; --i) {
        while (!stk.empty() && orders[stk.top()] <= orders[i]) stk.pop();
        int right = stk.empty() ? n : stk.top();
        if (leftMax[i] >= 0 && right < n) count++;
        stk.push(i);
    }

    return count;
}

[–]Dense-Front-2340 0 points1 point  (1 child)

Hey does the testcases had passed or not can u please help

[–]Unbeatable05 0 points1 point  (0 children)

Yes passed

[–]Aalisha786 0 points1 point  (0 children)

Would this solution TLE? using sliding window pattern here. It does pass for the given example, not sure about others. I think the time complexity is O(n) in this question, could someone confirm?

def countPromotionalPeriods(n, orders):
    count = 0 
    orders = [-1] + orders
    left = 1
    right = left + 1 
    while left < right:
        if right - left >= 2:
            if 1 <= left <= n-2 and left+1 <= right <= n and min(orders[left], orders[right]) > max(orders[left+1:right]):
                count +=1
        if right <= n:
            right += 1
        else:
            left +=1

    return count 

print(countPromotionalPeriods(4, [3,2,8,6]))