Problem

Replace Values and Return Sums

AmazonFULLTIMEOA
See Amazon hiring insights

You are given an integer array entries and a 2D integer array transactions. Each transaction is a pair [oldValue, newValue].

For each transaction, replace every occurrence of oldValue in entries with newValue. After applying the transaction, record the sum of all values in entries.

Return an array containing the recorded sums after each transaction.

The sums may exceed the range of a 32-bit integer.

Examples
01 · Example 1
entries = [1, 2, 1, 3]
transactions = [[1, 4], [2, 1], [4, 2]]
return = [13, 12, 8]

After replacing 1 with 4, the array is [4,2,4,3] with sum 13. After replacing 2 with 1, the sum is 12. After replacing 4 with 2, the sum is 8.

02 · Example 2
entries = [5, 5]
transactions = [[1, 2], [5, 1]]
return = [10, 2]

The first transaction has no matching value, so the sum stays 10. The second transaction changes both values to 1, so the sum becomes 2.

Constraints
  • transactions[i].length == 2
  • The returned sums may require 64-bit integers.
More Amazon problems
drafts saved locally
public long[] replaceValuesAndReturnSums(int[] entries, int[][] transactions) {
  // write your code here
}
entries[1, 2, 1, 3]
transactions[[1, 4], [2, 1], [4, 2]]
expected[13, 12, 8]
sign in to submit