Sort an Array such that the odd numbers appear first followed by the even numbers . The odd numbers in ascending order and the even numbers in descending order.

Objective: Given an array of intergers, sort it such that the odd numbers appear first followed by the even numbers . The odd numbers in ascending order and the even numbers in descending order.

Input: An Array of Integers

Example:

Input Array : 1 2 3 4 5 6 7 8 9 10
Output : 1 3 5 7 9 10 8 6 4 2

Approach:

  • First separate the odd numbers and even numbers, put odd numbers first followed by even numbers using quick sort technique. (Click here to read about quick sort)
  • Now sort the odd numbers in ascending order
  • Sort the even numbers in descending order.

Complete Code:


Output:

Original Array :
1 2 3 4 5 6 7 8 9 10
Output Array :
1 3 5 7 9 10 8 6 4 2

__________________________________________________
Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.
__________________________________________________

You may also like...

  • danielwx

    Thx for sharing! What is the time btw?

%d bloggers like this: