PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#BS0000176. Số nguyên dương bị thiếu thứ K (Kth Missing Positive Number)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số nguyên dương bị thiếu thứ K (Kth Missing Positive Number)

Kth Missing Positive Number

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

You are given nn distinct positive integers in strictly increasing order. A positive integer is missing if it does not occur in the array.

Consider all missing positive integers in increasing order. Find the value of the kk-th missing positive integer, using one-based counting.

For example, if the array is 2,4,72,4,7, the missing integers begin with 1,3,5,6,8,…1,3,5,6,8,\ldots.

Input

  • The first line contains integers nn and kk: the array size and the missing-number rank.
  • The second line contains exactly nn integers a1,a2,…,ana_1,a_2,\ldots,a_n in strictly increasing order.

Output

Print one integer: the value of the kk-th missing positive integer.

Subtasks

  • Subtask 1 (20 points): 1≤n≤201\le n\le 20; 1≤k≤201\le k\le 20; 1≤a1<a2<⋯<an≤10001\le a_1<a_2<\cdots<a_n\le 1000.

  • Subtask 2 (30 points): 1≤n≤2001\le n\le 200; 1≤k≤2001\le k\le 200; 1≤a1<a2<⋯<an≤10001\le a_1<a_2<\cdots<a_n\le 1000.

  • Subtask 3 (50 points): 1≤n≤10001\le n\le 1000; 1≤k≤10001\le k\le 1000; 1≤a1<a2<⋯<an≤10001\le a_1<a_2<\cdots<a_n\le 1000.

Examples

Example 1

Input

5 5
2 3 4 7 11

Output

9

Explanation

The array contains 2,3,4,7,112,3,4,7,11. Its missing positive integers are 1,5,6,8,9,10,…1,5,6,8,9,10,\ldots. In order, the first five are 11, 55, 66, 88, and 99. Thus, for k=5k=5, the output is 9.

Example 2

Input

4 2
1 2 3 4

Output

6

Explanation

The first four positive integers are present, so the missing values start at 5,6,7,…5,6,7,\ldots. The second missing value is 6.