Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7576201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:51:53+00:00 2026-05-30T16:51:53+00:00

Given an array find the next smaller element in array for each element without

  • 0

Given an array find the next smaller element in array for each element without changing the original order of the elements.

For example, suppose the given array is 4,2,1,5,3.

The resultant array would be 2,1,-1,3,-1.

I was asked this question in an interview, but i couldn’t think of a solution better than the trivial O(n^2) solution.
Any approach that I could think of, i.e. making a binary search tree, or sorting the array, will distort the original order of the elements and hence lead to a wrong result.

Any help would be highly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:51:54+00:00Added an answer on May 30, 2026 at 4:51 pm

    O(N) Algorithm

    1. Initialize output array to all -1s.
    2. Create an empty stack of indexes of items we have visited in the input array but don’t yet know the answer for in the output array.
    3. Iterate over each element in the input array:
      1. Is it smaller than the item indexed by the top of the stack?
        1. Yes. It is the first such element to be so. Fill in the corresponding element in our output array, remove the item from the stack, and try again until the stack is empty or the answer is no.
        2. No. Continue to 3.2.
      2. Add this index to the stack. Continue iteration from 3.

    Python implementation

    def find_next_smaller_elements(xs):
        ys=[-1 for x in xs]
        stack=[]
        for i,x in enumerate(xs):
            while len(stack)>0 and x<xs[stack[-1]]:
               ys[stack.pop()]=x
            stack.append(i)
        return ys
    
    >>> find_next_smaller_elements([4,2,1,5,3])
    [2, 1, -1, 3, -1]
    >>> find_next_smaller_elements([1,2,3,4,5])
    [-1, -1, -1, -1, -1]
    >>> find_next_smaller_elements([5,4,3,2,1])
    [4, 3, 2, 1, -1]
    >>> find_next_smaller_elements([1,3,5,4,2])
    [-1, 2, 4, 2, -1]
    >>> find_next_smaller_elements([6,4,2])
    [4, 2, -1]
    

    Explanation

    How it works

    This works because whenever we add an item to the stack, we know its value is greater or equal to every element in the stack already. When we visit an element in the array, we know that if it’s lower than any item in the stack, it must be lower than the last item in the stack, because the last item must be the largest. So we don’t need to do any kind of search on the stack, we can just consider the last item.

    Note: You can skip the initialization step so long as you add a final step to empty the stack and use each remaining index to set the corresponding output array element to -1. It’s just easier in Python to initialize it to -1s when creating it.

    Time complexity

    This is O(N). The main loop clearly visits each index once. Each index is added to the stack exactly once and removed at most once.

    Solving as an interview question

    This kind of question can be pretty intimidating in an interview, but I’d like to point out that (hopefully) an interviewer isn’t going to expect the solution to spring from your mind fully-formed. Talk them through your thought process. Mine went something like this:

    • Is there some relationship between the positions of numbers and their next smaller number in the array? Does knowing some of them constrain what the others might possibly be?
    • If I were in front of a whiteboard I would probably sketch out the example array and draw lines between the elements. I might also draw them as a 2D bar graph – horizontal axis being position in input array and vertical axis being value.
    • I had a hunch this would show a pattern, but no paper to hand. I think the diagram would make it obvious. Thinking about it carefully, I could see that the lines would not overlap arbitrarily, but would only nest.
    • Around this point, it occurred to me that this is incredibly similar to the algorithm Python uses internally to transform indentation into INDENT and DEDENT virtual tokens, which I’d read about before. See “How does the compiler parse the indentation?” on this page: http://www.secnetix.de/olli/Python/block_indentation.hawk However, it wasn’t until I actually worked out an algorithm that I followed up on this thought and determined that it was in fact the same, so I don’t think it helped too much. Still, if you can see a similarity to some other problem you know, it’s probably a good idea to mention it, and say how it’s similar and how it’s different.
    • From here the general shape of the stack-based algorithm became apparent, but I still needed to think about it a bit more to be sure it would work okay for those elements that have no subsequent smaller element.

    Even if you don’t come up with a working algorithm, try to let your interviewer see what you’re thinking about. Often it is the thought process more than the answer that they’re interested in. For a tough problem, failing to find the best solution but showing insight into the problem can be better than knowing a canned answer but not being able to give it much analysis.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had previously posted a question, Given an array, find out the next smaller
Given an array. How can we find sum of elements in index interval (i,
Question: Given a sorted array A find all possible difference of elements from A.
Given an array of items, each of which has a value and cost ,
Given an array with n values, for example: $arr[] = 'ABCDEFABC'; $arr[] = 'ABCDEFDEF';
Given array a= [1,4,5,9,2].I need to find/print combinations of two values where sum =
Given an array, what is the most time- and space-efficient algorithm to find the
Given an array of n integers, where one element appears more than n/2 times.
Possible Duplicate: How to find all possible subsets of a given array? How can
I have a string. I need to replace all instances of a given array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.