implement flip array algorithm