Skip to content

4133. Longest Mirror Subsequence

DifficultyHard

Description

You are given an integer array nums.

A non-empty subsequence is called a mirror subsequence if the sum of its first half is equal to the sum of its second half.

For subsequences of odd length, the middle element is ignored when comparing the two halves. In particular, a subsequence of length 1 is always a mirror subsequence.

Return an integer denoting the length of the longest mirror subsequence in nums.

 

Example 1:

Input: nums = [4,-5,9,2,3,-4]

Output: 5

Explanation:

  • One longest mirror subsequence is [4, -5, 9, 3, -4].
  • Its first half is [4, -5], with sum 4 + (-5) = -1.
  • The middle element 9 is ignored.
  • Its second half is [3, -4], with sum 3 + (-4) = -1.
  • Thus, this subsequence is a mirror subsequence of length 5.

Example 2:

Input: nums = [1,2,1,2]

Output: 4

Explanation:

  • The entire array is a mirror subsequence.
  • Its first half is [1, 2], with sum 1 + 2 = 3.
  • Its second half is [1, 2], with sum 1 + 2 = 3.
  • Since both sums are equal, the answer is 4.

Example 3:

Input: nums = [1,2,3]

Output: 1

Explanation:

There is no mirror subsequence of length 2 or 3, so the answer is 1.

 

Constraints:

  • 1 <= nums.length <= 50
  • -20 <= nums[i] <= 20

Solutions

Solution 1

1

1

1

1

Comments