Swift | O(n) -> O(√n) time optimized
Trivial solution\n- Time complexity: $O(n)$\n- Space complexity: $O(1)$\n\nclass Solution {\n    func sumOfSquares(_ nums: [Int]) -> Int {\n        var sum = 0,
# Trivial solution\n- Time complexity: $O(n)$\n- Space complexity: $O(1)$\n```\nclass Solution {\n    func sumOfSquares(_ nums: [Int]) -> Int {\n        var sum = 0, n = nums.count\n        for (i, num) in nums.enumerated() {\n            if n % (i+1) == 0 { sum += num*num }\n        }\n        return sum\n    }\n}\n```\n# Optimizations\n1. We can check divisors not from $1$ to $n$, but from $1$ to $\\sqrt{n}$, since any pair of divisors $x \\cdot y = n$ can be arranged that $x \\le \\sqrt{n} \\le y$.\n\n\n# Complexity\n- Time complexity: $O(\\sqrt{n})$\n- Space complexity: $O(1)$\n\n```\nextension Int { var squared:  Int { self * self } }\nclass Solution {\n    func sumOfSquares(_ nums: [Int]) -> Int\n    {\n        var i = 1, sum = 0, n = nums.count\n        while i.squared < n {\n            if n % i == 0 { sum += nums[i-1].squared + nums[n/i - 1].squared }\n            i += 1\n        }\n        if i.squared == n { sum += nums[i-1].squared }\n        return sum\n    }\n}\n```\n2. It can be noted that special indices do not depend on nums and can be precomputed and/or cached\n\n# Complexity\n- Time complexity: $O(\\sqrt{n})$\n- Space complexity: $O(n \\cdot \\sqrt{n}) = O(n^{\\frac{3}{2}})$\n\n# Code\n```\nextension Int { var squared:  Int { self * self } }\nclass Solution\n{\n    func sumOfSquares(_ nums:  [Int]) -> Int {\n        getIndexes(nums.count).reduce(0){ $0 + nums[$1].squared }\n    }\n\n    func getIndexes(_ n:  Int) -> [Int]\n    {\n        if let indexes = specialIndexes[n] { return indexes }\n        else\n        {\n            var i = 1, indexes:  [Int] = []\n            while i.squared < n {\n                if n % i == 0 { indexes.append(i - 1); indexes.append(n/i - 1) }\n                i += 1\n            }\n            if i.squared == n { indexes.append(i - 1) }\n            specialIndexes[n] = indexes\n            return indexes\n        }\n    }\n}\n// static var for caching between tests\nvar specialIndexes: [Int:[Int]] = [\n1 :  [0],\n2 :  [0, 1],\n3 :  [0, 2],\n4 :  [0, 1, 3],\n5 :  [0, 4],\n6 :  [0, 1, 2, 5],\n7 :  [0, 6],\n8 :  [0, 1, 3, 7],\n9 :  [0, 2, 8],\n10 : [0, 1, 4, 9],\n11 : [0, 10],\n12 : [0, 1, 2, 3, 5, 11],\n13 : [0, 12],\n14 : [0, 1, 6, 13],\n15 : [0, 2, 4, 14],\n16 : [0, 1, 3, 7, 15],\n17 : [0, 16],\n18 : [0, 1, 2, 5, 8, 17],\n19 : [0, 18],\n20 : [0, 1, 3, 4, 9, 19],\n21 : [0, 2, 6, 20],\n22 : [0, 1, 10, 21],\n23 : [0, 22],\n24 : [0, 1, 2, 3, 5, 7, 11, 23],\n25 : [0, 4, 24],\n26 : [0, 1, 12, 25],\n27 : [0, 2, 8, 26],\n28 : [0, 1, 3, 6, 13, 27],\n29 : [0, 28],\n30 : [0, 1, 2, 4, 5, 9, 14, 29],\n31 : [0, 30],\n32 : [0, 1, 3, 7, 15, 31],\n33 : [0, 2, 10, 32],\n34 : [0, 1, 16, 33],\n35 : [0, 4, 6, 34],\n36 : [0, 1, 2, 3, 5, 8, 11, 17, 35],\n37 : [0, 36],\n38 : [0, 1, 18, 37],\n39 : [0, 2, 12, 38],\n40 : [0, 1, 3, 4, 7, 9, 19, 39],\n41 : [0, 40],\n42 : [0, 1, 2, 5, 6, 13, 20, 41],\n43 : [0, 42],\n44 : [0, 1, 3, 10, 21, 43],\n45 : [0, 2, 4, 8, 14, 44],\n46 : [0, 1, 22, 45],\n47 : [0, 46],\n48 : [0, 1, 2, 3, 5, 7, 11, 15, 23, 47],\n49 : [0, 6, 48],\n50 : [0, 1, 4, 9, 24, 49]]\n```
['Math', 'Swift']