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.
Split nums into two arrays A and B using the following rule:
nums must go into array A.B.Return the absolute difference between the sums of the two arrays: |sum(A) - sum(B)|.
Note: An empty array has a sum of 0.
Example 1:
Input: nums = [2,3,4]
Output: 1
Explanation:
nums[2] = 4 is placed in array A.nums[0] = 2 and nums[1] = 3 are placed in array B.sum(A) = 4, sum(B) = 2 + 3 = 5.|4 - 5| = 1.Example 2:
Input: nums = [-1,5,7,0]
Output: 3
Explanation:
nums[2] = 7 and nums[3] = 0 are placed in array A.nums[0] = -1 and nums[1] = 5 are placed in array B.sum(A) = 7 + 0 = 7, sum(B) = -1 + 5 = 4.|7 - 4| = 3.Constraints:
1 <= nums.length <= 105-109 <= nums[i] <= 109Problem summary: You are given an integer array nums. Split nums into two arrays A and B using the following rule: Elements at prime indices in nums must go into array A. All other elements must go into array B. Return the absolute difference between the sums of the two arrays: |sum(A) - sum(B)|. Note: An empty array has a sum of 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[2,3,4]
[-1,5,7,0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3618: Split Array by Prime Indices
class Solution {
private static final int M = 100000 + 10;
private static boolean[] primes = new boolean[M];
static {
for (int i = 0; i < M; i++) {
primes[i] = true;
}
primes[0] = primes[1] = false;
for (int i = 2; i < M; i++) {
if (primes[i]) {
for (int j = i + i; j < M; j += i) {
primes[j] = false;
}
}
}
}
public long splitArray(int[] nums) {
long ans = 0;
for (int i = 0; i < nums.length; ++i) {
ans += primes[i] ? nums[i] : -nums[i];
}
return Math.abs(ans);
}
}
// Accepted solution for LeetCode #3618: Split Array by Prime Indices
const M = 100000 + 10
var primes [M]bool
func init() {
for i := 0; i < M; i++ {
primes[i] = true
}
primes[0], primes[1] = false, false
for i := 2; i < M; i++ {
if primes[i] {
for j := i + i; j < M; j += i {
primes[j] = false
}
}
}
}
func splitArray(nums []int) (ans int64) {
for i, num := range nums {
if primes[i] {
ans += int64(num)
} else {
ans -= int64(num)
}
}
return max(ans, -ans)
}
# Accepted solution for LeetCode #3618: Split Array by Prime Indices
m = 10**5 + 10
primes = [True] * m
primes[0] = primes[1] = False
for i in range(2, m):
if primes[i]:
for j in range(i + i, m, i):
primes[j] = False
class Solution:
def splitArray(self, nums: List[int]) -> int:
return abs(sum(x if primes[i] else -x for i, x in enumerate(nums)))
// Accepted solution for LeetCode #3618: Split Array by Prime Indices
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3618: Split Array by Prime Indices
// class Solution {
// private static final int M = 100000 + 10;
// private static boolean[] primes = new boolean[M];
//
// static {
// for (int i = 0; i < M; i++) {
// primes[i] = true;
// }
// primes[0] = primes[1] = false;
//
// for (int i = 2; i < M; i++) {
// if (primes[i]) {
// for (int j = i + i; j < M; j += i) {
// primes[j] = false;
// }
// }
// }
// }
//
// public long splitArray(int[] nums) {
// long ans = 0;
// for (int i = 0; i < nums.length; ++i) {
// ans += primes[i] ? nums[i] : -nums[i];
// }
// return Math.abs(ans);
// }
// }
// Accepted solution for LeetCode #3618: Split Array by Prime Indices
const M = 100000 + 10;
const primes: boolean[] = Array(M).fill(true);
const init = (() => {
primes[0] = primes[1] = false;
for (let i = 2; i < M; i++) {
if (primes[i]) {
for (let j = i + i; j < M; j += i) {
primes[j] = false;
}
}
}
})();
function splitArray(nums: number[]): number {
let ans = 0;
for (let i = 0; i < nums.length; i++) {
ans += primes[i] ? nums[i] : -nums[i];
}
return Math.abs(ans);
}
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: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.