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

  • SEARCH
  • Home
  • 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 788587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:23:25+00:00 2026-05-14T21:23:25+00:00

http://en.wikipedia.org/wiki/All_nearest_smaller_values . This is the site of the problem and here is my code,

  • 0

http://en.wikipedia.org/wiki/All_nearest_smaller_values. This is the site of the problem
and here is my code, but I have some trouble to implement it:

import java.util.*;
public class stack{

    public static void main(String[]args){

        int x[]=new int[]{  0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };

        Stack<Integer> st=new Stack<Integer>();

        for (int a:x){
            while (!st.empty() && st.pop()>=a){
                System.out.println( st.pop());
                if (st.empty()){
                    break;
                }
                else{
                    st.push(a);
                }
            }
        }
    }
}

And here is the pseudo code from the site:

S = new empty stack data structure
for x in the input sequence:
    while S is nonempty and the top element of S is greater than or equal to x:
        pop S
    if S is empty:
        x has no preceding smaller value
    else:
        the nearest smaller value to x is the top element of S
    push x onto S

What is the matter with my code?

  • 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-14T21:23:25+00:00Added an answer on May 14, 2026 at 9:23 pm

    Here is the same pseudo code but I’ve added brackets so you can see where each statement begins and ends.

    S = new empty stack data structure
    for x in the input sequence:
    {
        // peek instead of pop when you're checking what's in the queue
        while S is nonempty and the top element of S is greater than or equal to x:
        {
            pop S // you can call pop here
        }
    
        if S is empty:  // just check if the queue is empty, don't peek or pop
        {
            x has no preceding smaller value
        }
        else:
        {
            the nearest smaller value to x is the top element of S
        }
        push x onto S
    }
    

    You had the if/else statement inside the while loop, which was incorrect.

    Check the documentation of the stack to understand what push, pop and peek do, here is the documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Stack.html

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

Sidebar

Related Questions

Here's my code.. require open-uri base_url = http://en.wikipedia.org/wiki (1..5).each do |x| # sets up
According to http://en.wikipedia.org/wiki/Producer-consumer_problem I want to simulate P/C problem using semaphore. I am getting
I'm using PeerTracker http://en.wikipedia.org/wiki/PeerTracker to track torrent hashes. I have a simple HTML page
I have read the explanation in http://en.wikipedia.org/wiki/PageRank and i understand that the page rank
I have the following JSON string (from wikipedia http://en.wikipedia.org/wiki/JSON ) { name:Product, properties: {
I google'd a bit and read http://en.wikipedia.org/wiki/Portable_Executable but i can't seem to find when
I'll write a program for Interactive UNIX ( http://en.wikipedia.org/wiki/INTERACTIVE_UNIX ). But in a year
I want to implement a version of Benford's law ( http://en.wikipedia.org/wiki/Benford%27s_law ) that basically
Here is the wiki for those who want to know about Apache CXF. http://en.wikipedia.org/wiki/Apache_CXF
From this Wikipedia article: http://en.wikipedia.org/wiki/Hamiltonian_path_problem A randomized algorithm for Hamiltonian path that is fast

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.