0

Example if you have a stack

2 48 1 32 24 12 60

Then after operation it should look like

2 48 60 32 24 12 1

4
  • a lot of things but bringing the orignal stack back witout hampering the sequence is a problem. Commented Sep 22, 2015 at 13:06
  • Can you provide some code or approach you have tried ? Commented Sep 22, 2015 at 13:09
  • 1
    I'm not seeing a lot of things Commented Sep 22, 2015 at 13:09
  • Can any of you suggest something.... Commented Sep 22, 2015 at 13:28

2 Answers 2

3

Here is a much better Javascript immplementation:

var stack = [2, 48, 1, 32, 24, 12, 60]; var minPos = stack.indexOf(stack.reduce(function (a, b) { return a <= b ? a : b; })); var maxPos = stack.indexOf(stack.reduce(function (a, b) { return a >= b ? a : b; })); stack[minPos] = stack.splice(maxPos, 1, stack[minPos])[0]; 

Other than the line to set-up an example stack, this approach does 3 things:

  1. Find the position in the array of the 'minimum' number. A reduce function is used to iterate over the array, reducing the array to a single result, which represents the smallest number (See the JS Array.reduce method).
  2. Find the position in the array of the 'maximum' number. This is done similar to the above, but with a reduce function that results in the highest number.
  3. Using the positions we found for 'min' and 'max', swap the elements at these positions in the array.
Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain this? If you post code to a question that doesn't specify a language, it's best to not use language-specific features and syntax (like the other answer), but if you're going to, you should explain what they do. Or you can ask the question poster which language they'd like a solution in before posting an answer.
3

Here is a JS implementation of a solution.

var myStack = [ 2, 48, 1, 32, 24, 12, 60 ]; var posMax = 0; var maxValue = myStack[0]; var posMin = 0; var minValue = myStack[0]; var tempStack = [ ]; // will be constructed, but in the reverse order. var counter = 0; do { var tempElement = myStack.pop(); if(tempElement > maxValue) { posMax = counter; maxValue = tempElement; } if(tempElement < minValue) { posMin = counter; minValue = tempElement; } tempStack.push(tempElement); counter++; } while(myStack.length != 0); // Reverse the order of the temp Stack var tempStack2 = []; do { tempStack2.push(tempStack.pop()); } while (tempStack.length != 0) tempStack = tempStack2; // Constructing the returned Stack. var newStack = []; counter = 0; do { var tempElement = tempStack.pop(); if(counter !== posMin && counter !== posMax) { newStack.push(tempElement); } if(counter === posMax) { newStack.push(minValue); } if(counter === posMin) { newStack.push(maxValue); } counter++; } while(tempStack.length != 0); // Reverse the order of the new Stack var result = []; do { result.push(newStack.pop()); } while (newStack.length != 0); console.log("Final:" + result); 

7 Comments

but then u can only operate on the element at the top of the stack only
another thing once you have popped out an element how can you access the earlier stack
Please, see edit in the code above. I guess you will find answer to all your questions. :)
i think the reverse in tempstack2 is not neseccary size-posMax and size-posMin can also work
This is correct, but seems to me to be harder to understand because item are reversed ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.