Initial commit - working code. High time complexity. Repeated code.