Tap here to switch tabs
Problem
Submissions

Problem: Find X-Sum of All K-Long Subarrays I

med
30 min
Try to solve the Find X-Sum of All K-Long Subarrays I problem.

Statement

You are given an integer array nums of length n, along with two integers k and x.

The xx sum of an array is determined by the following process:

  • Count the frequency of each distinct element in the array.

  • Choose the top x most frequent elements. If two elements have the same frequency, the element with the greater value is preferred. If the array contains fewer than x distinct elements, choose all elements.

  • Compute the sum of all occurrences of the chosen elements.

Return an integer array answer of length n −- k +1+ 1 where answer[i] represents the xx sum of the subarray nums[i..i + k - 1].

Constraints:

  • 1≤1 \leq n ==== nums.length ≤50\leq 50

  • 1≤1 \leq nums[i] ≤50\leq 50

  • 1≤1 \leq x ≤\leq k ≤\leq nums.length

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Find X-Sum of All K-Long Subarrays I

med
30 min
Try to solve the Find X-Sum of All K-Long Subarrays I problem.

Statement

You are given an integer array nums of length n, along with two integers k and x.

The xx sum of an array is determined by the following process:

  • Count the frequency of each distinct element in the array.

  • Choose the top x most frequent elements. If two elements have the same frequency, the element with the greater value is preferred. If the array contains fewer than x distinct elements, choose all elements.

  • Compute the sum of all occurrences of the chosen elements.

Return an integer array answer of length n −- k +1+ 1 where answer[i] represents the xx sum of the subarray nums[i..i + k - 1].

Constraints:

  • 1≤1 \leq n ==== nums.length ≤50\leq 50

  • 1≤1 \leq nums[i] ≤50\leq 50

  • 1≤1 \leq x ≤\leq k ≤\leq nums.length