Sum Squared Values at Special Array Positions

Quick Overview

Add the squared values at array positions that divide the array length, using the problem’s one-based position convention.

Sum Squared Values at Special Array Positions

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Sum Squared Values at Special Array Positions You are given an integer array `nums` of length `n`, described using 1-based positions. Position `i` is special when `i` divides `n`, meaning `n % i == 0`. Return the sum of the squares of the elements at all special positions. ### Examples **Example 1** ```text Input: nums = [3,4,2,7,6] Output: 45 ``` The length is `5`, so the special positions are `1` and `5`. Their values are `3` and `6`, giving `3 * 3 + 6 * 6 = 45`. **Example 2** ```text Input: nums = [4,3,5,8,9,2] Output: 54 ``` The special positions for length `6` are `1`, `2`, `3`, and `6`. Their values are `4`, `3`, `5`, and `2`, so the required sum is `16 + 9 + 25 + 4 = 54`. ### Constraints - `1 <= nums.length == n <= 50`. - `1 <= nums[i] <= 50`. ### Clarifying Questions - Does divisibility apply to the value or its position? It applies to the 1-based position `i`. - Does a special element contribute its value or its square? Its square. - How does the problem's indexing map to the C++ vector? Logical position `i` is stored at `nums[i - 1]`. - Are the first and last positions special? Yes; both `1` and `n` divide `n`. If `n == 1`, they are the same single position, counted once. ### C++ Interface ```cpp class Solution { public: int sumOfSquares(vector<int>& nums) { } }; ``` ### Reasoning Checkpoints - Keep the mathematical positions separate from C++ storage indices. - Apply the special-position definition rather than a test on the element value. - Count each qualifying position once, including the length-one case. ```hint Position versus value Write down the C++ vector index corresponding to each logical position before deciding which value to square. ```

Overview: Add the squared values at array positions that divide the array length, using the problem’s one-based position convention.

Bloomberg logo
Bloomberg
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Sum Squared Values at Special Array Positions

You are given an integer array nums of length n, described using 1-based positions. Position i is special when i divides n, meaning n % i == 0.

Return the sum of the squares of the elements at all special positions.

Examples

Example 1

Input: nums = [3,4,2,7,6]
Output: 45

The length is 5, so the special positions are 1 and 5. Their values are 3 and 6, giving 3 * 3 + 6 * 6 = 45.

Example 2

Input: nums = [4,3,5,8,9,2]
Output: 54

The special positions for length 6 are 1, 2, 3, and 6. Their values are 4, 3, 5, and 2, so the required sum is 16 + 9 + 25 + 4 = 54.

Constraints

  • 1 <= nums.length == n <= 50 .
  • 1 <= nums[i] <= 50 .

Clarifying Questions Guidance

  • Does divisibility apply to the value or its position? It applies to the 1-based position i .
  • Does a special element contribute its value or its square? Its square.
  • How does the problem's indexing map to the C++ vector? Logical position i is stored at nums[i - 1] .
  • Are the first and last positions special? Yes; both 1 and n divide n . If n == 1 , they are the same single position, counted once.

C++ Interface

class Solution {
public:
    int sumOfSquares(vector<int>& nums) {
        
    }
};

Reasoning Checkpoints

  • Keep the mathematical positions separate from C++ storage indices.
  • Apply the special-position definition rather than a test on the element value.
  • Count each qualifying position once, including the length-one case.