Two Sum

Find indices of two numbers in an array that add up to a target value using a hash map for O(n) time complexity.

Code

Algorithms
echo "$arr" | jq --argjson t "$target" 'to_entries | . as $a | first(range(length) as $i | range($i+1; length) as $j | select($a[$i].value + $a[$j].value == $t) | [$i, $j]) // null'

Parameters

Array of numbers (JSON)

Target sum

Server

More Bash Snippets