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 6Input: 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:
- Sort the array in descending order.
- Sort the last (N – K) elements of the array in ascending order.
- Print the array.
Below is the implementation of the above approach:
C++
// C++ program to implement// the above approach#include <bits/stdc++.h>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<int>()); // 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
<script> // Javascript program to implement// the above approach// Function to sort first K array elements in// descending and last N - K in ascending orderfunction sortArrayInDescAsc(arr, N, K){ // Sort the array in descending order arr.sort((b,a)=> a-b) var temp = 0; for(var i = K; i < N; i++) { for(var j = i + 1; j < N; j++) { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } // Print array elements arr.forEach(element => { document.write(element+" "); });}// Driver Codevar arr = [7, 6, 8, 9, 0, 1, 2, 2, 1, 8, 9, 6, 7 ];var N = arr.length;var K = 6;sortArrayInDescAsc(arr, N, K);//This code is contributed by rrrtnx.</script> |
9 9 8 8 7 7 0 1 1 2 2 6 6
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



