Sum of all Subarrays | Set 1 | Computes sum all sub - array ; Pick starting point ; Pick ending point ; sum subarray between current starting and ending points ; Driver Code
< ? php function SubArraySum ( $ arr , $ n ) { $ result = 0 ; $ temp = 0 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { $ temp = 0 ; for ( $ j = $ i ; $ j < $ n ; $ j ++ ) { $ temp += $ arr [ $ j ] $ result += $ temp ; } } return $ result ; } $ arr = array ( 1 , 2 , 3 ) ; $ n = sizeof ( $ arr ) ; echo " Sum ▁ of ▁ SubArray ▁ : ▁ " , SubArraySum ( $ arr , $ n ) , " STRNEWLINE " ; ? >