Calculate ratio for substring
if i > 1 : NEW_LINE INDENT count = sum [ n - 1 ] - sum [ i - 2 ] NEW_LINE DEDENT else : NEW_LINE INDENT count = sum [ n - 1 ] NEW_LINE DEDENT if i < n : NEW_LINE INDENT count -= sum [ n - i - 1 ] NEW_LINE DEDENT ans += count / i NEW_LINE return ans NEW_LINE