Problem · Array

Find Consistent Logs

HardGoogleOA
See Google hiring insights

The organizers of a gaming tournament want to analyze player participation based on event logs. There are n event logs, where arr[i] indicates the playerId of the player who participated in the ith event. The organizers need to identify subarrays of these logs that are consistent, meaning that the frequency of the most frequent player in the subarray matches the frequency of the least frequent player in the entire array. Determine the maximum length of such consistent logs.

Function Description

Complete the function findConsistentLogs with the following parameters:

int arr[n]: the playerIds present in the event logs

Returns

int: the maximum length of the consistent logs

Examples
01 · Example 1
arr = [1, 2, 1, 3, 4, 2, 4, 3, 3, 4]
return = 8
Given:
  • n = 10
  • arr = [1, 2, 1, 3, 4, 2, 4, 3, 3, 4]
  • The frequencies of playerIds 1 and 2 are 2. The frequencies of playerIds 3 and 4 are 3. The minimum frequency in the array is 2. The longest valid subarray with this property is [1, 2, 1, 3, 4, 2, 4, 3], which has 8 elements. In this subarray, the most common element appears 2 times, which matches the minimum frequency in the entire array. Therefore, the maximum length of consistent logs is 8.
    Constraints
    1 ≤ n ≤ 104
    More Google problems
    drafts saved locally
    public int findConsistentLogs(int[] arr) {
      // write your code here
    }
    
    arr[1, 2, 1, 3, 4, 2, 4, 3, 3, 4]
    expected8
    checking account