Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

pranavg9927's blog

By pranavg9927, history, 14 months ago, In English

Problem Description You are given an integer array arr of length n. A subarray arr[i] is considered good if it contains at least one integer that appears exactly once within the subarray. You can select any element in arr[] and replace it with any integer. Find the minimum number of elements needed to be replaced so that every subarray of the array becomes good.

Examples

Example 1: Input: n = 4, arr = [1, 2, 1, 2] Output: 1 Explanation: Replace 0th element (0-based indexing) with 3 to get [3, 2, 1, 2]. Now, every subarray contains at least one element that appears exactly once within it. Thus, the answer is 1.

Example 2: Input: n = 4, arr = [4, 4, 4, 4] Output: 2 Explanation: Replace 0th and 2nd element (0-based indexing) with 3 and 5 respectively to get [3, 4, 5, 4]. Now, every subarray contains at least one element that appears exactly once within it. Thus, the answer is 2.

Example 3: Input: n = 5, arr = [1, 3, 2, 1, 2] Output: 0 Explanation: Every subarray already contains at least one element that appears exactly once in it. Thus, the answer is 0.

Constraints

  • 1 <= n <= 2 * 10^5

  • 1 <= arr[i] <= n

Full text and comments »

  • Vote: I like it
  • +2
  • Vote: I do not like it