Problem
Submissions

Problem: Minimum Operations to Make All Array Elements Equal

Statement

You are given an array, nums, consisting of positive integers of size n and another array queries of size m. For each query ii in queries, the goal is to make all elements of nums equal to queries[i]. To achieve this, you can perform the following operation any number of times:

  • Increase or decrease an element of nums by 1.

Return an array answer of size m, where answer[i] is the minimum number of operations needed to make all elements of nums equal to queries[i]. After processing each query, the nums array is reset to its original state.

Constraints

  • n ==== nums.length

  • m ==== queries.length

  • 1≤1 \leq n, m ≤103\leq 10^3

  • 1≤1 \leq nums[i], queries[i] ≤105\leq 10^5

Problem
Submissions

Problem: Minimum Operations to Make All Array Elements Equal

Statement

You are given an array, nums, consisting of positive integers of size n and another array queries of size m. For each query ii in queries, the goal is to make all elements of nums equal to queries[i]. To achieve this, you can perform the following operation any number of times:

  • Increase or decrease an element of nums by 1.

Return an array answer of size m, where answer[i] is the minimum number of operations needed to make all elements of nums equal to queries[i]. After processing each query, the nums array is reset to its original state.

Constraints

  • n ==== nums.length

  • m ==== queries.length

  • 1≤1 \leq n, m ≤103\leq 10^3

  • 1≤1 \leq nums[i], queries[i] ≤105\leq 10^5