Combine Maximum

You are given an array A of integers, of size N.

You also have an operation:

Select any two adjacent indices, say, i and i + 1.
If A[i] == A[i+1], then you can remove A[i] and A[i+1], and put a single number in their place with value A[i] + 1.

You want to maximize the maximum number that is left in the array after applying the operation 0 or more times.

Find and return this maximum number.


Input Format:

The first and the only argument of input contains an integer array, of size N.

Output Format:

Return an integer representing the answer.

Constraints:

1 <= N <= 250
1 <= A[i] <= 50

Examples:

Input 1:
    A = [1, 1, 1, 1, 1]

Output 1:
    3

Explanation 1:
    We combine the first and second elements to get : [2, 1, 1, 1].
    We combine the second and third elements to get : [2, 2, 1].
    We combine the first and second elements to get : [3, 1].
    
    You can check that this is the maximum value we can get.

Input 2:
    A = [3, 3, 4, 4, 4]

Output 2:
    6

Explanation 2:
    We combine the second and third elements to get : [4, 4, 4, 4]
    We combine the first and second elements to get : [5, 4, 4]
    We combine the second and third elements to get : [5, 5]
    We combine the first and second elements to get : [6]
    
    You can check that this is the maximum value we can get.

Input 3:
    A = [1, 2, 3, 4, 5]
   
Output 3:
    5

Explanation 3:
    We cannot combine any number here, so the answer will be 5.
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Combine Maximum on Interview Code Editor
Hints
  • Hints are not available for this problem

Discussion


Loading...
Click here to start solving coding interview questions