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
Sort Vector of Pairs in Ascending Order in C++ Sorting a vector of pairs in ascending order means arranging the elements in such a way that first pair is lesser than second pair, second pair is lesser than third pair and so on.The easiest way to sort the vector of pairs in ascending order is to use std::sort() function. The below code example il
4 min read
C++ Program to Sort the Elements of an Array in Ascending Order Here, we will see how to sort the elements of an array in ascending order using a C++ program. Below are the examples: Input: 3 4 5 8 1 10Output: 1 3 4 5 8 10 Input: 11 34 6 20 40 3Output: 3 6 11 20 34 40 There are 2 ways to sort an array in ascending order in C++: Brute-force Approach Using Bubble
4 min read
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
C++ Program to Find the K'th largest element in a stream Given an infinite stream of integers, find the k'th largest element at any point of time.Example: Input:stream[] = {10, 20, 11, 70, 50, 40, 100, 5, ...}k = 3Output: {_, _, 10, 11, 20, 40, 50, 50, ...} Extra space allowed is O(k). Recommended: Please solve it on "PRACTICE" first, before moving on to
7 min read
Sort an array according to absolute difference with given value using Functors Given an array of n distinct elements and a number x, arrange array elements according to the absolute difference with x, i. e., the element having a minimum difference comes first and so on. Note: If two or more elements are at equal distance arrange them in same sequence as in the given array. Exa
6 min read
Sort an array where a subarray of a sorted array is in reverse order Given an array of N numbers where a subarray is sorted in descending order and rest of the numbers in the array are in ascending order. The task is to sort an array where a subarray of a sorted array is in reversed order. Examples: Input: 2 5 65 55 50 70 90 Output: 2 5 50 55 65 70 90 The subarray fr
9 min read
Implementing Counting Sort using map in C++ Counting Sort is one of the best sorting algorithms which can sort in O(n) time complexity but the disadvantage with the counting sort is it's space complexity, for a small collection of values, it will also require a huge amount of unused space. So, we need two things to overcome this: A data struc
2 min read
K-th Smallest Element in an Unsorted Array using Priority Queue Given an array arr[] consisting of N integers and an integer K, the task is to find the Kth smallest element in the array using Priority Queue. Examples: Input: arr[] = {5, 20, 10, 7, 1}, N = 5, K = 2Output: 5Explanation: In the given array, the 2nd smallest element is 5. Therefore, the required out
5 min read
C++ Program To Merge K Sorted Linked Lists Using Min Heap - Set 2 Given k linked lists each of size n and each list is sorted in non-decreasing order, merge them into a single sorted (non-decreasing order) linked list and print the sorted linked list as output.Examples:Â Input: k = 3, n = 4 list1 = 1->3->5->7->NULL list2 = 2->4->6->8->NULL list3 = 0->9->10->11->NU
5 min read