LeetCode Entry
2778. Sum of Squares of Special Elements
Sum of squares at positions divisible by size
2778. Sum of Squares of Special Elements easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1509
Problem TLDR
Sum of squares at positions divisible by size
Intuition
Brute force works. Optimization: check only the first sqrt(n) positions, and for each use i and size/i
Approach
- this problem requires attention to what is actually asked to do
Complexity
-
Time complexity: \(O(n)\)
-
Space complexity: \(O(1)\)
Code
fun sumOfSquares1(n: IntArray)=
n.indices.sumOf{(1-(n.size%(it+1)).sign)*n[it]*n[it]}
pub fn sum_of_squares(n: Vec<i32>) -> i32 {
n.iter().zip(1..).map(|(x,i)|x*x*((n.len()%i==0)as i32)).sum()
}
Comments