Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Merge Adjacent Equal Numbers

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Merge Adjacent Equal Numbers. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Given an array, you can merge two adjacent equal numbers into one; return the smallest final array after applying operations optimally.

A merge replaces equal adjacent values x, x with 2 * x. Choose the operations that produce the shortest final array; if several results have the same length, return the lexicographically smallest one. For signed values, a value created from a negative pair is not merged again.

Implement merge_equal_adjacent(nums), which accepts a list of integers and returns the resulting list.

Constraints:

  • 1 <= len(nums) <= 200
  • -10^9 <= nums[i] <= 10^9

Constraints

  • 1 <= len(nums) <= 200
  • -10^9 <= nums[i] <= 10^9
  • A merge replaces x, x with 2 * x
  • A value created from a negative pair is not merged again
  • Among shortest results, choose the lexicographically smallest array

Function Signature

def merge_equal_adjacent(nums):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output