Introduction

Hello there! In this unit, we're offering an engaging coding lesson that highlights the performance efficiencies offered by the utilization of std::unordered_map in C++. We'll address a vector-based problem that requires us to make an optimal choice to minimize the size of our vector. Excited? So am I! Let's get started.

Task Statement

In this unit's task, we'll manipulate a vector of integers. You are required to construct a C++ function titled minimal_max_block(). This function should accept a vector as an input and compute an intriguing property related to contiguous blocks within the vector.

More specifically, you must select a particular integer, k, from the vector. Once you've selected k, the function should remove all occurrences of k from the vector, thereby splitting it into several contiguous blocks or remaining sub-vectors. A unique feature of k is that it is chosen such that the maximum length among these blocks is minimized.

For instance, consider the vector {1, 2, 2, 3, 1, 4, 4, 4, 1, 2, 5}. If we eliminate all instances of 2 (our k), the remaining blocks would be {1}, {3, 1, 4, 4, 4, 1}, , with the longest containing 6 elements. Now, if we instead remove all instances of 1, the new remaining blocks would be , , , the longest of which contains 3 elements. As such, the function should return 1 in this case, as it leads to a minimally maximal block length.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal