Java ✅ || Simple Solution ✅ || Runtime - 100% beats ✅ || Memory - 100% beats ✅ || With Explanation ✅
Approach\nThe problem asks us to find the sum of the squares of the special elements in the given array nums. An element nums[i] is considered special if its in
# Approach\nThe problem asks us to find the sum of the squares of the special elements in the given array `nums`. An element `nums[i]` is considered special if its index `i + 1` divides the length of the array `n` without any remainder, i.e., `n % (i + 1) == 0`. We need to find all such special elements and calculate the sum of their squares.\n\n**Logic:**\n1. We start by initializing a variable `sum` to 0. This will keep track of the sum of squares of the special elements.\n\n2. We use a for loop to iterate through each element in the `nums` array. The loop variable `i` represents the index of the current element.\n\n3. Inside the loop, we check if the current index `i + 1` is a divisor of the length of the array `n`, i.e., `n % (i + 1) == 0`.\n\n4. If the condition is true, it means that the current element `nums[i]` is special because its index is a divisor of the array length.\n\n5. We then calculate the square of the special element `nums[i] * nums[i]` and add it to the `sum` variable.\n\n6. The loop continues for all elements in the array, and we keep adding the squares of the special elements to the `sum`.\n\n7. Once the loop is finished, the `sum` variable contains the sum of the squares of all special elements.\n\n8. Finally, we return the value of `sum` as the result of the function.\n\n**Example:**\nLet\'s take the first example to see how the logic works:\n\nInput: nums = [1, 2, 3, 4]\n\n1. `sum` is initialized to 0.\n\n2. The loop starts with the first element. `i = 0`. The index is 1, and it divides the length of the array (4) without any remainder. So, `nums[0]` (which is 1) is special. We add its square (1 * 1) to `sum`.\n\n3. The loop continues for the second element. `i = 1`. The index is 2, and it divides the length of the array (4) without any remainder. So, `nums[1]` (which is 2) is special. We add its square (2 * 2) to `sum`.\n\n4. The loop continues for the third element. `i = 2`. The index is 3, and it does not divide the length of the array (4) without any remainder. So, `nums[2]` (which is 3) is not special. We do not add its square to `sum`.\n\n5. The loop continues for the fourth element. `i = 3`. The index is 4, and it divides the length of the array (4) without any remainder. So, `nums[3]` (which is 4) is special. We add its square (4 * 4) to `sum`.\n\n6. The loop is finished, and we return `sum`, which is 21.\n\nSo, the output is 21, which is the correct answer as it represents the sum of squares of the special elements in the array.\n\n# Complexity\n- Time complexity: $$O(n)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```\nclass Solution {\n    public int sumOfSquares(int[] nums) {\n        int sum = 0;\n        int n = nums.length;\n        for(int i = 0; i < n; i++)\n            if(n % (i + 1) == 0)\n                sum += nums[i] * nums[i];\n        return sum;\n    }\n}\n```
['Array', 'Simulation', 'Java', 'C#']