Consider all sub - arrays starting from i
for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ j ] == 0 : NEW_LINE INDENT sum -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT DEDENT