GeeksforGeeks App
Open App
Browser
Continue

# Sort given array to descending-lowest-ascending form

Given an array arr[] of size N and an integer K, the task is to sort the array such that the first K elements of the array are in descending order and the last N – K elements of the array are in ascending order.

Examples:

Input: arr[]= {7, 6, 8, 9, 0, 1, 2, 2, 1, 8, 9, 6, 7}, K = 6
Output: 9 9 8 8 7 7 0 1 1 2 2 6 6
Explanation:
The first K (= 6) elements of the sorted array are {9, 9, 8, 8, 7, 7}, which are in descending order.
The last N – K (= 6) elements of the sorted array are {0, 1, 1, 2, 2, 6, 6}, which are in ascending order.
Therefore, the required output is 9 9 8 8 7 7 0 1 1 2 2 6 6

Input: arr[]= {65, 34, 54, 56, 75, 34, 54, 65, 56, 75, 15}, K = 5
Output: 75 75 65 65 56 56 15 34 34 54 54

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 // C++ program to implement// the above approach #include using namespace std; // Function to sort first K array elements in// descending and last N - K in ascending ordervoid sortArrayInDescAsc(int arr[], int N, int K){    // Sort the array in descending order    sort(arr, arr + N, greater());     // Sort last (N - K) array    // elements in ascending order    sort(arr + K, arr + N);     // Print array elements    for (int i = 0; i < N; i++) {        cout << arr[i] << " ";    }} // Driver Codeint main(){    int arr[] = { 7, 6, 8, 9, 0, 1, 2,                  2, 1, 8, 9, 6, 7 };    int N = sizeof(arr) / sizeof(arr[0]);    int K = 6;    sortArrayInDescAsc(arr, N, K);}

## Java

 // Java program to implement// the above approachimport java.util.*; class GFG{ // Function to sort first K array elements in// descending and last N - K in ascending orderstatic void sortArrayInDescAsc(int arr[], int N,                               int K){         // Sort the array in descending order    Arrays.sort(arr);     for(int i = 0, j = N - 1; i < N / 2; i++)    {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp;        j--;    }     // Sort last (N - K) array    // elements in ascending order    Arrays.sort(arr, K, N);     // Print array elements    for(int i = 0; i < N; i++)    {        System.out.print(arr[i] + " ");    }} // Driver Codepublic static void main(String[] args){    int arr[] = { 7, 6, 8, 9, 0, 1, 2,                  2, 1, 8, 9, 6, 7 };    int N = arr.length;    int K = 6;         sortArrayInDescAsc(arr, N, K);}} // This code is contributed by Amit Katiyar

## Python3

 # Python3 program to implement# the above approach # Function to sort first K array# elements in descending and last# N - K in ascending orderdef sortArrayInDescAsc(arr, N, K):         # Sort the array in descending order    arr = sorted(arr)    arr = arr[::-1]     # Sort last (N - K) array    # elements in ascending order    for i in arr[:K]:        print(i, end = " ")    for i in reversed(arr[K:]):        print(i, end = " ") # Driver Codeif __name__ == '__main__':         arr = [ 7, 6, 8, 9, 0, 1,            2, 2, 1, 8, 9, 6, 7 ]    N = len(arr)    K = 6         sortArrayInDescAsc(arr, N, K) # This code is contributed by mohit kumar 29

## C#

 // C# program to implement// the above approach using System; class GFG{      // Function to sort first K array elements in// descending and last N - K in ascending orderstatic void sortArrayInDescAsc(int[] arr, int N,                               int K){         // Sort the array in descending order    Array.Sort(arr);    Array.Reverse(arr);      // Sort last (N - K) array    // elements in ascending order    int temp = 0;         for(int i = K; i < N; i++)    {        for(int j = i + 1; j < N; j++)        {                if (arr[i] > arr[j])            {                temp = arr[i];                   arr[i] = arr[j];                   arr[j] = temp;               }            }        }      // Print array elements    for(int i = 0; i < N; i++)    {        Console.Write(arr[i] + " ");    }}  // Driver codepublic static void Main(){    int[] arr = { 7, 6, 8, 9, 0, 1, 2,                  2, 1, 8, 9, 6, 7 };    int N = arr.Length;    int K = 6;         sortArrayInDescAsc(arr, N, K);}} // This code is contributed by sanjoy_62

## Javascript



Output:

9 9 8 8 7 7 0 1 1 2 2 6 6

Time Complexity: O(N * log(N))
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up