We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Balanced Trees
  4. Array and simple queries

Array and simple queries

Problem
Submissions
Leaderboard
Discussions
Editorial

Given two numbers and . indicates the number of elements in the array and indicates number of queries. You need to perform two types of queries on the array .

You are given queries. Queries can be of two types, type 1 and type 2.

  • Type 1 queries are represented as 1 i j : Modify the given array by removing elements from to and adding them to the front.

  • Type 2 queries are represented as 2 i j : Modify the given array by removing elements from to and adding them to the back.

Your task is to simply print of the resulting array after the execution of queries followed by the resulting array.

Note While adding at back or front the order of elements is preserved.

Input Format

First line consists of two space-separated integers, and .
Second line contains integers, which represent the elements of the array.
queries follow. Each line contains a query of either type 1 or type 2 in the form

Constraints


Output Format

Print the absolute value i.e. in the first line.
Print elements of the resulting array in the second line. Each element should be seperated by a single space.

Sample Input

8 4
1 2 3 4 5 6 7 8
1 2 4
2 3 5
1 4 7
2 1 4

Sample Output

1
2 3 6 5 7 8 4 1

Explanation

Given array is .
After execution of query , the array becomes .
After execution of query , the array becomes .
After execution of query , the array becomes .
After execution of query , the array becomes .
Now is i.e. and the array is

Author

bokamvinaykumar

Difficulty

Hard

Max Score

80

Submitted By

8454

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature