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++ 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 order void 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 Code int 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 program to implement // the above approach import java.util.*;
class GFG{
// Function to sort first K array elements in // descending and last N - K in ascending order static 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 Code public 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 program to implement # the above approach # Function to sort first K array # elements in descending and last # N - K in ascending order def 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 Code if __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# 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 order static 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 code public 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 |
<script> // Javascript program to implement // the above approach // Function to sort first K array elements in // descending and last N - K in ascending order function 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 Code var 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)