Partitioning into two contiguous element subarrays with equal sums | Class 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 class . ; Driver Code
class Data : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . element = - 1 NEW_LINE self . position = - 1 NEW_LINE DEDENT DEDENT def findMinElement ( arr , n ) : NEW_LINE INDENT result = Data ( ) NEW_LINE prefixSum = [ 0 ] * n NEW_LINE suffixSum = [ 0 ] * n NEW_LINE prefixSum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] suffixSum [ n - 1 ] = arr [ n - 1 ] NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] NEW_LINE DEDENT mini = suffixSum [ 0 ] NEW_LINE pos = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < mini : NEW_LINE INDENT mini = abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) NEW_LINE if suffixSum [ i + 1 ] < prefixSum [ i ] : NEW_LINE INDENT pos = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT pos = i NEW_LINE DEDENT DEDENT DEDENT result . element = mini NEW_LINE result . position = pos NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE values = Data ( ) NEW_LINE values = findMinElement ( arr , n ) NEW_LINE print ( " Minimum ▁ element ▁ : " , values . element ,   " Position : " , values . position ) NEW_LINE DEDENT