Sort first k values in ascending order and remaining n-k values in descending order
Last Updated :
23 Jun, 2022
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:
- Store the first k elements in an array and sort that in ascending order.
- Store the remaining n-k elements in an array and sort that in descending order.
- 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 <bits/stdc++.h>
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
<?php
// PHP program to sort first
// k elements in increasing order
// and remaining n-k elements in
// decreasing
// Function to sort the array
function printOrder($arr, $n, $k)
{
$len1 = $k;
$len2 = $n - $k;
$arr1 = array_fill(0, $k, 0);
$arr2 = array_fill(0, ($n - $k), 0);
// Store the k elements
// in an array
for ($i = 0; $i < $k; $i++)
$arr1[$i] = $arr[$i];
// Store the remaining n-k
// elements in an array
for ($i = $k; $i < $n; $i++)
$arr2[$i - $k] = $arr[$i];
// sorting the array from
// 0 to k-1 places
sort($arr1);
// sorting the array from
// k to n places
sort($arr2);
// storing the values in the
// final array arr
for ($i = 0; $i < $n; $i++)
if ($i < $k)
$arr[$i] = $arr1[$i];
else
{
$arr[$i] = $arr2[$len2 - 1];
$len2 -= 1;
}
// printing the array
for ($i = 0; $i < $n; $i++)
print($arr[$i] . " ");
}
// Driver code
$arr = array( 5, 4, 6, 2, 1, 3, 8, 9, -1 );
$k = 4;
$n = count($arr);
printOrder($arr, $n, $k);
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
// Function to print half of the array in
// ascending order and the other half in
// descending order
function printOrder(arr, n, k)
{
let len1 = k, len2 = n - k;
let arr1 = new Array(k);
let arr2 = new Array(n - k);
// Store the k elements in an array
for(let i = 0; i < k; i++)
arr1[i] = arr[i];
// Store the remaining n-k
// elements in an array
for(let i = k; i < n; i++)
arr2[i - k] = arr[i];
// Sorting the array from 0 to k-1 places
arr1.sort(function(a, b){return a - b;});
// Sorting the array from k to n places
arr2.sort(function(a, b){return a - b;});
// Storing the values in the final array arr
for(let i = 0; i < n; i++)
{
if (i < k)
arr[i] = arr1[i];
else
{
arr[i] = arr2[len2 - 1];
len2--;
}
}
// Printing the array
for(let i = 0; i < n; i++)
{
document.write(arr[i] + " ");
}
}
// Driver code
let arr = [ 5, 4, 6, 2, 1, 3, 8, 9, -1 ];
let k = 4;
let n = arr.length;
printOrder(arr, n, k);
// This code is contributed by rag2127
</script>
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 <bits/stdc++.h>
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
<?php
// PHP program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
// function to sort the array
function printOrder($arr, $n, $k)
{
// Sort first k elements in ascending order
$a= array_slice($arr, 0, $k);
sort($a);
// Sort remaining n-k elements in descending order
$b = array_slice($arr, $k, $n);
sort($b);
$b = array_reverse($b);
unset($arr);
$arr = $a;
return array_merge($arr, $b);
}
// Driver code
$arr = array( 5, 4, 6, 2, 1, 3, 8, 9, -1 );
$k = 4;
$n = count($arr);
$arr=printOrder($arr, $n, $k);
for ($i = 0; $i < $n; $i++)
echo $arr[$i]." ";
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript program to sort first k elements
// in increasing order and remaining
// n-k elements in decreasing
function printOrder(arr, k)
{
let n = arr.length;
// Sort first k elements in ascending order
// Sort remaining n-k elements in descending order
arr = arr.slice(0, k).sort(
function(a, b){return a - b;}).concat(
arr.slice(k, n).sort(function(a, b){return b - a;}));
return arr;
}
// Driver code
// Our arr contains 8 elements
let arr = [ 5, 4, 6, 2, 1, 3, 8, 9, -1 ];
let k = 4;
arr = printOrder(arr, k);
document.write(arr.join(" "));
// This code is contributed by avanitrachhadiya2155
</script>
Output: 2 4 5 6 9 8 3 1 -1
Time Complexity: O(N * log(N))
Auxiliary Space: O(1)
Similar Reads
Number of steps to sort the array by changing order of three elements in each step Given an array arr[] of size N consisting of unique elements in the range [0, N-1], the task is to find K which is the number of steps required to sort the given array by selecting three distinct elements and rearranging them. And also, print the indices selected in those K steps in K lines. For exa
15+ min read
Find the Kth pair in ordered list of all possible sorted pairs of the Array Given an array arr[] containing N integers and a number K, the task is to find the K-th pair in the ordered list of all possible N2 sorted pairs of the array arr[]. A pair (p1, q1) is lexicographically smaller than the pair (p2, q2) only if p1 ? p2 and q1 < q2. Examples: Input: arr[] = {2, 1}, K
6 min read
Multi-set for user defined data type You are given Q queries. Each query contains an integer k and a person's information i.e, first name, last name, age. For each query, we need to output Kth person among them if all person information are arrange in ascending order. Note: Person A comes before person B if first name of A is lexicogra
3 min read
C++ Program for Sorting all array elements except one Given an array, a positive integer, sort the array in ascending order such that the element at index K in the unsorted array stays unmoved and all other elements are sorted. Examples: Input : arr[] = {10, 4, 11, 7, 6, 20} k = 2; Output : arr[] = {4, 6, 11, 7, 10, 20} Input : arr[] = {30, 20, 10} k =
3 min read
Sort first half in ascending and second half in descending order | 1 Given an array of integers, sort the first half of the array in ascending order and second half in descending order. Examples: Input : arr[] = {5, 2, 4, 7, 9, 3, 1, 6, 8} Output : arr[] = {1, 2, 3, 4, 9, 8, 7, 6, 5} Input : arr[] = {1, 2, 3, 4, 5, 6} Output : arr[] = {1, 2, 3, 6, 5, 4}Recommended Pr
10 min read
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
5 min read
Stable sort for descending order Given an array of n integers, we have to reverse sort the array elements such the equal keys are stable after sorting. Examples: Input : arr[] = {4, 2, 3, 2, 4} Output : 4, 4, 3, 2, 2 Prerequisite : Stability in sorting algorithmsMethod 1 (Writing our own sorting function : Bubble Sort)We know sorti
8 min read
Find the value of K after replacing every index of the array by |ai - K| Given an array, arr[] containing n integers, the task is to find an integer (say K) such that after replacing each and every index of the array by |ai - K| where ( i ? [1, n]), results in a sorted array. If no such integer exists that satisfies the above condition then return -1. Examples: Input: ar
10 min read
Rearrange an array in order - smallest, largest, 2nd smallest, 2nd largest, .. Given an array of integers, the task is to print the array in the order - smallest number, the Largest number, 2nd smallest number, 2nd largest number, 3rd smallest number, 3rd largest number, and so on..... Examples: Input : arr[] = [5, 8, 1, 4, 2, 9, 3, 7, 6] Output :arr[] = {1, 9, 2, 8, 3, 7, 4,
7 min read
Permutation of first N elements with absolute adjacent difference in increasing order Given a positive integer N, the task is to construct a permutation from 1 to N such that the absolute difference of elements is in strictly increasing order. Note: N cannot be 0 or 1. Examples: Input: N = 10Output: 6 5 7 4 8 3 9 2 10 1Explanation: abs(6 - 5) i.e., 1 < abs(5 - 7) i.e., 2 < abs(
5 min read