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 decending order.
  3. Merge the two arrays by adding the elements from second array in reverse order.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 array from 0 to k-1 places
        Arrays.sort(arr1, 0, k);
  
        // sorting the array from k to n places
        Arrays.sort(arr2, k, 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);
    }
}

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

2 4 5 6 9 8 3 1 -1

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right


Output:

2 4 5 6 9 8 3 1 -1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.