Print Last K Elements
EasyPractice library · +10 XPheapspriority queues
Given a sequence of N integers and an integer K, print the last K integers that appeared in the sequence, in the order they appeared. If N < K, print all N integers in order.
Input
The first line contains two integers N and K, separated by a space. The second line contains N integers, the sequence.
Output
Print the last K integers from the sequence, separated by spaces, in the order they appeared. If N < K, print all N integers in order.
Constraints
1 <= N <= 1000; 1 <= K <= N; The integers in the sequence are between -10^9 and 10^9.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
5 3 10 20 30 40 50
Output
30 40 50
The last 3 elements are 30, 40, 50.
Example 2
Input
4 5 1 2 3 4
Output
1 2 3 4
N < K, so all elements are printed in order.
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 10 20 30 40 50
Example 2 input
4 5 1 2 3 4