Sort even numbers in order and then odd in descending in an array
Last Updated :
06 May, 2025
Given an array arr[] of positive integers. The task is to sort them so that the first part of the array contains odd numbers sorted in descending order, and the rest of the portion contains even numbers sorted in ascending order.
Examples:
Input: arr[] = [1, 2, 3, 5, 4, 7, 10]
Output: [7, 5, 3, 1, 2, 4, 10]
Explanation: 7 5 3 1 are odds in descending order and 2 4 10 are evens in ascending order.
Input: arr[] = [0, 4, 5, 3, 7, 2, 1]
Output: [7, 5, 3, 1, 0, 2, 4]
Explanation: 7 5 3 1 are odds in descending order and 0 2 4 are evens in ascending order.
Input: arr[] = [6, 9, 2, 8, 3, 7]
Output: [9, 7, 3, 2, 6, 8]
[Naive Approach] Using Two Auxiliary Arrays - O(n*log(n)) Time and O(n) Space
The idea is to first separate all odd and even numbers into two different arrays. Then, we sort the odd numbers in descending order and the even numbers in ascending order. Finally, we reconstruct the original array by placing all sorted odd numbers first, followed by the sorted even numbers.
C++
// C++ program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Naive Approach
#include <bits/stdc++.h>
using namespace std;
// Function to sort array as per the condition
void sortIt(vector<int>& arr) {
vector<int> odds;
vector<int> evens;
// Separate the elements into odds and evens
for (int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 == 1) {
odds.push_back(arr[i]);
} else {
evens.push_back(arr[i]);
}
}
// Sort odds in descending order
sort(odds.begin(), odds.end(), greater<int>());
// Sort evens in ascending order
sort(evens.begin(), evens.end());
// Merge both back into the original array
int idx = 0;
for (int i = 0; i < odds.size(); i++) {
arr[idx++] = odds[i];
}
for (int i = 0; i < evens.size(); i++) {
arr[idx++] = evens[i];
}
}
// Driver code
int main() {
vector<int> arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
cout << x << " ";
}
return 0;
}
Java
// Java program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Naive Approach
import java.util.*;
class GfG {
// Function to sort array as per the condition
static void sortIt(int[] arr) {
List<Integer> odds = new ArrayList<>();
List<Integer> evens = new ArrayList<>();
// Separate the elements into odds and evens
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 1) {
odds.add(arr[i]);
} else {
evens.add(arr[i]);
}
}
// Sort odds in descending order
Collections.sort(odds, Collections.reverseOrder());
// Sort evens in ascending order
Collections.sort(evens);
// Merge both back into the original array
int idx = 0;
for (int i = 0; i < odds.size(); i++) {
arr[idx++] = odds.get(i);
}
for (int i = 0; i < evens.size(); i++) {
arr[idx++] = evens.get(i);
}
}
public static void main(String[] args) {
int[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
System.out.print(x + " ");
}
}
}
Python
# Python program to sort array such that
# odds are in descending order followed
# by evens in ascending order using
# Naive Approach
def sortIt(arr):
odds = []
evens = []
# Separate the elements into odds and evens
for i in range(len(arr)):
if arr[i] % 2 == 1:
odds.append(arr[i])
else:
evens.append(arr[i])
# Sort odds in descending order
odds.sort(reverse=True)
# Sort evens in ascending order
evens.sort()
# Merge both back into the original array
idx = 0
for i in range(len(odds)):
arr[idx] = odds[i]
idx += 1
for i in range(len(evens)):
arr[idx] = evens[i]
idx += 1
if __name__ == "__main__":
arr = [1, 2, 3, 5, 4, 7, 10]
sortIt(arr)
for x in arr:
print(x, end=" ")
C#
// C# program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Naive Approach
using System;
using System.Collections.Generic;
class GfG {
// Function to sort array as per the condition
static void sortIt(int[] arr) {
List<int> odds = new List<int>();
List<int> evens = new List<int>();
// Separate the elements into odds and evens
for (int i = 0; i < arr.Length; i++) {
if (arr[i] % 2 == 1) {
odds.Add(arr[i]);
} else {
evens.Add(arr[i]);
}
}
// Sort odds in descending order
odds.Sort((a, b) => b - a);
// Sort evens in ascending order
evens.Sort();
// Merge both back into the original array
int idx = 0;
for (int i = 0; i < odds.Count; i++) {
arr[idx++] = odds[i];
}
for (int i = 0; i < evens.Count; i++) {
arr[idx++] = evens[i];
}
}
static void Main() {
int[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
foreach (int x in arr) {
Console.Write(x + " ");
}
}
}
JavaScript
// JavaScript program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Naive Approach
function sortIt(arr) {
let odds = [];
let evens = [];
// Separate the elements into odds and evens
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 1) {
odds.push(arr[i]);
} else {
evens.push(arr[i]);
}
}
// Sort odds in descending order
odds.sort((a, b) => b - a);
// Sort evens in ascending order
evens.sort((a, b) => a - b);
// Merge both back into the original array
let idx = 0;
for (let i = 0; i < odds.length; i++) {
arr[idx++] = odds[i];
}
for (let i = 0; i < evens.length; i++) {
arr[idx++] = evens[i];
}
}
// Driver Code
let arr = [1, 2, 3, 5, 4, 7, 10];
sortIt(arr);
for (let x of arr) {
process.stdout.write(x + " ");
}
[Expected Approach - 1] Using Partition Method - O(n*log(n)) Time and O(1) Space
The idea is to partition the array such that all odd numbers are placed before the even numbers. Once partitioned, we can independently sort the odd and even parts: odds in descending and evens in ascending order. This avoids using extra space and gives an efficient in-place solution.
Steps to implement the above idea:
- Declare an integer left = 0 to track the position for placing odd numbers during partition.
- Traverse the array and swap current element with arr[left] if it is an odd number.
- Increment left after every successful odd placement to maintain correct boundary between odds and evens.
- After traversal, the array is partitioned with all odds on the left and evens on the right.
- Apply descending sort on the subarray from index 0 to left - 1 for odd numbers.
- Apply ascending sort on the subarray from index left to n - 1 for the even numbers.
- Finally, return the array.
C++
// C++ program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Partition Method
#include <bits/stdc++.h>
using namespace std;
// Function to sort array as per the condition
void sortIt(vector<int>& arr) {
int n = arr.size();
int left = 0;
// Partition: Move all odds to the front
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
swap(arr[i], arr[left]);
left++;
}
}
// Sort odds in descending order
sort(arr.begin(), arr.begin() + left, greater<int>());
// Sort evens in ascending order
sort(arr.begin() + left, arr.end());
}
// Driver code
int main() {
vector<int> arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
cout << x << " ";
}
return 0;
}
Java
// Java program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Partition Method
import java.util.*;
class GfG {
// Function to sort array as per the condition
static void sortIt(int[] arr) {
int n = arr.length;
int left = 0;
// Partition: Move all odds to the front
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
int temp = arr[i];
arr[i] = arr[left];
arr[left] = temp;
left++;
}
}
// Sort odds in descending order
Arrays.sort(arr, 0, left);
for (int i = 0; i < left / 2; i++) {
int temp = arr[i];
arr[i] = arr[left - 1 - i];
arr[left - 1 - i] = temp;
}
// Sort evens in ascending order
Arrays.sort(arr, left, n);
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
System.out.print(x + " ");
}
}
}
Python
# Python program to sort array such that
# odds are in descending order followed
# by evens in ascending order using
# Partition Method
# Function to sort array as per the condition
def sortIt(arr):
n = len(arr)
left = 0
# Partition: Move all odds to the front
for i in range(n):
if arr[i] % 2 == 1:
arr[i], arr[left] = arr[left], arr[i]
left += 1
# Sort odds in descending order
arr[:left] = sorted(arr[:left], reverse=True)
# Sort evens in ascending order
arr[left:] = sorted(arr[left:])
if __name__ == "__main__":
arr = [1, 2, 3, 5, 4, 7, 10]
sortIt(arr)
for x in arr:
print(x, end=" ")
C#
// C# program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Partition Method
using System;
class GfG {
// Function to sort array as per the condition
public static void sortIt(int[] arr) {
int n = arr.Length;
int left = 0;
// Partition: Move all odds to the front
for (int i = 0; i < n; i++) {
if (arr[i] % 2 == 1) {
int temp = arr[i];
arr[i] = arr[left];
arr[left] = temp;
left++;
}
}
// Sort odds in descending order
Array.Sort(arr, 0, left);
Array.Reverse(arr, 0, left);
// Sort evens in ascending order
Array.Sort(arr, left, n - left);
}
// Driver code
public static void Main() {
int[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
foreach (int x in arr) {
Console.Write(x + " ");
}
}
}
JavaScript
// JavaScript program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Partition Method
// Function to sort array as per the condition
function sortIt(arr) {
let n = arr.length;
let left = 0;
// Partition: Move all odds to the front
for (let i = 0; i < n; i++) {
if (arr[i] % 2 === 1) {
let temp = arr[i];
arr[i] = arr[left];
arr[left] = temp;
left++;
}
}
// Sort odds in descending order
arr.slice(0, left)
.sort((a, b) => b - a)
.forEach((val, i) => arr[i] = val);
// Sort evens in ascending order
arr.slice(left)
.sort((a, b) => a - b)
.forEach((val, i) => arr[left + i] = val);
}
// Driver code
let arr = [1, 2, 3, 5, 4, 7, 10];
sortIt(arr);
for (let x of arr) {
process.stdout.write(x + " ");
}
[Expected Approach - 2] Using Negative Multiplication - O(n*log(n)) Time and O(1) Space
The idea is to utilize the fact that negative odd numbers will be treated as smaller in ascending sort. So, we convert all odd elements to negative, making larger odd values more negative (e.g., 7 becomes -7). During sorting, these more negative values (i.e., originally bigger odds) are pushed further to the left, achieving descending order for odds. After sorting, we revert negative numbers back to positive, placing odds in descending order followed by evens in ascending order.
Note: This approach fails when the array already contains negative values, as it cannot distinguish between original and transformed negatives, leading to incorrect results.
C++
// C++ program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Negative Multiplication
#include <bits/stdc++.h>
using namespace std;
// Function to sort array as per the condition
void sortIt(vector<int>& arr) {
// Make odd elements negative
for (int i = 0; i < arr.size(); i++) {
if (arr[i] % 2 == 1) {
arr[i] *= -1;
}
}
// Sort entire array
sort(arr.begin(), arr.end());
// Make negative elements (originally odds)
// positive again
for (int i = 0; i < arr.size(); i++) {
if (arr[i] < 0) {
arr[i] *= -1;
}
}
}
// Driver code
int main() {
vector<int> arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
cout << x << " ";
}
return 0;
}
Java
// Java program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Negative Multiplication
import java.util.*;
class GfG {
// Function to sort array as per the condition
static void sortIt(int[] arr) {
// Make odd elements negative
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 2 == 1) {
arr[i] *= -1;
}
}
// Sort entire array
Arrays.sort(arr);
// Make negative elements (originally odds)
// positive again
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
arr[i] *= -1;
}
}
}
// Driver code
public static void main(String[] args) {
int[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
System.out.print(x + " ");
}
}
}
Python
# Python program to sort array such that
# odds are in descending order followed
# by evens in ascending order using
# Negative Multiplication
# Function to sort array as per the condition
def sortIt(arr):
# Make odd elements negative
for i in range(len(arr)):
if arr[i] % 2 == 1:
arr[i] *= -1
# Sort entire array
arr.sort()
# Make negative elements (originally odds)
# positive again
for i in range(len(arr)):
if arr[i] < 0:
arr[i] *= -1
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 5, 4, 7, 10]
sortIt(arr)
for x in arr:
print(x, end=" ")
C#
// C# program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Negative Multiplication
using System;
class GfG {
// Function to sort array as per the condition
public static void sortIt(int[] arr) {
// Make odd elements negative
for (int i = 0; i < arr.Length; i++) {
if (arr[i] % 2 == 1) {
arr[i] *= -1;
}
}
// Sort entire array
Array.Sort(arr);
// Make negative elements (originally odds)
// positive again
for (int i = 0; i < arr.Length; i++) {
if (arr[i] < 0) {
arr[i] *= -1;
}
}
}
// Driver code
public static void Main() {
int[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
foreach (int x in arr) {
Console.Write(x + " ");
}
}
}
JavaScript
// JavaScript program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Negative Multiplication
// Function to sort array as per the condition
function sortIt(arr) {
// Make odd elements negative
for (let i = 0; i < arr.length; i++) {
if (arr[i] % 2 === 1) {
arr[i] *= -1;
}
}
// Sort entire array
arr.sort((a, b) => a - b);
// Make negative elements (originally odds)
// positive again
for (let i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
arr[i] *= -1;
}
}
}
// Driver code
let arr = [1, 2, 3, 5, 4, 7, 10];
sortIt(arr);
for (let x of arr) {
process.stdout.write(x + " ");
}
[Expected Approach - 3] Using Custom Comparator - O(n*log(n)) Time and O(1) Space
The idea is to leverage the inbuilt sort function with a custom comparator to sort the array according to the desired condition. The logic for the comparator is based on how two elements compare under three distinct cases:
- If both elements are even, they should be arranged in ascending order, so the smaller even number comes first.
- If both elements are odd, they should be arranged in descending order, meaning the larger odd number should appear earlier.
- If one element is odd and the other is even, the odd number must come before the even number to maintain the required order.
C++
// C++ program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Custom Comparator
#include <bits/stdc++.h>
using namespace std;
// Custom compare function
bool compare(int a, int b) {
// When both are even: sort in ascending order
if (a % 2 == 0 && b % 2 == 0) {
return a < b;
}
// When both are odd: sort in descending order
if (a % 2 == 1 && b % 2 == 1) {
return a > b;
}
// If one is odd and one is even: odd comes first
return a % 2 == 1;
}
// Function to sort array as per the condition
void sortIt(vector<int>& arr) {
// Sort using the custom comparator
sort(arr.begin(), arr.end(), compare);
}
// Driver code
int main() {
vector<int> arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
cout << x << " ";
}
return 0;
}
Java
// Java program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Custom Comparator
import java.util.*;
class GfG {
// Function to sort array as per the condition
public static void sortIt(Integer[] arr) {
// Sort using the custom comparator
Arrays.sort(arr, new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
// When both are even: sort in ascending order
if (a % 2 == 0 && b % 2 == 0) {
return a - b;
}
// When both are odd: sort in descending order
if (a % 2 == 1 && b % 2 == 1) {
return b - a;
}
// If one is odd and one is even: odd comes first
return (a % 2 == 1) ? -1 : 1;
}
});
}
// Driver code
public static void main(String[] args) {
Integer[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
for (int x : arr) {
System.out.print(x + " ");
}
}
}
Python
# Python program to sort array such that
# odds are in descending order followed
# by evens in ascending order using
# Custom Comparator
from functools import cmp_to_key
# Custom compare function
def compare(a, b):
# When both are even: sort in ascending order
if a % 2 == 0 and b % 2 == 0:
return a - b
# When both are odd: sort in descending order
if a % 2 == 1 and b % 2 == 1:
return b - a
# If one is odd and one is even: odd comes first
return -1 if a % 2 == 1 else 1
# Function to sort array as per the condition
def sortIt(arr):
# Sort using the custom comparator
arr.sort(key=cmp_to_key(compare))
# Driver code
if __name__ == "__main__":
arr = [1, 2, 3, 5, 4, 7, 10]
sortIt(arr)
for x in arr:
print(x, end=" ")
C#
// C# program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Custom Comparator
using System;
using System.Collections.Generic;
class GfG {
// Custom compare function
class CustomComparer : IComparer<int> {
public int Compare(int a, int b) {
// When both are even: sort in ascending order
if (a % 2 == 0 && b % 2 == 0) {
return a - b;
}
// When both are odd: sort in descending order
if (a % 2 == 1 && b % 2 == 1) {
return b - a;
}
// If one is odd and one is even: odd comes first
return (a % 2 == 1) ? -1 : 1;
}
}
// Function to sort array as per the condition
public static void sortIt(int[] arr) {
// Sort using the custom comparator
Array.Sort(arr, new CustomComparer());
}
// Driver code
public static void Main() {
int[] arr = {1, 2, 3, 5, 4, 7, 10};
sortIt(arr);
foreach (int x in arr) {
Console.Write(x + " ");
}
}
}
JavaScript
// JavaScript program to sort array such that
// odds are in descending order followed
// by evens in ascending order using
// Custom Comparator
// Custom compare function
function compare(a, b) {
// When both are even: sort in ascending order
if (a % 2 === 0 && b % 2 === 0) {
return a - b;
}
// When both are odd: sort in descending order
if (a % 2 === 1 && b % 2 === 1) {
return b - a;
}
// If one is odd and one is even: odd comes first
return (a % 2 === 1) ? -1 : 1;
}
// Function to sort array as per the condition
function sortIt(arr) {
// Sort using the custom comparator
arr.sort(compare);
}
// Driver code
let arr = [1, 2, 3, 5, 4, 7, 10];
sortIt(arr);
for (let i = 0; i < arr.length; i++) {
process.stdout.write(arr[i] + " ");
}
Similar Reads
Sort all even numbers in ascending order and then sort all odd numbers in descending order
Given an array arr[] of positive integers. The task is to sort them so that the first part of the array contains odd numbers sorted in descending order, and the rest of the portion contains even numbers sorted in ascending order.Examples: Input: arr[] = [1, 2, 3, 5, 4, 7, 10]Output: [7, 5, 3, 1, 2,
15+ min read
Sort all even numbers in the Array without changing order of odd elements
Given an array arr[] of size N, the task is to sort all the even numbers in the array, without changing the order of odd elementsExamples: Input: arr[] = {4, 7, 2, 11, 15}Output: {2, 7, 4, 11, 15}Explanation: Even numbers are sorted at their corresponding places, without changing the order of odd el
4 min read
Operations to Sort an Array in non-decreasing order
Given an array arr[] of integers of size n, the task is to check if we can sort the given array in non-decreasing order(i, e.arr[i] ⤠arr[i+1]) by using two types of operation by performing any numbers of time: You can choose any index from 1 to n-1(1-based indexing) and increase arr[i] and arr[i+1]
7 min read
Rearrange Odd and Even values in Alternate Fashion in Ascending Order
Given an array of integers (both odd and even), the task is to arrange them in such a way that odd and even values come in alternate fashion in non-decreasing order(ascending) respectively. If the smallest value is Even then we have to print Even-Odd pattern.If the smallest value is Odd then we have
15+ min read
Sort even-placed in increasing and odd-placed in decreasing order
We are given an array of n distinct numbers. The task is to sort all even-placed numbers in increasing and odd-placed numbers in decreasing order. The modified array should contain all sorted even-placed numbers followed by reverse sorted odd-placed numbers.Note that the first element is considered
14 min read
Count number of even and odd length elements in an Array
Given an array arr[] of integers of size N, the task is to find the number elements of the array having even and odd length.Examples: Input: arr[] = {14, 735, 3333, 223222} Output: Number of even length elements = 3 Number of odd length elements = 1 Input: arr[] = {1121, 322, 32, 14783, 44} Output:
5 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 1
11 min read
Sort and separate odd and even numbers in an Array using custom comparator
Given an array arr[], containing N elements, the task is to sort and separate odd and even numbers in an Array using a custom comparator. Example: Input: arr[] = { 5, 3, 2, 8, 7, 4, 6, 9, 1 }Output: 2 4 6 8 1 3 5 7 9 Input: arr[] = { 12, 15, 6, 2, 7, 13, 9, 4 }Output: 2 4 6 12 7 9 13 15 Approach: As
5 min read
Add minimum number to an array so that the sum becomes even
Given an array, write a program to add the minimum number(should be greater than 0) to the array so that the sum of array becomes even. Examples: Input : 1 2 3 4 5 6 7 8 Output : 2 Explanation : Sum of array is 36, so we add minimum number 2 to make the sum even. Input : 1 2 3 4 5 6 7 8 9 Output : 1
8 min read
Sort an array of strings in ascending order with each string sorted in descending order
Given a array of strings S[] of size N (1 ? N ? 105), sort characters of each string in descending order and then print the array of strings in ascending order. Examples: Input: s[] = {"apple", "box", "cat"} Output: pplea tca xob Explanation: Sorting each string in descending order, S[] modifies to
9 min read