Split array into two subsequences having minimum count of pairs with sum equal to X | Function to split the array into two subsequences ; Stores the two subsequences ; Flag to set / reset to split arrays elements alternately into two arrays ; Traverse the given array ; If 2 * arr [ i ] is less than X ; Push element into the first array ; If 2 * arr [ i ] is greater than X ; Push element into the second array ; If 2 * arr [ i ] is equal to X ; Alternatively place the elements into the two arrays ; Print both the arrays ; Driver Code ; Size of the array ; Function Call
def solve ( arr , N , X ) : NEW_LINE INDENT A = [ ] NEW_LINE B = [ ] NEW_LINE c = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( ( 2 * arr [ i ] ) < X ) : NEW_LINE INDENT A . append ( arr [ i ] ) NEW_LINE DEDENT elif ( ( 2 * arr [ i ] ) > X ) : NEW_LINE INDENT B . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( c % 2 == 0 ) : NEW_LINE INDENT A . append ( arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT B . append ( arr [ i ] ) NEW_LINE DEDENT c += 1 NEW_LINE DEDENT DEDENT print ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " , end = " ▁ " ) NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE print ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " , end = " ▁ " ) NEW_LINE for i in range ( len ( B ) ) : NEW_LINE INDENT print ( B [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 ] NEW_LINE X = 7 NEW_LINE N = len ( arr ) NEW_LINE solve ( arr , N , X ) NEW_LINE DEDENT