Skip to main content

Maximum Frequency After Subarray Operation - Solution & Explanation

MediumArrayHash TableDynamic ProgrammingGreedy3 min readAsked at: Amazon, Microsoft, Google +1
Practice this problem

Problem Statement

You are given an array nums of length n. You are also given an integer k.

You perform the following operation on nums once:

  • Select a subarray nums[i..j] where 0 <= i <= j <= n - 1.
  • Select an integer x and add x to all the elements in nums[i..j].

Find the maximum frequency of the value k after the operation.

 

Example 1:

Input: nums = [1,2,3,4,5,6], k = 1

Output: 2

Explanation:

After adding -5 to nums[2..5], 1 has a frequency of 2 in [1, 2, -2, -1, 0, 1].

Example 2:

Input: nums = [10,2,3,4,5,5,4,3,2,2], k = 10

Output: 4

Explanation:

After adding 8 to nums[1..9], 10 has a frequency of 4 in [10, 10, 11, 12, 13, 13, 12, 11, 10, 10].

 

Constraints:

  • 1 <= n == nums.length <= 105
  • 1 <= nums[i] <= 50
  • 1 <= k <= 50

Approach Overview

Problem Overview: You are given an array and allowed to apply a single operation on a subarray. The operation shifts every element in that subarray by the same value. After the operation, the goal is to maximize the frequency of any number in the array.

Approach 1: Brute Force Enumeration (O(n^3) time, O(1) space)

Start by enumerating every possible subarray [l, r]. For each subarray, try all possible shifts that would convert one element in that subarray into a chosen target value. Apply the transformation conceptually and recompute the frequency of all numbers. This involves iterating over the subarray, modifying values, and counting frequencies with a map. The method works for small inputs but becomes impractical because there are O(n^2) subarrays and each evaluation requires another scan.

Approach 2: Target Enumeration with Prefix Counting (O(n^2) time, O(n) space)

Instead of recomputing the whole array each time, fix a target value t. For each index, determine the shift needed to convert nums[i] into t. When the same shift is applied to a subarray, elements that share the same difference align with the target simultaneously. You can track contributions using prefix frequency maps or counters while scanning the array. This reduces redundant work and makes it easier to count how many elements can be converted together.

Approach 3: Greedy Gain Tracking with Kadane's Algorithm (O(n) time, O(n) space)

The optimal strategy treats the operation as a gain/loss problem. Suppose you want the final value to be t. Elements already equal to t contribute to the base frequency. If you apply a shift on a subarray, some elements become t (gain) while existing t values inside the subarray might change (loss). Convert this into a gain array where converting a value gives +1 and breaking an existing match gives -1. The best subarray to operate on is the maximum subarray sum, which can be computed using Kadane's algorithm. Combining the base frequency with the maximum gain yields the final answer.

This approach relies on ideas from array processing, prefix sum style counting, and greedy dynamic programming similar to Kadane’s algorithm. A hash table helps track frequencies efficiently.

Recommended for interviews: The Kadane-style gain tracking approach. Interviewers expect you to recognize that applying the operation on a subarray is equivalent to selecting a segment with maximum net gain. Explaining the brute force first shows understanding of the search space, but converting the problem into a maximum subarray problem demonstrates strong algorithmic intuition.

Solutions for this problem are being prepared.

Try solving it yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Subarray EnumerationO(n^3)O(1)Only for conceptual understanding or very small arrays
Target Enumeration with Prefix CountingO(n^2)O(n)When optimizing brute force and analyzing conversions per target value
Greedy Gain + Kadane’s AlgorithmO(n)O(n)Best general solution; optimal for interviews and large inputs

Video Solution

3434. Maximum Frequency After Subarray Operation | Modified Kadane's AlgorithmAryan Mittal9,111 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Maximum Frequency After Subarray Operation easy or hard?
Maximum Frequency After Subarray Operation is generally classified as a Medium problem. The challenge lies in recognizing that the operation can be modeled as a maximum subarray gain problem rather than brute‑forcing all subarrays.
Maximum Frequency After Subarray Operation Python/Java solution
The standard implementation iterates through the array, computes the gain for each element relative to a target value, and applies Kadane’s algorithm to find the maximum gain segment. The same logic translates directly to Python, Java, C++, and Go with O(n) time complexity.
How to solve Maximum Frequency After Subarray Operation in O(n)?
Track the base count of the target value in the array. Convert the effect of applying the operation into a gain array where beneficial conversions add +1 and harmful changes add -1. The best subarray to operate on is the maximum subarray sum, which can be computed in linear time using Kadane’s algorithm.
What is the best approach for Maximum Frequency After Subarray Operation?
The most efficient approach models the operation as a gain/loss problem and uses Kadane’s algorithm to find the best subarray to modify. First count the base frequency of a target value. Then compute how many additional elements can be converted by applying a single shift to a subarray. This results in an O(n) time solution.
Is Maximum Frequency After Subarray Operation asked at Google/Amazon/Meta?
Problems involving maximum frequency after modifications and subarray gain optimization are common in interviews at companies like Google, Amazon, and Meta. They typically test understanding of greedy techniques, prefix reasoning, and Kadane-style dynamic programming.
What data structure is used in Maximum Frequency After Subarray Operation?
The solution commonly uses arrays for iteration and a hash table to count frequencies of values. Dynamic programming logic similar to Kadane’s algorithm is applied to evaluate the best subarray transformation.
What is the time complexity of Maximum Frequency After Subarray Operation?
The optimal solution runs in O(n) time using a greedy maximum-subarray strategy similar to Kadane’s algorithm. Earlier brute-force approaches that check all subarrays require O(n^2) or O(n^3) time, which is too slow for large inputs.

Ready to solve this problem?

Practice Maximum Frequency After Subarray Operation with our built-in code editor and test cases.

Practice on FleetCode