We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here .
if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum // 3 ) : NEW_LINE INDENT return [ True , pos1 , pos2 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT return [ False , pos1 , pos2 ] ; NEW_LINE DEDENT if ( pre [ i ] < suf [ j ] ) : NEW_LINE i += 1 ; NEW_LINE else : NEW_LINE j -= 1 ; NEW_LINE return [ False , pos1 , pos2 ] ; NEW_LINE def equiSum ( arr ) : NEW_LINE pos1 = - 1 ; NEW_LINE pos2 = - 1 ; NEW_LINE ans = equiSumUtil ( arr , pos1 , pos2 ) ; NEW_LINE pos1 = ans [ 1 ] ; NEW_LINE pos2 = ans [ 2 ] ; NEW_LINE if ( ans [ 0 ] ) : NEW_LINE print ( " First ▁ Segment ▁ : ▁ " , end = " " ) ; NEW_LINE for i in range ( pos1 + 1 ) : NEW_LINE print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE print ( " " ) ; NEW_LINE print ( " Second ▁ Segment ▁ : ▁ " , end = " " ) ; NEW_LINE for i in range ( pos1 + 1 , pos2 ) : NEW_LINE print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE print ( " " ) ; NEW_LINE print ( " Third ▁ Segment ▁ : ▁ " , end = " " ) ; NEW_LINE for i in range ( pos2 , len ( arr ) ) : NEW_LINE print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE print ( " " ) ; NEW_LINE else : NEW_LINE println ( " Array ▁ cannot ▁ be ▁ divided ▁ into " , " three ▁ equal ▁ sum ▁ segments " ) ; NEW_LINE