Find Kth element in an array containing odd elements first and then even elements | Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code
def getNumber ( n , k ) : NEW_LINE INDENT arr = [ 0 ] * n ; NEW_LINE i = 0 ; NEW_LINE odd = 1 ; NEW_LINE while ( odd <= n ) : NEW_LINE INDENT arr [ i ] = odd ; NEW_LINE i += 1 ; NEW_LINE odd += 2 ; NEW_LINE DEDENT even = 2 ; NEW_LINE while ( even <= n ) : NEW_LINE INDENT arr [ i ] = even ; NEW_LINE i += 1 ; NEW_LINE even += 2 ; NEW_LINE DEDENT return arr [ k - 1 ] ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE k = 5 ; NEW_LINE print ( getNumber ( n , k ) ) ; NEW_LINE DEDENT