Cap Repeated Values in a Sorted Array

Quick Overview

Limit each value in a sorted array to the required number of occurrences while retaining order, and consider the constant-extra-space in-place follow-up.

Cap Repeated Values in a Sorted Array

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Cap Repeated Values in a Sorted Array You are given an integer array `nums`, sorted in nondecreasing order, and an integer `k`. Return the array obtained by limiting each distinct value to at most `k` occurrences while preserving order. If a value occurs at least `k` times, retain exactly `k` copies. If it occurs fewer than `k` times, retain all its copies. ### Function `vector<int> limitOccurrences(vector<int>& nums, int k)` ### Example 1 **Input:** `nums = [2, 2, 2, 2, 5, 5, 8], k = 3` **Output:** `[2, 2, 2, 5, 5, 8]` **Explanation:** Retain three of the four copies of `2`, both copies of `5`, and the single `8`. Their order is unchanged. ### Example 2 **Input:** `nums = [4, 4, 6, 6, 6, 9, 9], k = 1` **Output:** `[4, 6, 9]` **Explanation:** Each distinct value is retained once. ### Constraints - `1 <= nums.length <= 100` - `1 <= nums[i] <= 100` - `nums` is sorted in nondecreasing order. - `1 <= k <= nums.length` ### Clarifications - Return the resulting values, not just their count. - Values already appearing at most `k` times are retained without change. - The array is already sorted; no rearrangement is needed or allowed in the result. ### What a Strong Answer Covers - Retaining the required number of occurrences from every value, including the first and last runs. - Preserving order and handling `k = 1` as well as limits that remove nothing. - Returning the complete resulting array through the specified interface. ### Follow-up Questions - Can you do the filtering in place with `O(1)` extra space? Space used for returning or resizing the result does not count toward that extra-space requirement.

Overview: Limit each value in a sorted array to the required number of occurrences while retaining order, and consider the constant-extra-space in-place follow-up.

Meta logo
Meta
easySoftware EngineerOtherCoding & Algorithms
0
0

Cap Repeated Values in a Sorted Array

You are given an integer array nums, sorted in nondecreasing order, and an integer k.

Return the array obtained by limiting each distinct value to at most k occurrences while preserving order. If a value occurs at least k times, retain exactly k copies. If it occurs fewer than k times, retain all its copies.

Function

vector<int> limitOccurrences(vector<int>& nums, int k)

Example 1

Input: nums = [2, 2, 2, 2, 5, 5, 8], k = 3

Output: [2, 2, 2, 5, 5, 8]

Explanation: Retain three of the four copies of 2, both copies of 5, and the single 8. Their order is unchanged.

Example 2

Input: nums = [4, 4, 6, 6, 6, 9, 9], k = 1

Output: [4, 6, 9]

Explanation: Each distinct value is retained once.

Constraints

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 100
  • nums is sorted in nondecreasing order.
  • 1 <= k <= nums.length

Clarifications

  • Return the resulting values, not just their count.
  • Values already appearing at most k times are retained without change.
  • The array is already sorted; no rearrangement is needed or allowed in the result.

What a Strong Answer Covers Guidance

  • Retaining the required number of occurrences from every value, including the first and last runs.
  • Preserving order and handling k = 1 as well as limits that remove nothing.
  • Returning the complete resulting array through the specified interface.

Follow-up Questions Guidance

  • Can you do the filtering in place with O(1) extra space? Space used for returning or resizing the result does not count toward that extra-space requirement.