diff --git a/dsa-problems/leetcode-problems/3100-3199.md b/dsa-problems/leetcode-problems/3100-3199.md index ee901c12b..7c1a9e96c 100644 --- a/dsa-problems/leetcode-problems/3100-3199.md +++ b/dsa-problems/leetcode-problems/3100-3199.md @@ -56,7 +56,7 @@ export const problems = [ "problemName": "3107. Minimum Operations to Make Median of Array Equal to K", "difficulty": "Medium", "leetCodeLink": "https://leetcode.com/problems/minimum-operations-to-make-median-of-array-equal-to-k", -"solutionLink": "#" +"solutionLink": "/dsa-solutions/lc-solutions/3100-3199/Minimum-operation-to-make-median-of-array-equal-to-k" }, { "problemName": "3108. Minimum Cost Walk in Weighted Graph", diff --git a/dsa-solutions/lc-solutions/3100-3199/3100-Water-Bottles-II.md b/dsa-solutions/lc-solutions/3100-3199/3100-Water-Bottles-II.md index e08332816..cd141719a 100644 --- a/dsa-solutions/lc-solutions/3100-3199/3100-Water-Bottles-II.md +++ b/dsa-solutions/lc-solutions/3100-3199/3100-Water-Bottles-II.md @@ -2,7 +2,7 @@ id: water-bottles-II title: Water-Bottles-II level: hard -sidebar_label: Water-Bottles-II +sidebar_label: 3100-Water-Bottles-II tags: - Dynamic Programming - Bit Manipulation diff --git a/dsa-solutions/lc-solutions/3100-3199/3101-count-alternating-subarrays.md b/dsa-solutions/lc-solutions/3100-3199/3101-count-alternating-subarrays.md index 2aeadf6ad..18fba8661 100644 --- a/dsa-solutions/lc-solutions/3100-3199/3101-count-alternating-subarrays.md +++ b/dsa-solutions/lc-solutions/3100-3199/3101-count-alternating-subarrays.md @@ -2,7 +2,7 @@ id: Count-Alternating-subarrays title: Count-Alternating-subarrays level: hard -sidebar_label: Count-Alternating-subarrays +sidebar_label: 3101-Count-Alternating-subarrays tags: - Dynamic Programming - Bit Manipulation @@ -83,13 +83,11 @@ The provided code effectively implements the sliding window approach with the me 4. int left = 0;: Initializes left and right pointers, both starting at index 0. - - - int right = 0; + - int right = 0; 5. while (right < n): A loop that continues as long as right hasn't reached the end of the array. - - - while (right < n - 1 && nums[right] != nums[right + 1]): This inner loop extends the current subarray as long as adjacent elements differ. + - while (right < n - 1 && nums[right] != nums[right + 1]): This inner loop extends the current subarray as long as adjacent elements differ. 6. It checks if right is within bounds and if the elements at right and right + 1 are different. diff --git a/dsa-solutions/lc-solutions/3100-3199/3102-Minimize Manhattan Distance.md b/dsa-solutions/lc-solutions/3100-3199/3102-Minimize Manhattan Distance.md index 48d5473fb..e3c0b6c96 100644 --- a/dsa-solutions/lc-solutions/3100-3199/3102-Minimize Manhattan Distance.md +++ b/dsa-solutions/lc-solutions/3100-3199/3102-Minimize Manhattan Distance.md @@ -2,7 +2,7 @@ id: Minimize-Manhattan-Distances title: Minimize Manhattan Distances level: hard -sidebar_label: Minimize Manhattan Distances +sidebar_label: 3102-Minimize Manhattan Distances tags: - Dynamic Programming - Bit Manipulation @@ -74,15 +74,14 @@ Constraints: `maxi=max(maxsum-minsum,maxdiff-mindiff);` - - Now, find the values of minsum, maxsum, mindiff, maxdiff - - `minsum=st1.begin(), maxsum=--st1.end()` + - `minsum=st1.begin(), maxsum=--st1.end()` - - `mindiff=st2.begin(), maxdiff=--st2.end()` + - `mindiff=st2.begin(), maxdiff=--st2.end()` - `Note:- st.end() denotes an iterator pointing to the position - just after the last element that's why we have taken --st.end()` + `Note:- st.end() denotes an iterator pointing to the position + just after the last element that's why we have taken --st.end()` 8. After finding the maximum value upon removal of the current sum and difference, store the minimum computed so far in variable mini which holds the minimum of all Manhattan Distance found so far. diff --git a/dsa-solutions/lc-solutions/3100-3199/3107-Minimum-opration-to-make-median-of-array-equal-to-k.md b/dsa-solutions/lc-solutions/3100-3199/3107-Minimum-opration-to-make-median-of-array-equal-to-k.md new file mode 100644 index 000000000..f4d4ef780 --- /dev/null +++ b/dsa-solutions/lc-solutions/3100-3199/3107-Minimum-opration-to-make-median-of-array-equal-to-k.md @@ -0,0 +1,193 @@ +--- +id: Minimum-operation-to-make-median-of-array-equal-to-k +title: Minimum-operation-to-make-median-of-array-equal-to-k +sidebar_label: 3107-Minimum-operation-to-make-median-of-array-equal-to-k +tags: + - Java + - Greedy + - String +description: "This document provides a solution of Minimum-operation-to-make-median-of-array-equal-to-k" +--- + +## Problem statement: + +You are given an integer array nums and a non-negative integer k. In one operation, you can increase or decrease any element by 1. + +Return the minimum number of operations needed to make the median of nums equal to k. + +The median of an array is defined as the middle element of the array when it is sorted in non-decreasing order. If there are two choices for a median, the larger of the two values is taken. + +**Example 1:** + +Input: nums = [2,5,6,8,5], k = 4 +Output: 2 + +Explanation: +We can subtract one from nums[1] and nums[4] to obtain [2, 4, 6, 8, 4]. The median of the resulting array is equal to k. + +**Example 2:** + +Input: nums = [2,5,6,8,5], k = 7 +Output: 3 + +Explanation: +We can add one to nums[1] twice and add one to nums[2] once to obtain [2, 7, 7, 8, 5]. + +**Example 3:** + +Input: nums = [1,2,3,4,5,6], k = 4 +Output: 0 + +Explanation: +The median of the array is already equal to k. + +Constraints: + +`1 <= nums.length <= 2 * 105` +`1 <= nums[i] <= 109` +`1 <= k <= 109` + +## Solutions: + +### Intuition + +-> Plot the intial points and the expected points of the given array on a graph. +-> To find the expected median 'k' the expected graph should coincide with the inital graph. +-> Find minimum number of steps to coincide them. + +### Approach + +consider: nums = [2,5,6,8,5], k = 4 + + ![alt text](image-2.png) + +1. till the n/2th element-> all elements to the left of expected graph should be merged with the expected graph and those on the right wouldn't affect the median so no need to add in answer. + +2. for n/2th element it should be same for both graphs, so take difference and add. + +3. for elements after n/2-> the element to the left of expected graph wouldn't affect the median however those on the right will, so shift them so that they coincide and add the cost of shifting as well. + +### Observation: + +To make both the graphs coincide: + +1. For elements less than the median (i < n / 2): calculate the difference between the current element nums[i] and the target value k. If this difference is positive (i.e., nums[i] is greater than k), add this difference to ans. Otherwise, add 0(already conincided). + +2. For the median element itself (i == n / 2): calculate the absolute difference between the current element nums[i] and k and add it to ans. This ensures that regardless of whether k is greater or smaller than the median, its distance is considered. + +3. For elements greater than the median (i > n / 2): calculate the difference between k and the current element nums[i]. If this difference is positive (i.e., nums[i] is less than k), add this difference to ans. Otherwise, add 0(already coincided). + +## code: + + + + + ```cpp + #include + #include + using namespace std; + + class Solution { + public: + long long minOperationsToMakeMedianK(vector& nums, int k) { + int n = nums.size(); + sort(nums.begin(), nums.end()); + long long ans = 0; + for (int i = 0; i < n; i++) { + if (i < n / 2) + ans += max(0, nums[i] - k); // 1st case + else if (i == n / 2) + ans += abs(nums[i] - k); // 2nd case + else + ans += max(0, k - nums[i]); // 3rd case + } + return ans; + } + }; + ``` + + + + ```java + import java.util.*; + + public class Solution { + public long minOperationsToMakeMedianK(List nums, int k) { + int n = nums.size(); + Collections.sort(nums); + long ans = 0; + for (int i = 0; i < n; i++) { + if (i < n / 2) + ans += Math.max(0, nums.get(i) - k); // 1st case + else if (i == n / 2) + ans += Math.abs(nums.get(i) - k); // 2nd case + else + ans += Math.max(0, k - nums.get(i)); // 3rd case + } + return ans; + } + } + ``` + + + + ```python + from typing import List + + class Solution: + def minOperationsToMakeMedianK(self, nums: List[int], k: int) -> int: + n = len(nums) + nums.sort() + ans = 0 + for i in range(n): + if i < n // 2: + ans += max(0, nums[i] - k) # 1st case + elif i == n // 2: + ans += abs(nums[i] - k) # 2nd case + else: + ans += max(0, k - nums[i]) # 3rd case + return ans + ``` + + + + ```c + #include + #include + #include + + int cmp(const void* a, const void* b) { + return (*(int*)a - *(int*)b); + } + + long long minOperationsToMakeMedianK(int* nums, int numsSize, int k) { + qsort(nums, numsSize, sizeof(int), cmp); + long long ans = 0; + for (int i = 0; i < numsSize; i++) { + if (i < numsSize / 2) + ans += nums[i] > k ? nums[i] - k : 0; // 1st case + else if (i == numsSize / 2) + ans += abs(nums[i] - k); // 2nd case + else + ans += nums[i] < k ? k - nums[i] : 0; // 3rd case + } + return ans; + } + + int main() { + int nums[] = {1, 3, 2, 5, 4}; + int numsSize = sizeof(nums) / sizeof(nums[0]); + int k = 3; + printf("%lld\n", minOperationsToMakeMedianK(nums, numsSize, k)); + return 0; + } + ``` + + + + +## Complexity + +Time complexity: $O(nlogn)$ + +Space complexity: $O(1)$ diff --git a/dsa-solutions/lc-solutions/3100-3199/image-2.png b/dsa-solutions/lc-solutions/3100-3199/image-2.png new file mode 100644 index 000000000..ef5c48362 Binary files /dev/null and b/dsa-solutions/lc-solutions/3100-3199/image-2.png differ