Logo
IconChallenge
Icon
IconProblem
IconSolutions
IconSubmissions

Maximum Frequency After Subarray Operation

XPChallenge Points: 20
levelLevel: Medium

You are given an integer array nums and an integer k.
You are allowed to perform exactly one operation:

  1. Choose a subarray nums[i..j].

  2. Choose an integer x.

  3. Add x to each element in the selected subarray.

After performing the operation, some elements in the array might become equal to k.

Your task is to maximize the number of elements equal to k after this operation.

Return the maximum possible frequency of k after the operation.

Example 1:

Input: nums = [5, 5, 5], k = 5

Output: 3

Explanation:

No operation needed. k already appears 3 times.

Example 2:

Input: nums = [2, 4, 6, 8], k = 6

Output: 2

Explanation:

Add 4 to nums[0..1] → [6, 8, 6, 8] Now 6 appears twice.

to Continue
like
dislike

Accepted:

Submission:

IconReport an issue
Icon
IconCode
IconYou need toto run or submitYou need toto run or submit
IconTest Case
IconTest Result