Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 0-indexed array nums that consists of n distinct positive integers. Apply m operations to this array, where in the ith operation you replace the number operations[i][0] with operations[i][1].
It is guaranteed that in the ith operation:
operations[i][0] exists in nums.operations[i][1] does not exist in nums.Return the array obtained after applying all the operations.
Example 1:
Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]] Output: [3,2,7,1] Explanation: We perform the following operations on nums: - Replace the number 1 with 3. nums becomes [3,2,4,6]. - Replace the number 4 with 7. nums becomes [3,2,7,6]. - Replace the number 6 with 1. nums becomes [3,2,7,1]. We return the final array [3,2,7,1].
Example 2:
Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]] Output: [2,1] Explanation: We perform the following operations to nums: - Replace the number 1 with 3. nums becomes [3,2]. - Replace the number 2 with 1. nums becomes [3,1]. - Replace the number 3 with 2. nums becomes [2,1]. We return the array [2,1].
Constraints:
n == nums.lengthm == operations.length1 <= n, m <= 105nums are distinct.operations[i].length == 21 <= nums[i], operations[i][0], operations[i][1] <= 106operations[i][0] will exist in nums when applying the ith operation.operations[i][1] will not exist in nums when applying the ith operation.Problem summary: You are given a 0-indexed array nums that consists of n distinct positive integers. Apply m operations to this array, where in the ith operation you replace the number operations[i][0] with operations[i][1]. It is guaranteed that in the ith operation: operations[i][0] exists in nums. operations[i][1] does not exist in nums. Return the array obtained after applying all the operations.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,4,6] [[1,3],[4,7],[6,1]]
[1,2] [[1,3],[2,1],[3,2]]
find-all-numbers-disappeared-in-an-array)maximum-number-of-integers-to-choose-from-a-range-i)maximum-number-of-integers-to-choose-from-a-range-ii)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2295: Replace Elements in an Array
class Solution {
public int[] arrayChange(int[] nums, int[][] operations) {
int n = nums.length;
Map<Integer, Integer> d = new HashMap<>(n);
for (int i = 0; i < n; ++i) {
d.put(nums[i], i);
}
for (var op : operations) {
int x = op[0], y = op[1];
nums[d.get(x)] = y;
d.put(y, d.get(x));
}
return nums;
}
}
// Accepted solution for LeetCode #2295: Replace Elements in an Array
func arrayChange(nums []int, operations [][]int) []int {
d := map[int]int{}
for i, x := range nums {
d[x] = i
}
for _, op := range operations {
x, y := op[0], op[1]
nums[d[x]] = y
d[y] = d[x]
}
return nums
}
# Accepted solution for LeetCode #2295: Replace Elements in an Array
class Solution:
def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:
d = {x: i for i, x in enumerate(nums)}
for x, y in operations:
nums[d[x]] = y
d[y] = d[x]
return nums
// Accepted solution for LeetCode #2295: Replace Elements in an Array
/**
* [2295] Replace Elements in an Array
*
* You are given a 0-indexed array nums that consists of n distinct positive integers. Apply m operations to this array, where in the i^th operation you replace the number operations[i][0] with operations[i][1].
* It is guaranteed that in the i^th operation:
*
* operations[i][0] exists in nums.
* operations[i][1] does not exist in nums.
*
* Return the array obtained after applying all the operations.
*
* Example 1:
*
* Input: nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]
* Output: [3,2,7,1]
* Explanation: We perform the following operations on nums:
* - Replace the number 1 with 3. nums becomes [<u>3</u>,2,4,6].
* - Replace the number 4 with 7. nums becomes [3,2,<u>7</u>,6].
* - Replace the number 6 with 1. nums becomes [3,2,7,<u>1</u>].
* We return the final array [3,2,7,1].
*
* Example 2:
*
* Input: nums = [1,2], operations = [[1,3],[2,1],[3,2]]
* Output: [2,1]
* Explanation: We perform the following operations to nums:
* - Replace the number 1 with 3. nums becomes [<u>3</u>,2].
* - Replace the number 2 with 1. nums becomes [3,<u>1</u>].
* - Replace the number 3 with 2. nums becomes [<u>2</u>,1].
* We return the array [2,1].
*
*
* Constraints:
*
* n == nums.length
* m == operations.length
* 1 <= n, m <= 10^5
* All the values of nums are distinct.
* operations[i].length == 2
* 1 <= nums[i], operations[i][0], operations[i][1] <= 10^6
* operations[i][0] will exist in nums when applying the i^th operation.
* operations[i][1] will not exist in nums when applying the i^th operation.
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/replace-elements-in-an-array/
// discuss: https://leetcode.com/problems/replace-elements-in-an-array/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
// Credit: https://leetcode.com/problems/replace-elements-in-an-array/solutions/2991607/rust-concise-by-gvnpl-qalu/
pub fn array_change(nums: Vec<i32>, operations: Vec<Vec<i32>>) -> Vec<i32> {
let mut nums = nums;
let mut hm = std::collections::HashMap::new();
for i in 0..nums.len() {
hm.insert(nums[i], i);
}
for o in operations {
let i = hm.get(&o[0]).copied().unwrap_or(0);
nums[i] = o[1];
hm.insert(o[1], i);
}
nums
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2295_example_1() {
let nums = vec![1, 2, 4, 6];
let operations = vec![vec![1, 3], vec![4, 7], vec![6, 1]];
let result = vec![3, 2, 7, 1];
assert_eq!(Solution::array_change(nums, operations), result);
}
#[test]
fn test_2295_example_2() {
let nums = vec![1, 2];
let operations = vec![vec![1, 3], vec![2, 1], vec![3, 2]];
let result = vec![2, 1];
assert_eq!(Solution::array_change(nums, operations), result);
}
}
// Accepted solution for LeetCode #2295: Replace Elements in an Array
function arrayChange(nums: number[], operations: number[][]): number[] {
const d: Map<number, number> = new Map(nums.map((x, i) => [x, i]));
for (const [x, y] of operations) {
nums[d.get(x)!] = y;
d.set(y, d.get(x)!);
}
return nums;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.