Reverse First K Elements
EasyPractice library · +10 XPqueuestacksimulation
Given a sequence of N integers and an integer K, reverse the order of the first K elements in the sequence while the remaining N-K elements stay in their original relative order. The entire sequence should then be printed.
Input
The first line contains two integers N and K, where N is the number of elements in the sequence and K is the number of elements to reverse from the beginning. The second line contains N space-separated integers representing the sequence.
Output
Print the N space-separated integers of the modified sequence.
Constraints
1 <= K <= N <= 1000 -1000 <= element <= 1000
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 3 1 2 3 4 5
Output
3 2 1 4 5
The first 3 elements (1, 2, 3) are reversed to (3, 2, 1). The sequence becomes 3 2 1 4 5.
Example 2
Input
7 5 10 20 30 40 50 60 70
Output
50 40 30 20 10 60 70
The first 5 elements (10 to 50) are reversed. The sequence becomes 50 40 30 20 10 60 70.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
5 3 1 2 3 4 5
Example 2 input
7 5 10 20 30 40 50 60 70