Problem · Array
Compressing Array
Learn this problemProblem statement
Given an array of integers, a, in one operation one can select any two adjacent elements and replace them with their product. This operation can only be applied if the product of those adjacent elements is less than or equal to k.
The goal is to reduce the length of the array as much as possible by performing any number of operations. Return that minimum size.
Function
getMinLength(a: int[], k: int) → int
Complete the function getMinLength in the editor.
getMinLength has the following parameters:
int a[n]: an array of integersint k: the constraint of the operation
Returns
int: the minimum length of the array after performing any number of operations
Examples
Example 1
a = [2, 3, 3, 7, 3, 5]k = 20return = 3This is the list of operations that will give us the smallest array (1-based indexing):>
Merge the elements at indices (1, 2), resulting array will be - [6, 3, 7, 3, 5]
Merge the elements at indices (1, 2), resulting array will be - [18, 7, 3, 5]
Merge the elements at indices (3, 4), resulting array will be - [18, 7, 15]
Hence, the answer is 3.
Constraints
:>/code>