We have assumed that x1 will be left and x2 right but if not then just swap
if ( x1 > x2 ) : NEW_LINE INDENT x1 , x2 = x2 , x1 NEW_LINE DEDENT Occ_Till_Right = pref [ x2 - 1 ] NEW_LINE Occ_Till_Left = pref [ x1 ] NEW_LINE ans += ( Occ_Till_Right - Occ_Till_Left ) NEW_LINE return ans NEW_LINE