We can also take pre [ pos2 - 1 ] - pre [ pos1 ] == total_sum / 3 here .
if ( suf [ pos1 + 1 ] - suf [ pos2 ] == total_sum / 3 ) { return true ; } else { return false ; } } if ( pre [ i ] < suf [ j ] ) { i ++ ; } else { j -- ; } } return false ; } void equiSum ( int arr [ ] , int n ) { bool ans = equiSumUtil ( arr , n ) ; if ( ans ) { cout << " First ▁ Segment ▁ : ▁ " ; for ( int i = 0 ; i <= pos1 ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; cout << " Second ▁ Segment ▁ : ▁ " ; for ( int i = pos1 + 1 ; i < pos2 ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; cout << " Third ▁ Segment ▁ : ▁ " ; for ( int i = pos2 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } cout << endl ; } else { cout << " Array ▁ cannot ▁ be ▁ divided ▁ into ▁ three ▁ equal ▁ sum ▁ segments " ; } }