Sort in specific order (GFG)

 Given an array A of positive integers. Your task is to sort them in such a way that the first part of the array contains odd numbers sorted in descending order, rest portion contains even numbers sorted in ascending order.

Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer N denoting the size of the array. The next line contains N space separated values of the array.

Output:
For each test case in a new line print the space separated values of the modified array.

Constraints:
1 <= T <= 103
1 <= N <= 107
0 <= A<= 1018

Example:
Input:

2
7
1 2 3 5 4 7 10
7
0 4 5 3 7 2 1

Output:
7 5 3 1 2 4 10
7 5 3 1 0 2 4

Explanation:
Testcase1: Array elements 7 5 3 1 are odd and sorted in descending order, whereas 2 4 10 are even numbers and sorted in ascending order.


SOLUTION:

t=int(input())

for i in range (t):

    n=int(input())

    p=list(map(int,input().split()))

    odd=[]

    even=[]

    for i in range (n):

        if p[i]%2==0:

            even.append(p[i])

        else:

            odd.append(p[i])

    odd.sort(reverse = True)

    even.sort()

    oddeven = odd +even

    for i in range(n):

        print(oddeven[i], end=" ")

    print()    

        

    

Comments

Popular posts from this blog

Short Notes Of computer Network

Short Note of Data Structure