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 an integer array nums of size n containing only 1 and -1, and an integer k.
You can perform the following operation at most k times:
Choose an index i (0 <= i < n - 1), and multiply both nums[i] and nums[i + 1] by -1.
Note that you can choose the same index i more than once in different operations.
Return true if it is possible to make all elements of the array equal after at most k operations, and false otherwise.
Example 1:
Input: nums = [1,-1,1,-1,1], k = 3
Output: true
Explanation:
We can make all elements in the array equal in 2 operations as follows:
i = 1, and multiply both nums[1] and nums[2] by -1. Now nums = [1,1,-1,-1,1].i = 2, and multiply both nums[2] and nums[3] by -1. Now nums = [1,1,1,1,1].Example 2:
Input: nums = [-1,-1,-1,1,1,1], k = 5
Output: false
Explanation:
It is not possible to make all array elements equal in at most 5 operations.
Constraints:
1 <= n == nums.length <= 105nums[i] is either -1 or 1.1 <= k <= nProblem summary: You are given an integer array nums of size n containing only 1 and -1, and an integer k. You can perform the following operation at most k times: Choose an index i (0 <= i < n - 1), and multiply both nums[i] and nums[i + 1] by -1. Note that you can choose the same index i more than once in different operations. Return true if it is possible to make all elements of the array equal after at most k operations, and false otherwise.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,-1,1,-1,1] 3
[-1,-1,-1,1,1,1] 5
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3576: Transform Array to All Equal Elements
class Solution {
public boolean canMakeEqual(int[] nums, int k) {
return check(nums, nums[0], k) || check(nums, -nums[0], k);
}
private boolean check(int[] nums, int target, int k) {
int cnt = 0, sign = 1;
for (int i = 0; i < nums.length - 1; ++i) {
int x = nums[i] * sign;
if (x == target) {
sign = 1;
} else {
sign = -1;
++cnt;
}
}
return cnt <= k && nums[nums.length - 1] * sign == target;
}
}
// Accepted solution for LeetCode #3576: Transform Array to All Equal Elements
func canMakeEqual(nums []int, k int) bool {
check := func(target, k int) bool {
cnt, sign := 0, 1
for i := 0; i < len(nums)-1; i++ {
x := nums[i] * sign
if x == target {
sign = 1
} else {
sign = -1
cnt++
}
}
return cnt <= k && nums[len(nums)-1]*sign == target
}
return check(nums[0], k) || check(-nums[0], k)
}
# Accepted solution for LeetCode #3576: Transform Array to All Equal Elements
class Solution:
def canMakeEqual(self, nums: List[int], k: int) -> bool:
def check(target: int, k: int) -> bool:
cnt, sign = 0, 1
for i in range(len(nums) - 1):
x = nums[i] * sign
if x == target:
sign = 1
else:
sign = -1
cnt += 1
return cnt <= k and nums[-1] * sign == target
return check(nums[0], k) or check(-nums[0], k)
// Accepted solution for LeetCode #3576: Transform Array to All Equal Elements
impl Solution {
pub fn can_make_equal(nums: Vec<i32>, k: i32) -> bool {
fn check(target: i32, k: i32, nums: &Vec<i32>) -> bool {
let mut cnt = 0;
let mut sign = 1;
for i in 0..nums.len() - 1 {
let x = nums[i] * sign;
if x == target {
sign = 1;
} else {
sign = -1;
cnt += 1;
}
}
cnt <= k && nums[nums.len() - 1] * sign == target
}
check(nums[0], k, &nums) || check(-nums[0], k, &nums)
}
}
// Accepted solution for LeetCode #3576: Transform Array to All Equal Elements
function canMakeEqual(nums: number[], k: number): boolean {
function check(target: number, k: number): boolean {
let [cnt, sign] = [0, 1];
for (let i = 0; i < nums.length - 1; i++) {
const x = nums[i] * sign;
if (x === target) {
sign = 1;
} else {
sign = -1;
cnt++;
}
}
return cnt <= k && nums[nums.length - 1] * sign === target;
}
return check(nums[0], k) || check(-nums[0], k);
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
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: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.