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.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given a 1-indexed integer array nums of length n.
An element nums[i] of nums is called special if i divides n, i.e. n % i == 0.
Return the sum of the squares of all special elements of nums.
Example 1:
Input: nums = [1,2,3,4] Output: 21 Explanation: There are exactly 3 special elements in nums: nums[1] since 1 divides 4, nums[2] since 2 divides 4, and nums[4] since 4 divides 4. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[4] * nums[4] = 1 * 1 + 2 * 2 + 4 * 4 = 21.
Example 2:
Input: nums = [2,7,1,19,18,3] Output: 63 Explanation: There are exactly 4 special elements in nums: nums[1] since 1 divides 6, nums[2] since 2 divides 6, nums[3] since 3 divides 6, and nums[6] since 6 divides 6. Hence, the sum of the squares of all special elements of nums is nums[1] * nums[1] + nums[2] * nums[2] + nums[3] * nums[3] + nums[6] * nums[6] = 2 * 2 + 7 * 7 + 1 * 1 + 3 * 3 = 63.
Constraints:
1 <= nums.length == n <= 501 <= nums[i] <= 50Problem summary: You are given a 1-indexed integer array nums of length n. An element nums[i] of nums is called special if i divides n, i.e. n % i == 0. Return the sum of the squares of all special elements of nums.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[1,2,3,4]
[2,7,1,19,18,3]
sum-of-square-numbers)sum-of-all-odd-length-subarrays)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2778: Sum of Squares of Special Elements
class Solution {
public int sumOfSquares(int[] nums) {
int n = nums.length;
int ans = 0;
for (int i = 1; i <= n; ++i) {
if (n % i == 0) {
ans += nums[i - 1] * nums[i - 1];
}
}
return ans;
}
}
// Accepted solution for LeetCode #2778: Sum of Squares of Special Elements
func sumOfSquares(nums []int) (ans int) {
n := len(nums)
for i, x := range nums {
if n%(i+1) == 0 {
ans += x * x
}
}
return
}
# Accepted solution for LeetCode #2778: Sum of Squares of Special Elements
class Solution:
def sumOfSquares(self, nums: List[int]) -> int:
n = len(nums)
return sum(x * x for i, x in enumerate(nums, 1) if n % i == 0)
// Accepted solution for LeetCode #2778: Sum of Squares of Special Elements
// 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 #2778: Sum of Squares of Special Elements
// class Solution {
// public int sumOfSquares(int[] nums) {
// int n = nums.length;
// int ans = 0;
// for (int i = 1; i <= n; ++i) {
// if (n % i == 0) {
// ans += nums[i - 1] * nums[i - 1];
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2778: Sum of Squares of Special Elements
function sumOfSquares(nums: number[]): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
if (n % (i + 1) === 0) {
ans += nums[i] * nums[i];
}
}
return 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.