Sort-Odd&Even - python - Spicy Coders

Recent

Monday, July 10, 2017

Sort-Odd&Even - python

An array of N numbers is passed as the input. The program must sort the odd numbers and even numbers separately in ascending order. The odd and even numbers must retain their original odd even slots in the input.

Input Format:

The first line contains N indicating the count of numbers in the array. The second line contains the N array elements separated by a space.

Output Format:

The first line contains the N sorted array elements separated by a space.

Boundary Conditions: 2 <= N <= 100

Example Input/Output 1:
Input:
9
169 181 298 16 147 263 102 155 141

Output:
141 147 16 102 155 169 298 181 263

Coding:
n = int(input())
a = [int(x) for x in input().split()]
o = []
e = []
j,k = 0,0
for i in range(n):
    if a[i]%2==0:
        e.append(a[i])
    else:
        o.append(a[i])
e.sort()
o.sort()
for i in range(n):
    if a[i]%2==0:
        print(e[j],'',end='')
        j+=1
    else:
        print(o[k],'',end='')
        k+=1s

No comments:

Post a Comment