---
# System prepended metadata

title: Untitled

---

```go=

Designing game looting system. Write a function such that it outputs the item based on the drop rate was given

Drop rate:
Sword 5% (5)
Armor 15% (15)
Potion 20% 20
Materials 25% (25)
Nothing 35% (35)

and total is 100

type Reward struct {
    Name string
}

type RewardQueue struct{
    Queue []Reward
}

func New(config)RewardQueue{
    return RewardQueue{
        
        ... setup queue // [Sword, nothing, Sword, Sword, Sword, Armor.....] 
    }
}

O(1)

func (r *RewardQueue) Drop() reward{
    if len(r.Queue) == 0 {
        return Reward{
            Name: "nothing"
        }
    }
    output:= r.Queue[0]
    r.Queue = r.Queue[1:]
    return output
}

      4
     / \
     8  5
    / \. \
    0. 1. 6

For node = 5, average value of that (5+6 == 11)/2  = 5.5 ~= 5

Output the number of nodes such that the average value of that subtree of the node == the value of the node itself
output = 5 (number of nodes)

O(n^n)

O(n) time + O(1) space

func Find(root *Node,out *int){
    if root == nil {
        return
    }
    numOfNode := 0
    sum:=0
    numOfNode
    sum(root,&sum,&numOfNode)
    avg:=sum/numOfNode
    if avg == root.Value{
        out++
    }
    Find(root.Left,&out)
    Find(root.Right,&out)
}

func sum(n *Node, sum,numOfNode *int){
    if n == nil {
        return
    }
    
    sum+=n.Value
    numOfNode++
    sum(n.Left,&sum,&numOfNode)
    sum(n.Right,&sum,&numbOfNode)
}

```