File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes anyone can solve this one?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Reply locked New topic

anyone can solve this one??

alex lotel
Ranch Hand

Joined: Feb 01, 2008
Posts: 191
i have two stack S1 and S2 the total amount of objects
in them is "n"
(there is also stack S3 )
in both stack all the objects are sorted in accending order

(the biggest number is on top the smallest number is on the bottom)

and thats the way it goes in both stacks

the question asks me to build a method which
puts all of the objects from stacks S1 and S2 into stack S3
in accended order plus we have to accomplish this using 4n-4 moves ??
(the biggest number is on top the smallest number is on the bottom)

i tried to solve that by
flipping the stacks using the S3 so they will be sorted from the smallest
to the biggest
taking two templorary variables pop each object from each stack
into the temporary variable
and then we compare them and the smallest goes to S3
and the other one waits till we have an object from the other stack which is smaller than him,
and then we have to flip S3 in order have the biggest on top
also i tried using TOP commang but i found out that
its implementation also has push and pop in it.
but in that way we have more than 4n-4 moves
Henry Wong

Joined: Sep 28, 2004
Posts: 20374

Please don't crosspost. It just wastes people's time and effort. This questions is already being addressed here....


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: anyone can solve this one??
It's not a secret anymore!