Transform Array Using Pair Operations
Invariant (Sum Preserved)
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementYou are given two integer arrays source and target.ReadHide
Problem Statement
You are given two integer arrays source and target.
In one operation, you may choose two distinct indices i and j in source, along with any integer delta. Then update:
source[i] = source[i] + source[j] - delta
source[j] = delta
Return true if it is possible to make source equal to target after any number of operations (including zero), otherwise false.
Input / Output
Input: source = [1,2,3], target = [0,2,4]
Output: true
Explanation: i = 0, j = 2, delta = 4 → [0, 2, 4]
Input: source = [-5,-5], target = [-15,5]
Output: true
Input: source = [1,2,1], target = [0,2,5]
Output: false
Constraints
2 ≤ source.length == target.length ≤ 10^5
-10^9 ≤ source[i], target[i] ≤ 10^9
Understanding the Problem
Look at what happens to source[i] + source[j]:
before: source[i] + source[j]
after: (source[i] + source[j] - delta) + delta = source[i] + source[j]
→ Every operation preserves the total sum. So sum(source) == sum(target) is necessary.
It is also sufficient (n ≥ 2): use the last index as a "sink".
For every j < n-1, apply the operation with i = n-1 and delta = target[j].
Each source[j] becomes exactly target[j], and the sink absorbs the difference —
since the sum never changes, the sink ends up equal to target[n-1].
Edge Cases
- Arrays already equal → true (zero operations)
- Values up to 10^9 × 10^5 → sums need 64-bit integers
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Pattern Recognition
Find the Invariant — when an operation looks arbitrary, check what it can't change. Here the only invariant is the sum, and it's the whole answer.
Approach
Optimized — O(n)
return sum(source) == sum(target) (use 64-bit sums)
Complexity
| Approach | Time | Space | Notes |
|---|---|---|---|
| Sum Invariant | O(n) | O(1) | Watch overflow — use long |
Online Compiler
Solution code
import sys
input = sys.stdin.readline
# ─────────────────────────────────────────────────────────────────────────
# OPTIMIZED — O(n) time | O(1) space
# Operation preserves source[i] + source[j] → total sum is invariant.
# With n >= 2, any target with the same sum is reachable (last index as sink).
# ─────────────────────────────────────────────────────────────────────────
def can_transform(source, target):
return sum(source) == sum(target) # Python ints never overflow
# ── Input ─────────────────────────────────────────────────────────────────
n = int(input())
source = list(map(int, input().split()))
target = list(map(int, input().split()))
print("true" if can_transform(source, target) else "false")Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
- Community solutions on LeetCode ↗ for many more versions, in many languages. Read them after you've solved it.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language