Dec 26, 2020 in Python 3,390 views

## 1 answer to this question.

Hi, @There,

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

 ```# 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 =  * k arr2 =  * (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```
```Output:

2 4 5 6 9 8 3 1 -1
```

• 65,930 points

+1 vote

## I am trying to write a program in python for the occurrences of a character given the start and stop values of that string only using while

Here's the code: check = input("Enter the character: ...READ MORE

## Print a list in both ascending and descending order

You can use listName.sort(reverse=True). This will sort the ...READ MORE

## Write a for loop that prints all elements of a list and their position in the list. a = [4,7,3,2,5,9]

Try using this question by list comprehension: a=[4,7,3,2,5,9] print([x for ...READ MORE

## Write code to create a list of word lengths for the words in original_str using the accumulation pattern and assign the answer to a variable num_words_list.

Hi,  num_words_list = len(original_str.split()) original_str.split() - split words in ...READ MORE

## Write a Python program that takes as input a month and year, determines how many days are in the month, and outputs the number of days. Thirty days are in September, April, June, and November

Write a Python program that takes as ...READ MORE

## I am using a built-in function name as a method identifier and sometimes as an attribute in Python, is this bad?

Hi, good question. The thing is that ...READ MORE

–1 vote