InferenceAI

Enter at least two characters to search.

Start free

Values Above a Third

mediumArrays & Strings25 min25 XP

Return, in ascending order, every value that appears more than ⌊n/3⌋\lfloor n / 3 \rfloor times in nums.

Requirements

  • At most two values can qualify. Try the O(1)O(1)-space Boyer–Moore voting idea with two candidates.

Examples

EXAMPLE WALKTHROUGHone value

A guided view of the published example, not a trace of your code.

INPUT[[3,2,3]]
TASKfrequent_thirds()
EXPECTED OUTPUT[3]

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [3,2,3]

Output: [3]

Explanation: one value

Input: nums = [1]

Output: [1]

Explanation: single

Input: nums = [1,2]

Output: [1,2]

Explanation: both qualify

Hint 1

Keep two candidates with counters; on a third distinct value, decrement both.

Hint 2

Verify the candidates with a second pass.

Topics

DSAArrays & StringsPython