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.

Be the first one to answer
Add answer anonymously...
Walmart Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter