Problem · Array

Compressing Array

Learn this problem
MediumWells FargoFULLTIMEOA

Problem 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:

  1. int a[n]: an array of integers
  2. int 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 = 3
This 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>

    More Wells Fargo problems

    drafts saved locally
    public int getMinLength(int[] a, int k) {
      // write your code here
    }
    
    a[2, 3, 3, 7, 3, 5]
    k20
    expected3
    checking account