# Sort first k values in ascending order and remaining n-k values in descending order

Given an array of size n, arrange the first k elements of the array in ascending order and the remaining n-k elements in descending order.

Examples:

Input: arr[] = {5, 4, 6, 2, 1, 3, 8, 9, -1}, k = 4
Output: 2 4 5 6 9 8 3 1 -1

Input: arr[] = {5, 4, 6}, k = 2
Output: 4 5 6

Algorithm:

1. Store the first k elements in an array and sort that in ascending order.
2. Store the remaining n-k elements in an array and sort that in descending order.
3. Merge the two arrays by adding the elements from the second array in reverse order.

## C++

 `// C++ program to sort first k elements` `// in increasing order and remaining` `// n-k elements in decreasing` `#include ` `using` `namespace` `std;`   `// Function to sort the array` `void` `printOrder(``int` `arr[], ``int` `n, ``int` `k)` `{` `    ``int` `len1 = k, len2 = n - k;` `    ``int` `arr1[k], arr2[n - k];`   `    ``// Store the k elements in an array` `    ``for` `(``int` `i = 0; i < k; i++)` `        ``arr1[i] = arr[i];`   `    ``// Store the remaining n-k elements in an array` `    ``for` `(``int` `i = k; i < n; i++)` `        ``arr2[i - k] = arr[i];`   `    ``// sorting the array from 0 to k-1 places` `    ``sort(arr1, arr1 + len1);`   `    ``// sorting the array from k to n places` `    ``sort(arr2, arr2 + len2);`   `    ``// storing the values in the final array arr` `    ``for` `(``int` `i = 0; i < n; i++) {` `        ``if` `(i < k)` `            ``arr[i] = arr1[i];`   `        ``else` `{` `            ``arr[i] = arr2[len2 - 1];` `            ``len2--;` `        ``}` `    ``}` `    ``// printing the array` `    ``for` `(``int` `i = 0; i < n; i++)` `        ``cout << arr[i] << ``" "``;` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };` `    ``int` `k = 4;`   `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``printOrder(arr, n, k);`   `    ``return` `0;` `}`

## Java

 `// Java program to sort first k elements` `// in increasing order and remaining` `// n-k elements in decreasing` `import` `java.util.*;`   `class` `GFG {` `    ``// function to print half of the array in` `    ``// ascending order and the other half in` `    ``// descending order` `    ``static` `void` `printOrder(``int``[] arr, ``int` `n, ``int` `k)` `    ``{` `        ``int` `len1 = k, len2 = n - k;` `        ``int``[] arr1 = ``new` `int``[k];` `        ``int``[] arr2 = ``new` `int``[n - k];`   `        ``// Store the k elements in an array` `        ``for` `(``int` `i = ``0``; i < k; i++)` `            ``arr1[i] = arr[i];`   `        ``// Store the remaining n-k elements in an array` `        ``for` `(``int` `i = k; i < n; i++)` `            ``arr2[i - k] = arr[i];`   `        ``// sorting the array1 from 0 to k-1 places` `        ``Arrays.sort(arr1, ``0``, k);`   `        ``// sorting the array2 from 0 to n-k places` `        ``Arrays.sort(arr2, ``0``, n - k);`   `        ``// storing the values in the final array arr` `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``if` `(i < k)` `                ``arr[i] = arr1[i];`   `            ``else` `{` `                ``arr[i] = arr2[len2 - ``1``];` `                ``len2--;` `            ``}` `        ``}` `        ``// printing the array` `        ``for` `(``int` `i = ``0``; i < n; i++) {` `            ``System.out.print(arr[i] + ``" "``);` `        ``}` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int` `arr[] = { ``5``, ``4``, ``6``, ``2``, ``1``, ``3``, ``8``, ``9``, -``1` `};` `        ``int` `k = ``4``;` `        ``int` `n = arr.length;` `        ``printOrder(arr, n, k);` `    ``}` `}`

## Python3

 `# Python 3 program to sort first ` `# k elements in increasing order ` `# and remaining n-k elements in ` `# decreasing`   `# Function to sort the array` `def` `printOrder(arr, n, k):`   `    ``len1 ``=` `k` `    ``len2 ``=` `n ``-` `k` `    ``arr1 ``=` `[``0``] ``*` `k` `    ``arr2 ``=` `[``0``] ``*` `(n ``-` `k)`   `    ``# Store the k elements ` `    ``# in an array` `    ``for` `i ``in` `range``(k):` `        ``arr1[i] ``=` `arr[i]`   `    ``# Store the remaining n-k ` `    ``# elements in an array` `    ``for` `i ``in` `range``(k, n):` `        ``arr2[i ``-` `k] ``=` `arr[i]`   `    ``# sorting the array from` `    ``# 0 to k-1 places` `    ``arr1.sort()`   `    ``# sorting the array from` `    ``# k to n places` `    ``arr2.sort()`   `    ``# storing the values in the ` `    ``# final array arr` `    ``for` `i ``in` `range``(n):` `        ``if` `(i < k):` `            ``arr[i] ``=` `arr1[i]`   `        ``else` `:` `            ``arr[i] ``=` `arr2[len2 ``-` `1``]` `            ``len2 ``-``=` `1` `    `  `    ``# printing the array` `    ``for` `i ``in` `range``(n):` `        ``print``(arr[i], end ``=` `" "``)`   `# Driver code` `if` `__name__ ``=``=` `"__main__"``:` `    ``arr ``=` `[ ``5``, ``4``, ``6``, ``2``, ``1``,` `            ``3``, ``8``, ``9``, ``-``1` `]` `    ``k ``=` `4`   `    ``n ``=` `len``(arr)`   `    ``printOrder(arr, n, k)`   `# This code is contributed` `# by ChitraNayal`

## C#

 `// C# program to sort first k elements` `// in increasing order and remaining` `// n-k elements in decreasing` `using` `System;`   `class` `GFG {` `    ``// function to print half of the array in` `    ``// ascending order and the other half in` `    ``// descending order` `    ``static` `void` `printOrder(``int``[] arr,` `                           ``int` `n, ``int` `k)` `    ``{` `        ``int` `len2 = n - k;` `        ``int``[] arr1 = ``new` `int``[k];` `        ``int``[] arr2 = ``new` `int``[n - k];`   `        ``// Store the k elements in an array` `        ``for` `(``int` `i = 0; i < k; i++)` `            ``arr1[i] = arr[i];`   `        ``// Store the remaining n-k` `        ``// elements in an array` `        ``for` `(``int` `i = k; i < n; i++)` `            ``arr2[i - k] = arr[i];`   `        ``// sorting the array from` `        ``// 0 to k-1 places` `        ``Array.Sort(arr1, 0, k);`   `        ``// sorting the array from k to n places` `        ``Array.Sort(arr2, 0, n - k);`   `        ``// storing the values in` `        ``// the final array arr` `        ``for` `(``int` `i = 0; i < n; i++) {` `            ``if` `(i < k)` `                ``arr[i] = arr1[i];`   `            ``else` `{` `                ``arr[i] = arr2[len2 - 1];` `                ``len2--;` `            ``}` `        ``}`   `        ``// printing the array` `        ``for` `(``int` `i = 0; i < n; i++) {` `            ``Console.Write(arr[i] + ``" "``);` `        ``}` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main()` `    ``{` `        ``int``[] arr = { 5, 4, 6, 2, 1,` `                      ``3, 8, 9, -1 };` `        ``int` `k = 4;` `        ``int` `n = arr.Length;` `        ``printOrder(arr, n, k);` `    ``}` `}`   `// This code is contributed by Subhadeep`

## PHP

 ``

## Javascript

 ``

Output:

`2 4 5 6 9 8 3 1 -1`

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

Efficient Approach: The idea is simple, sort the first k elements in increasing order and remaining n-k elements in decreasing using library function.

## C++

 `// C++ program to sort first k elements` `// in increasing order and remaining` `// n-k elements in decreasing` `#include ` `using` `namespace` `std;`   `// function to sort the array` `void` `printOrder(``int` `arr[], ``int` `n, ``int` `k)` `{` `    ``// Sort first k elements in ascending order` `    ``sort(arr, arr + k);`   `    ``// Sort remaining n-k elements in descending order` `    ``sort(arr + k, arr + n, greater<``int``>());` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };` `    ``int` `k = 4;` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr[0]);`   `    ``printOrder(arr, n, k);`   `    ``for` `(``int` `i = 0; i < n; i++)` `        ``cout << arr[i] << ``" "``;`   `    ``return` `0;` `}`

## Java

 `// Java program to sort first k elements` `// in increasing order and remaining` `// n-k elements in decreasing` `import` `java.util.*;`   `public` `class` `SortExample {` `    ``static` `void` `printOrder(Integer[] arr, ``int` `k)` `    ``{` `        ``int` `n = arr.length;`   `        ``// Sort first k elements in ascending order` `        ``Arrays.sort(arr, ``0``, k);`   `        ``// Sort remaining n-k elements in descending order` `        ``Arrays.sort(arr, k, n, Collections.reverseOrder());` `    ``}`   `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``// Our arr contains 8 elements` `        ``Integer[] arr = { ``5``, ``4``, ``6``, ``2``, ``1``, ``3``, ``8``, ``9``, -``1` `};` `        ``int` `k = ``4``;` `        ``printOrder(arr, k);` `        ``System.out.printf(``"%s"``, Arrays.toString(arr));` `    ``}` `}`

## Python3

 `# Python3 program to sort first k elements` `# in increasing order and remaining` `# n-k elements in decreasing`   `# function to sort the array` `def` `printOrder(arr, n, k):`   `    ``# Sort first k elements in ascending order` `    ``a ``=` `arr[``0``:k];` `    ``a.sort();`   `    ``# Sort remaining n-k elements in descending order` `    ``b ``=` `arr[k:n];` `    ``b.sort();` `    ``b.reverse();` `    ``return` `a ``+` `b;`   `# Driver code` `arr ``=` `[ ``5``, ``4``, ``6``, ``2``, ``1``, ``3``, ``8``, ``9``, ``-``1` `];` `k ``=` `4``;` `n ``=` `len``(arr);`   `arr ``=` `printOrder(arr, n, k);`   `for` `i ``in` `range``(n):` `    ``print``(arr[i], end ``=``" "``);`   `# This code is contributed by mits`

## C#

 `// C# program to sort first k elements` `// in increasing order and remaining` `// n-k elements in decreasing` `using` `System;`   `public` `class` `SortExample {` `    ``static` `void` `printOrder(``int``[] arr, ``int` `k)` `    ``{` `        ``int` `n = arr.Length;`   `        ``// Sort first k elements in ascending order` `        ``Array.Sort(arr, 0, k);`   `        ``// Sort remaining n-k elements in descending order` `        ``Array.Sort(arr, k, n - k);` `        ``Array.Reverse(arr, k, n - k);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `Main(String[] args)` `    ``{` `        ``// Our arr contains 8 elements` `        ``int``[] arr = { 5, 4, 6, 2, 1, 3, 8, 9, -1 };` `        ``int` `k = 4;` `        ``printOrder(arr, k);` `        ``Console.Write(``"{0}"``, String.Join(``" "``, arr));` `    ``}` `}`   `// This code contributed by Rajput-Ji`

## PHP

 ``

## Javascript

 ``

Output:

`2 4 5 6 9 8 3 1 -1`

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

Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!

Previous
Next