finds the biggest product of ( numbers prefix_product max_product float ('-inf 'for in range(len( ))suffix_product suffix_products [ if len(aelse max_product maxmax_product prefix_product suffix_product prefix_product * [ireturn max_product the time complexity is ( )the space complexity is ( )since the solution uses single array of length we now solve this problem in (ntime and ( additional storage the insight comes from the fact that if there are no negative entriesthe maximum product comes from using all but the smallest element (note that this result is correct if the number of entries is zerooneor more if the number of negative entries is oddregardless of how many entries and positive entriesthe maximum product uses all entries except for the negative entry with the smallest absolute valuei the least negative entry going furtherif the number of negative entries is eventhe maximum product again uses all but the smallest nonnegative elementassuming the number of nonnegative entries is greater than zero (this is correcteven in the presence of entries if the number of negative entries is evenand there are no nonnegative entriesthe result must be negative since we want the largest productwe leave out the entry whose magnitude is largesti the greatest negative entry this analysis yields two-stage algorithm firstdetermine the applicable scenarioe are there an even number of negative entriesconsequentlyperform the actual multiplication to get the result def find_biggest_n_minus_one_product ( )number_of_negatives least_nonnegative_idx least_negative_idx greatest_negative_idx none identify the least negative greatest negative and least nonnegative entries for ia in enumerate ( )if number_of_negatives + if least_negative_idx is none or aleast_negative_idx aleast_negative_idx if greatest_negative_idx is none or agreatest_negative_idx ]greatest_negative_idx elsea > if least_nonnegative_idx is none or aleast_nonnegative_idx ]least_nonnegative_idx idx_to_skip least_negative_idx if number_of_negatives else least_nonnegative_idx if least_nonnegative_idx is not none else greatest_negative_idx return functools reduce lambda product aproduct ause generator rather than list comprehension to avoid extra space