**Question:** You are given a list of integers of size N. Write an algorithm to sort the first K elements (from list[0] to list[k-1] of the list in ascending order and the remaining (list[k] to list[k-1]) elements in decending order.

**Input:**

The first line of the input consists of an integer – * inputlist_size*, representing the number of elements in the list (N). The next line consists of N space separated integers representing the elements of the list. The last line consists of an integer num, representing the number of elements to be sorted in ascending order (K).

**Output:**

Print N space-separated integers representing the sorted list.

**Constraints:**

num<=inputlist_size

Examples:

Input:

8

11,7,5,10,46,23,16,8

3

**Output:**

5,7,11,46,23,16,10,8

**Algorithm to Solve **

- Take the input from user like number of array Elements, array elements and K value (upto which we want in ascending order).
- Later use two for loops for iterating through elements of array.
- inside second for loop use two condidtions
- If the outer loop value is <= K , then write logic to sort in ascending order
- If outer loop value is >k, then write logic to sort in decending order.

- Later return the sorted array

**JAVA Code **

package arrayPrograms; public class vrashi { public static void main(String[] args) { // coded by VRASHIKESH PATIL int N=8; int[] array1= {11,7,5,10,46,23,16,8}; int num=3; for(int i=0;i<array1.length;i++) { if(i<num) { for(int j=i+1;j<num;j++) { if(array1[i]>array1[j]) { int temp=array1[j]; array1[j]=array1[i]; array1[i]=temp; } } } else if(i>num){ for(int j=num;j<array1.length;j++) { if(array1[i]>array1[j]) { int temp=array1[j]; array1[j]=array1[i]; array1[i]=temp; } } } } for(int i:array1) { System.out.print(i+" "); } } }

**Watch Complete Explanation for Above Code **

**Similar off Campus Coding Question **

**Dynamic Programming HACKER EARTH questions**

**HACKER EARTH off Campus Coding Question**

**Rotate Array Elements towards Right Sidea**

Pingback: Finding all substrings of given string in JAVA - Is It Actually