Problem · Array

Consistent Logs

Learn this problem
MediumRippling logoRipplingNEW GRADINTERNOA

Problem statement

You are given an integer array userEvent, where userEvent[i] is the ID of the user who triggered event i.

Let minimumGlobalFrequency be the smallest total frequency of any user ID in the entire array. A contiguous subarray is consistent when the frequency of its most frequent user is exactly minimumGlobalFrequency.

Return the maximum length of a consistent subarray.

Function

findConsistentLogs(userEvent: int[]) → int

Examples

Example 1

userEvent = [1,2,1,3,4,2,4,3,3,4]return = 8

In the entire array, users 1 and 2 each appear 2 times, while users 3 and 4 each appear 3 times. Therefore, minimumGlobalFrequency = 2.

The subarray [1,2,1,3,4,2,4,3] has length 8, and every user in it appears exactly 2 times. Its maximum frequency is therefore 2, so it is consistent. Every subarray of length at least 9 contains a user with frequency 3, so 8 is the maximum length.

Example 2

userEvent = [7,7,8,8]return = 4

Both users appear 2 times in the entire array, so minimumGlobalFrequency = 2. The full array is consistent and has length 4.

Constraints

  • 1 <= userEvent.length <= 3 * 10^5
  • 1 <= userEvent[i] <= 10^9

More Rippling problems

drafts saved locally
public int findConsistentLogs(int[] userEvent) {
  // Write your code here.
}
userEvent[1,2,1,3,4,2,4,3,3,4]
expected8
checking account