Partitioning into two contiguous element subarrays with equal sums | Structure to store the minimum element and its position ; initialize prefix and suffix sum arrays with 0 ; add current element to Sum ; add current element to Sum ; initialize the minimum element to be a large value ; check for the minimum absolute difference between current prefix sum and the next suffix sum element ; if prefixsum has a greater value then position is the next element , else it 's the same element. ; return the data in struct . ; Driver Code
class data { constructor ( element , position ) { this . element = element ; this . position = position ; } } ; function findMinElement ( arr , n ) { let result = new data ( ) ; let prefixSum = new Array ( n ) ; let suffixSum = new Array ( n ) ; prefixSum [ 0 ] = arr [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) { suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; } let min = suffixSum [ 0 ] ; let pos = 0 ; for ( let i = 0 ; i < n - 1 ; i ++ ) { if ( Math . abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < min ) { min = Math . abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) ; if ( suffixSum [ i + 1 ] < prefixSum [ i ] ) pos = i + 1 ; else pos = i ; } } result . element = min ; result . position = pos ; return result ; } let arr = [ 10 , 1 , 2 , 3 , 4 ] ; let n = arr . length ; let values ; values = findMinElement ( arr , n ) ; document . write ( " " + values . element + " " + values . position ) ;