iteration with stack solution