FastPrepFastPrep
Problem Brief

Find Consistent Logs

INTERNOA

The developers working on a social media network app want to analyze user behavior. There are n event logs where userEvent[i] denotes the userId for the user that triggered the ith event. The team wants to analyze the subarray of the logs which are consistent, that is, the frequency of the most frequent user in the subarray is equal to the frequency of the least frequent user in the whole array. Find the maximum length of consistent logs.

Note: A subarray is a contiguous group of elements in an array.

Function Description

Complete the function findConsistentLogs in the editor 👉👉.

findConsistentLogs has the following parameters:

  1. int userEvent[n]: the userIds present in the event logs

Returns

int: the maximum length of consistent logs

𓇢𓆸, Credit to 𓇼 ⋆。˚Rachel 𓆝⋆。˚ 𓇼°

1Example 1

Input
userEvent = [1, 2, 1, 3, 4, 2, 4, 3, 3, 4]
Output
8
Explanation
* The frequencies of 1 and 2 are 2 * The frequencies of 3 and 4 are 3 The minimum frequency in the array is 2 The longest valid subarray has 8 elements: [1, 2, 1, 3, 4, 2, 4, 3] * The frequencies of 1, 2, 3, and 4 are all 2. The frequenccy of the most common element in the subarray is 2, the same as the min frequency in the entire array. Hence, the max len of the consistent logs is 8 :)

Constraints

Limits and guarantees your solution can rely on.

  • 1 <= n <= 3 * 105
  • 1 <= userEvent[i] <= 109
public int findConsistentLogs(int[] userEvent) {
  // write your code here :)
}
Input

userEvent

[1, 2, 1, 3, 4, 2, 4, 3, 3, 4]

Output

8

Sign in to submit your solution.