# Golang program to find minimum value in a stack

In Golang, we can find minimum value in a stack by using iterative method and optimized iterative method. A stack is a linear data structure that follows the Last-In-First-Out (LIFO) principle.

## Syntax

func (s *Stack) getMin() int {…}


The getMin() function is used to find the minimum value in a stack. It takes a address pointer to the stack as its argument.

## Algorithm

• Step 1 − First, we need to import the fmtpackage.

• Step 2 − Create a stack structure with an item slice to store the stack elements and a min slice to store the minimum value in a stack.

• Step 3 − Now, define the functions, push() and pop() to perform the basic operations with the stack.

• Step 4 − push() adds the item to the end of the stack and checks if item is smaller than or equal to the current minimum value in the min slice. If it is so, then item is also added to min slice.

• Step 5 − pop() removes the last item from the stack and checks if item is equal to the last item in the min slice. If it is so, that means the last item in the min slice was the minimum value in the stack and we need to remove it from the min slice as well.

• Step 6 − Now, create getMin() function to find the minimum value in a stack. It simply returns the last item in the min slice, which represents the minimum value in the stack.

• Step 7 − Start the main() function. Inside the main() function, create a stack struct and initialize a stack with some items.

• Step 8 − Now, call the getMin() function.

• Step 9 − Further, the minimum value in the stack is printed to the console.

## Example 1

In this example, we will define a getMin() function that is used to find minimum value in a stack by using iterative method.

package main

import "fmt"

type Stack struct {
items []int
min   []int
}

func (s *Stack) push(item int) {
s.items = append(s.items, item)
if len(s.min) == 0 || item <= s.min[len(s.min)-1] {
s.min = append(s.min, item)
}
}

func (s *Stack) pop() int {
if len(s.items) == 0 {
return -1 // stack is empty
}

popped := s.items[len(s.items)-1]
s.items = s.items[:len(s.items)-1]

if popped == s.min[len(s.min)-1] {
s.min = s.min[:len(s.min)-1]
}

return popped
}

func (s *Stack) getMin() int {
if len(s.min) == 0 {
return -1
}

return s.min[len(s.min)-1]
}

func main() {
s := Stack{}
s.push(5)
s.push(2)
s.push(7)
s.push(1)
s.push(8)
fmt.Println("Minimum value in stack is:", s.getMin())
s.pop()
s.pop()
fmt.Println("Minimum value in stack is:", s.getMin())
}


## Output

Minimum value in stack is: 1
Minimum value in stack is: 2


## Example 2

In this example, we will define a getMin() function that is used to find minimum value in a stack by using iterative method in optimized manner.

package main

import (
"errors"
"fmt"
)

type Stack struct {
items []int
}

func (s *Stack) Push(item int) {
s.items = append(s.items, item)
}

func (s *Stack) Pop() (int, error) {
if len(s.items) == 0 {
return 0, errors.New("stack is empty")
}
poppedItem := s.items[len(s.items)-1]
s.items = s.items[:len(s.items)-1]
return poppedItem, nil
}

func (s *Stack) Min() (int, error) {
if len(s.items) == 0 {
return 0, errors.New("stack is empty")
}
minItem := s.items[0]
for _, item := range s.items {
if item < minItem {
minItem = item
}
}
return minItem, nil
}

func main() {
stack := Stack{}
stack.Push(4)
stack.Push(6)
stack.Push(2)
stack.Push(8)
stack.Push(1)

minItem, err := stack.Min()
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Minimum value in stack is:", minItem)
}
}


## Output

Minimum value in stack is: 1


## Conclusion

We have successfully compiled and executed a go language program to find minimum value in a stack by using iterative method and optimized iterative method.In the first example, we have used the iterative method and in the second example, we have used the optimized iterative method.

Updated on: 10-May-2023

85 Views