DEV Community

Cover image for Go Coding with Asparagos: Can two veggies cure a vitamin crisis?
Asparagos
Asparagos

Posted on • Edited on

Go Coding with Asparagos: Can two veggies cure a vitamin crisis?

Hi! I'm Asparagos — an asparagus who codes in Go. Here you’ll find everyday problems that a typical veggie might struggle with — and my Go solutions to them. Today we are solving the problem of Vitamin Deficiency 🫑.

No fun. Severe vitamin C deficiency!


Problem

We’re facing a severe vitamin C deficiency.
Luckily, vegetables with the highest vitamin C content are here to help.

We have a set of vegetables. Each of them contains a certain amount of vitamin C.
We also have a target amount of vitamin C we want to consume as a snack.
We are allowed to choose exactly two different vegetables.

Can we get the exact target amount by combining two of them?


Input 🥦

A slice of integers — each integer represents the amount of vitamin C a vegetable contains.

A target sum — an integer we need to reach by summing two different elements of the slice.

Output 🥕

A slice of two integers — indices of the elements whose values sum up to the target.
If no such pair exists, return nil.
If there are multiple valid pairs, return any of them.


Examples 🥒:

  • Example 1

    Input: [26, 73, 14, 61, 100], 134

    Output: [1, 3] (73 + 61 == 134)

  • Example 2

    Input: [26, 73, 14, 61, 85], 99

    Output: [0, 1] or [2, 4] (26 + 73 == 14 + 85 == 99)

  • Example 3

    Input: [26, 73, 14, 61, 100], 30

    Output: nil

  • Example 4

    Input: [26, 73, 14, 61, 100], 52

    Output: nil (26 + 26 == 52, but we can't take one element twice)

  • Example 5

    Input: [26, 73, 14, 26, 100], 52

    Output: [0, 3] (26 + 26 == 52, these are different elements, even though their values are the same)

  • Example 6

    Input: [], 15

    Output: nil


Solution 💡

  1. We'll create a map vitaminToLastInd, where the key is the vitamin value and the value is the last index at which this value appeared in the slice.

  2. We iterate through the vitamins slice.

    At each iteration, we try to find a matching pair for the current element. For an element vitamin, its matching pair would be targetSum - vitamin. If this value exists in vitaminToLastInd, we return the pair of indices.

    If not, we store the current vitamin value vitamin in vitaminToLastInd with its index and continue iterating.

    Importantly, we only add the current value to the map after checking it, so we never accidentally pair a value with itself — only with previously seen values.

  3. If no suitable pair is found by the end of the loop, we return nil.

func findVitaminsSum(vitamins []int, targetSum int) []int {
    vitaminToLastInd := make(map[int]int)
    for ind, vitamin := range vitamins {
        targetValue := targetSum - vitamin
        if targetInd, ok := vitaminToLastInd[targetValue]; ok {
            return []int{targetInd, ind}
        }
        vitaminToLastInd[vitamin] = ind
    }
    return nil
}
Enter fullscreen mode Exit fullscreen mode

Feel free to check out the full code with tests on GitHub, and don’t hesitate to leave a ⭐ if you find it helpful!

Top comments (0)