Next Smaller Element Problem Statement

You are provided with an array of integers ARR of length N. Your task is to determine the next smaller element for each array element.

Explanation:

The Next Smaller Element for a given array element is the first element to its right that is strictly smaller than itself. If no such element exists, you should output -1 for that element.

Example:

Input:
ARR = [2, 3, 1]
Output:
[1, 1, -1]

Explanation: For the element 2, the next smaller element is 1. For the element 3, the next smaller element is 1. For the element 1, there is no smaller element to its right, so the result is -1.

Input:

The first line contains an integer ‘T’ representing the number of test cases.
The first line of each test case consists of an integer 'N', the number of elements in the array 'ARR'.
The second line comprises 'N' space-separated integers representing the array 'ARR'.

Output:

For each test case, output a single line with 'N' space-separated integers indicating the Next Smaller Element for each of the array elements.
Each test case should be printed on a separate line.

Constraints:

  • 1 <= T <= 10
  • 1 <= N <= 105
  • 0 <= ARR[i] <= 109
  • Time Limit: 1 second

Note: You are not required to print anything; the solution should return the result as specified.

AnswerBot
4mo

Find the next smaller element for each element in an array.

  • Iterate through the array from right to left and use a stack to keep track of elements.

  • Pop elements from the stack until a smaller element is...read more

Help your peers!
Select
Add answer anonymously...

Walmart Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Next Smaller Element Problem Statement You are provided with an array of integer...read more
A Software Developer Intern was asked Q. Josephus Problem Statement Consider 'N' individuals standing in a circle, number...read more
A Software Developer Intern was asked Q. Maximum Frequency Number Problem Statement Given an array of integers with numbe...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Next Smaller Element Problem Statement You are provided with an array of integer...read more
A Software Developer Intern was asked Q2. Josephus Problem Statement Consider 'N' individuals standing in a circle, number...read more
A Software Developer Intern was asked Q3. Maximum Frequency Number Problem Statement Given an array of integers with numbe...read more
Walmart Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits