Rearrange array to make product of prefix sum array non zero | Function to rearrange array that satisfies the given condition ; Stores sum of elements of the given array ; Calculate totalSum ; If the totalSum is equal to 0 ; No possible way to rearrange array ; If totalSum exceeds 0 ; Rearrange the array in descending order ; Otherwise ; Rearrange the array in ascending order ; Driver Code
def rearrangeArr ( arr , N ) : NEW_LINE INDENT totalSum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT totalSum += arr [ i ] NEW_LINE DEDENT if ( totalSum == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT elif ( totalSum > 0 ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE print ( * arr , sep = ' ▁ ' ) NEW_LINE DEDENT else : NEW_LINE INDENT arr . sort ( ) NEW_LINE print ( * arr , sep = ' ▁ ' ) NEW_LINE DEDENT DEDENT arr = [ 1 , - 1 , - 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE rearrangeArr ( arr , N ) ; NEW_LINE