Permutations
Practice
3 (29 votes)
Basic programming
Data structures
Prefix
Arrays
Basics of greedy algorithms
1 D
Problem
33% Success 8961 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

You are given a permutation with length $$n$$. You want to play a game with you friend, Bob, and the rule will be as follows: 

  • You will choose a subarray $$[l,r]$$ from the permutation. Then, ask Bob to find the maximum element in the rest of the numbers.

You are given a permutation $$a$$ contains all numbers $$1$$ to $$n$$. And, in $$q$$ queries, each query has two integers $$l$$ and $$r$$.

For each query, you have to help Bob find the maximum value that does not exist in the subarray $$[l,r]$$.

Note: A permutation is a sequence of integers from $$1$$ to $$n$$ of length $$n$$ containing each number exactly once. 
For example, (1), (4, 3, 5, 1, 2), (3, 2, 1) are permutations and (1, 1), (4, 3, 1), (2, 3, 4) are not.

Input format

  • The first line contains two integers $$n$$ and $$q$$ denoting the number of elements and the number of test cases. 
  • The second line contains $$n$$ space-separated integers.
  • The next $$q$$ lines and each line contains two integers $$l$$, $$r$$.

Output format

Print the maximum number that does not exist in the subarray $$[l, r]$$.

Constraints

$$2 \le   n,q   \le  10^5$$

$$1 \le  a_i  \le n$$

$$1 \le l,r \le  n$$

$$1 \le l-r+1 < n$$

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
32 votes
Tags:
Data StructuresEasyOne-dimensional
Points:30
44 votes
Tags:
ArraysData StructuresPrefix1-DHashing
Points:30
24 votes
Tags:
1-D ArrayArraysData Structures