Sum of Squares of Special Elements

The task is to find the sum of the squares of all special elements in the given array. A special element is an element that is placed on the index which divides the length of the array. Let’s solve this task with a Ruby code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# @param {Integer[]} nums
# @return {Integer}
def sum_of_specials(nums)
    n = nums.size
    sum = 0
    (1..n).each do |i|
        sum += nums[i - 1]**2 if n % i == 0
    end
    sum
end

In this Ruby function sum_of_specials, we calculate the sum of the squares of the special elements. We iterate over all the indices from 1 to n inclusive (using (1..n).each) and for each index i, we check if it divides n (if n % i == 0). If it does, we add the square of the element at that index to the sum.

Remember, in Ruby, array indexing starts from 0, so when we want to access an element at index i (considering 1-based indexing), we use nums[i - 1].

Finally, we return the sum as the result.

What is wrong with my code:

def sum_of_squares(nums) result = 0 n = nums.size

for i in (1..n)
    if n % (i-1) == 0
        result += nums[i-1]
    end
end
result

end

There are two main issues with your code:

  1. You’re using (i-1) in the if n % (i-1) == 0 condition. This leads to a problem when i is 1, because (i-1) becomes 0 and dividing by zero is undefined. Instead, you should directly check if n % i == 0.

  2. In the line result += nums[i-1], you’re adding the value of nums[i-1] to the result, but the problem statement requires you to add the square of the value. Therefore, you should be squaring this value before adding it to the result.

The corrected version of your code is:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
def sum_of_squares(nums)
    result = 0
    n = nums.size

    for i in (1..n)
        if n % i == 0
            result += nums[i-1] ** 2
        end
    end
    result
end

This code now correctly calculates the sum of the squares of all special elements in the nums array. It goes through each index i from 1 to n (inclusive) and if i divides n, it adds the square of the value at that index (in the array) to the result. Finally, it returns the calculated sum.

What is 1-indexed integer array ?

A 1-indexed integer array is an array in which the first element is considered to be at index 1, rather than the usual 0 in many programming languages such as Ruby, Python, Java, etc.

For instance, in a 0-indexed array (which is most common in many programming languages), you’d access the first element with array[0]. However, in a 1-indexed array, you’d access the first element with array[1].

Notably, some languages, such as MATLAB or Lua, use 1-indexing by default. When a problem statement mentions a 1-indexed array, it means that you should consider the first element to be at index 1 for the purpose of the problem, even if the actual programming language you are using is 0-indexed.

For example, if you have an array [10, 20, 30, 40], the first element 10 is at index 1 in 1-indexed array but at index 0 in 0-indexed array (like in Ruby, Python or Java).

It confused because I don’t know how to handle the 1-indexed integer array. I am only familiar with 0 based starting index in array

That’s completely understandable. Many programming languages like Ruby, Python, C++, Java and more use 0-based indexing. When a problem statement gives you a 1-indexed array, it means that the first element is considered at index 1 for the purpose of the problem.

The simplest way to handle this is to remember to adjust your index when accessing elements. If i is your 1-based index, you access the corresponding element in a 0-based language with array[i - 1].

Let’s apply this to your sum_of_squares function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def sum_of_squares(nums)
    result = 0
    n = nums.size

    for i in (1..n)
        if n % i == 0
            # nums[i - 1] is used instead of nums[i] because Ruby uses 0-based indexing
            result += nums[i - 1] ** 2
        end
    end

    result
end

In the code above, to handle the 1-indexed i in Ruby (which is 0-indexed), we subtract 1 when accessing the element: nums[i - 1].

Remember, 1-indexing is just a different way to conceptualize the array for a given problem. The actual implementation will depend on the characteristics of the programming language you are using.

The reason it has been given in the problem statement is that it prevents division by 0 error. Am I right?

Yes, you’re correct. One key advantage of using 1-indexing in this specific problem is that it naturally prevents division by zero. The problem statement requires you to check if the array length n is divisible by the index i. If you were to start i from 0 in a 0-indexed array, you’d end up trying to compute n % 0, which would result in a division by zero error. By starting with a 1-indexed array (or iterating from 1 in a 0-indexed language), you avoid this issue.

My corrected solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# @param {Integer[]} nums
# @return {Integer}
def sum_of_squares(nums)
    result = 0
    n = nums.size

    for i in (1..n)
        if n % i == 0
            result += nums[i-1] * nums[i-1]
        end
    end
    result
end

Key Takeaways

  • Did not know the meaning of 1 indexed array. This lead to division by 0 error.