MANO M
MANO M

Reputation: 1

Concating the list of values in which the odd numbers are placed after the even numbers

Its like online numbers game. print the odd numbers after the even numbers.

i try to give the codig according to the expected output, while running the code few of the test case ae failed.

You are playing an online game. In the game, a list of N numbers is given. The player has to arrange the numbers so that all the odd numbers on the list come after the even numbers. Write an algorithm to arrange the given list such that all the odd numbers of the list come after the even numbers.

Input Format

The first line of the input consists of an integer num, representing the size of the list(N). The second line of the input consists of N space-separated integers representing the values of the list.

Constraints

NA

Output Format

Print N space-separated integers such that all the odd numbers of the list comes after the even numbers

Sample Input 0

8
10 98 3 33 12 22 21 11

Sample Output 0

Array after Segregation
10 98 22 12 33 3 21 11

Sample Input 1

5
73 4 63 23 65

Sample Output 1

Array after Segregation
4 73 63 23 65

Its my coding:

n = int(input())
lst = list(map(int, input().split()))

even = []
odd = []

for i in lst:
    if i % 2 == 0:
        even.append(i)
    else:
        odd.append(i)
if len(even)<=1:
    result = even + odd
elif len(odd)<=1:
    even[-1], even[-2] = even[-2], even[-1]
    result = even + odd
    
else:
    even[-1], even[-2] = even[-2], even[-1]
    odd[0], odd[1] = odd[1], odd[0]
    result = even + odd


print("Array after Segregation")
print(*result)

Upvotes: 0

Views: 1258

Answers (1)

Tom Karzes
Tom Karzes

Reputation: 24052

The main problem here is that the problem is incompletely specified. The even numbers need to come before the odd numbers, sure, but the order of the even numbers is unspecified, and the order of the odd numbers is unspecified.

The most obvious interpretation is that the relative order of the even numbers should be preserved, and the relative order of the odd numbers should be preserved. That's what you'd get if you just made a single pass through the numbers, storing the even numbers in one list and the odd numbers in another list, then concatenated the resulting lists. And indeed, that's what the expected output from this statement of the problem shows.

However, this differs from the expected output in the question posted here. More explanation is clearly needed, but in spite of that, I believe I understand how the expected results are being generated. They appear to be the result of a specific algorithm that is used, which is this: You have two indices, one that starts at the left and one that starts at the right. Keep advancing the left index until the corresponding value is odd, and keep moving the right index down until the value it indexes is even. Then swap the two values and continue. The process ends then the two indices pass each other.

This algorithm allows the even vs. odd values to be efficiently separated in-place without requiring additional storage (and without having to constantly move large blocks of values to make room).

The code looks like this:

n = int(input())
lst = list(map(int, input().split()))

lo = 0
hi = len(lst) - 1

while True:
    while lo < hi and lst[lo] % 2 == 0:
        lo += 1

    while lo < hi and lst[hi] % 2 == 1:
        hi -= 1

    if lo >= hi:
        break

    lst[lo], lst[hi] = lst[hi], lst[lo]
    lo += 1
    hi -= 1

print("Array after Segregation")
print(*lst)

If you run this on the example input data, the results match the posted expected output.

8
10 98 3 33 12 22 21 11
Array after Segregation
10 98 22 12 33 3 21 11

and:

5
73 4 63 23 65
Array after Segregation
4 73 63 23 65

As I said, the problem is incompletely specified, and this is not what I would expect in the absence of further clarification, but it does seem to be how they obtained their expected output:

Upvotes: 0

Related Questions