SwapFirstNLastHalf

Write a program to print the first half of the list at last and last half of the list at first.
Input format:
First-line contains the size of the array.
The second line contains elements of array separated by space.
Output Format:
Modified Array
Source Code
n=int(input())
l=list(map(int, input().split()))
if n%2==0:
    for i in range(n//2):
        l.insert(0,l.pop())
else:
    for i in range(n//2):
        l.insert(0,l.pop())
    l.insert(i+1,l.pop())
print(*l)
Sample Input1:
5
1 2 3 4 5
Sample Output1:
4 5 3 1 2
Sample Input2:
6
1 4 6 2 3 5
Sample Output2:
2 3 5 1 4 6

No comments:

Post a Comment

Total Pageviews