Added optimized versions for Problems 1 and 2