Divide array into two parts with equal sum according to the given constraints | Function that checks if the given conditions are satisfied ; To store the prefix $sum of the array elements ; Sort the array ; Compute the prefix sum array ; Maximum element in the array ; Variable to check if there exists any number ; Stores the index of the largest number present in the array smaller than i ; Stores the index of the smallest number present in the array greater than i ; Find index of smallest number greater than i ; Find index of smallest number greater than i ; If there exists a number ; If no such number exists print no ; Driver code
< ? php function IfExists ( $ arr , $ n ) { $ sum = array_fill ( 0 , $ n , 0 ) ; sort ( $ arr ) ; $ sum [ 0 ] = $ arr [ 0 ] ; for ( $ i = 1 ; $ i < $ n ; $ i ++ ) $ sum [ $ i ] = $ sum [ $ i - 1 ] + $ arr [ $ i ] ; $ max = $ arr [ $ n - 1 ] ; $ flag = false ; for ( $ i = 1 ; $ i <= $ max ; $ i ++ ) { $ findex = 0 ; $ lindex = 0 ; $ l = 0 ; $ r = $ n - 1 ; while ( $ l <= $ r ) { $ m = ( $ l + $ r ) / 2 ; if ( $ arr [ $ m ] < $ i ) { $ findex = $ m ; $ l = $ m + 1 ; } else $ r = $ m - 1 ; } $ l = 1 ; $ r = $ n ; $ flag = false ; while ( $ l <= $ r ) { $ m = ( $ r + $ l ) / 2 ; if ( $ arr [ $ m ] > $ i ) { $ lindex = $ m ; $ r = $ m - 1 ; } else $ l = $ m + 1 ; } if ( $ sum [ $ findex ] == $ sum [ $ n - 1 ] - $ sum [ $ lindex - 1 ] ) { $ flag = true ; break ; } } if ( $ flag == true ) echo " Yes " ; else echo " No " ; } $ arr = array ( 1 , 2 , 2 , 5 ) ; $ n = sizeof ( $ arr ) ; IfExists ( $ arr , $ n ) ; ? >