Asset Group Consolidation
Zerodha manages a vast portfolio of financial assets for its clients. As market conditions evolve or corporate actions occur (e.g., mergers), certain distinct assets might become functionally equivalent for risk assessment, reporting, or portfolio rebalancing purposes. This problem challenges you to efficiently track and manage these equivalency relationships among assets.
You are tasked with implementing a system to manage asset equivalency groups. Initially, all assets are considered distinct. Your system must support two types of operations:
You need to process a sequence of these operations and return the results for all QUERY operations in the order they appear.
Your function consolidateAssetGroups will take two arguments:
n: An integer representing the total number of distinct assets, labeled from 0 to n-1.operations: An array of arrays. Each inner array represents an operation:
["UNION", assetA, assetB]: Indicates that assetA and assetB should be considered equivalent. assetA and assetB are integers within the range [0, n-1].["QUERY", assetA, assetB]: Asks whether assetA and assetB are currently considered equivalent. assetA and assetB are integers within the range [0, n-1].The function should return an array of booleans. Each boolean corresponds to the result of a QUERY operation, in the exact order they were processed. true if the assets are equivalent, false otherwise.
0 to n-1.QUERY operation asks about assetA and assetA, it should always return true.Examples
Example 1
Input:
n = 5, operations = [["UNION", 0, 1], ["QUERY", 0, 1], ["UNION", 2, 3], ["QUERY", 1, 3], ["UNION", 0, 2], ["QUERY", 1, 3]]
Output:
[true,false,true]
Note:
Initial groups: {0}, {1}, {2}, {3}, {4}
Example 2
Input:
n = 3, operations = [["QUERY", 0, 0], ["UNION", 0, 1], ["QUERY", 1, 2], ["UNION", 1, 2], ["QUERY", 0, 2]]
Output:
[true,false,true]
Note:
Initial groups: {0}, {1}, {2}
Example 3
Input:
n = 1, operations = [["QUERY", 0, 0], ["UNION", 0, 0]]
Output:
[true]
Note:
Initial groups: {0}
Constraints
1 <= n <= 10^51 <= operations.length <= 2 * 10^50 <= assetA, assetB < n for all operations.assetA and assetB can be the same in an operation. UNION(A, A) means nothing changes. QUERY(A, A) should be true.[0, n-1]). Assume all inputs are valid.N and operations.length.Loading editor...