Split an array into minimum number of non | Function to split the array into minimum count of subarrays such that each subarray is either non - increasing or non - decreasing ; Initialize variable to keep track of current sequence ; Stores the required result ; Traverse the array , arr [ ] ; If current sequence is neither non - increasing nor non - decreasing ; If previous element is greater ; Update current ; If previous element is equal to the current element ; Update current ; Otherwise ; Update current ; If current sequence is in non - decreasing ; I f previous element is less than or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; If current sequence is Non - Increasing ; If previous element is greater or equal to the current element ; Otherwise ; Update current as N and increment answer by 1 ; Print the answer ; Driver Code ; Given array ; Given size of array
def minimumSubarrays ( arr , n ) : NEW_LINE INDENT current = ' N ' NEW_LINE answer = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( current == ' N ' ) : NEW_LINE INDENT if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT current = ' D ' NEW_LINE DEDENT elif ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT current = ' N ' NEW_LINE DEDENT else : NEW_LINE INDENT current = ' I ' NEW_LINE DEDENT DEDENT elif ( current == ' I ' ) : NEW_LINE INDENT if ( arr [ i ] >= arr [ i - 1 ] ) : NEW_LINE INDENT current = ' I ' NEW_LINE DEDENT else : NEW_LINE INDENT current = ' N ' NEW_LINE answer += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( arr [ i ] <= arr [ i - 1 ] ) : NEW_LINE INDENT current = ' D ' NEW_LINE DEDENT else : NEW_LINE INDENT current = ' N ' NEW_LINE answer += 1 NEW_LINE DEDENT DEDENT DEDENT print ( answer ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 9 , 5 , 4 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE minimumSubarrays ( arr , n ) NEW_LINE DEDENT