AlgoMaster Logo
AlgoMasterMerge Two G-Countersmedium

Merge Two G-Counters

medium

A grow-only counter, or G-Counter, stores one nondecreasing component per replica. Its value is the sum of those components. Two replicas converge by retaining the greatest count observed for each component.

Design a GCounterMerger class:

  • GCounterMerger() creates a stateless merger.
  • int mergedValue(int[] a, int[] b) returns the value after merging the two states.

The arrays have the same length. Merge component i as max(a[i], b[i]), then return the sum of all merged components. Do not mutate either input array.

Example 1:

Input:

Output:

Explanation: The merged state is [3,2,3], so its value is 3 + 2 + 3 = 8.

Example 2:

Input:

Output:

Explanation: The two states contain progress from different replicas. The merged state is [10,10,0].

Constraints

  • 1 <= a.length == b.length <= 10^4
  • 0 <= a[i], b[i] <= 10^4
  • The merged value fits in a signed 32-bit integer.
  • At most 100 calls are made to mergedValue.
Hints

Loading...
CallReturns
new GCounterMerger()null
mergedValue([1,2,3], [3,1,0])8

The component-wise maximum is [3,2,3], whose sum is 8.

Run checks these cases. Submit also runs a larger hidden set.