O(n*k) in Rust but still beats 100%
Intuition\n Describe your first thoughts on how to solve this problem. \nYou can save k iterations for every n element by implementing a sliding windows approac
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nYou can save k iterations for every n element by implementing a sliding windows approach for calculating freq of element in the subarray.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n*k)$$\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```rust []\nuse std::collections::HashMap;\nuse std::collections::BinaryHeap;\nuse std::collections::HashSet;\n\nimpl Solution {\n    pub fn find_x_sum(nums: Vec<i32>, k: i32, x: i32) -> Vec<i32> {\n        let n = nums.len() as i32;\n        let mut freq = HashMap::new();\n        let mut ans = vec![];\n\n        nums[..k as usize].iter().for_each(|&n| *freq.entry(n).or_insert(0) += 1);\n\n        for start in 1..n-k+2 {\n            let mut sum = 0;\n\n            let mut popular = BinaryHeap::new();\n            for (v, f) in freq.iter() {\n                popular.push((*f,*v));\n            }\n            //let mut popular: BinaryHeap<(i32, i32)> = freq.iter().map(|(&v, &f)| (f, v)).collect();                \n\n            for i in 0..x {\n                if let Some((f, v)) = popular.pop() {\n                    sum += f*v;\n                } \n            }\n            ans.push(sum);\n\n            if start + k < n+1 {\n                let prev = nums[(start-1) as usize];\n                let new = nums[(start + k-1) as usize];\n\n                if let Some(f) = freq.get_mut(&prev) {                        \n                    *f -= 1;\n                    if (*f == 0) {\n                        freq.remove(&prev);\n                    }\n                }\n\n                *freq.entry(new).or_insert(0) += 1;\n            } \n\n        }\n        \n        ans\n   }\n}\n```
['Rust']