Skip to content

Instantly share code, notes, and snippets.

@enderahmetyurt
Created July 28, 2025 11:08
Show Gist options
  • Save enderahmetyurt/e0c60fe5bed3fb25b05b78562028a1dc to your computer and use it in GitHub Desktop.
Save enderahmetyurt/e0c60fe5bed3fb25b05b78562028a1dc to your computer and use it in GitHub Desktop.
Given an array of side lengths, write a function to determine they can form a hexagon with three side-length pairs (as in, three pairs of equal sides needed). Return true if possible.
def can_form_hexagon(sides)
return false unless sides.size == 6
tally = sides.tally
return false unless tally.size == 3 && tally.values.all?(2)
a, b, c = tally.keys.sort
[a, b, c].all? { |x| x < (a + b + c - x) }
end
puts can_form_hexagon([2, 3, 8, 8, 2, 3])
puts can_form_hexagon([1, 2, 3, 4, 5, 6])
puts can_form_hexagon([2, 2, 2, 2, 2, 2, 2])
puts can_form_hexagon([1, 1, 10, 10, 1, 1])
puts can_form_hexagon([3, 3, 4, 4, 5, 5])
puts can_form_hexagon([1, 2, 2, 3, 3, 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment