Given an array arr[] of even length N and an integer K, the task is to minimize the count of replacing array elements by an integer from the range [1, K] such that the sum of all pair of equidistant elements from the end of the array are equal.
Examples:
Input: arr[] = {1, 2, 4, 3}, K = 4
Output: 1
Explanation:
Replacing arr[2] by 2 modifies arr[] to {1, 2, 2, 3}.
arr[0] + arr[3] = 1 + 3 = 4.
arr[1] + arr[2] = 2 + 2 = 4.
Therefore, the sum of equidistant elements from the end of the array are equal(i.e., arr[i] + arr[N – 1 – i] = 4 for every i).Input: arr[] = [1, 2, 1, 2], K = 2
Output: 0
Approach: The minimum possible sum of the pair (arr[i], arr[N – i – 1]) is 2 by changing both the values to 1, and the maximum possible sum is 2 * K by changing both the values to K. Hence, for each pair of numbers (at index i and N – 1 – i) l and r:
- By 2 replacements: The sum between the range [2, 2 * K] can be achieved.
- By only 1 replacement:
- The minimum sum that can be achieved, say minSum, is (min(L, R) + 1).
- The maximum sum that can be achieved, say maxSum, is (max(L, R) + K).
- By no replacement: The sum (L + R) remains. Let it be pairSum.
Deducing from the above results, for a pair (L, R):
- In order to get the sum in the range [2, minSum – 1], 2 replacements are required.
- In order to get the sum in the range [minSum, pairSum – 1], 1 replacement is required.
- In order to get the sum pairSum, no replacement is required.
- In order to get the sum in the range [pairSum + 1, maxSum], 1 replacement is required.
- In order to get the sum in the range [maxSum + 1, 2*K], 2 replacements are required.
Therefore, for each pair of array elements
- Start with 2 replacements.
- From minSum onwards, 1 less replacement is required.
- For pairSum, 1 further less replacement is required.
- From pairSum + 1, 1 additional replacement is required.
- From maxSum + 1, 1 further additional replacement is required.
Follow the steps below to solve the problem:
- Initialize an auxiliary array, say memo[], of size (2 * K + 2), where memo[i] denotes the minimum number of replacements required to make the sum of all required pairs equal to i – N.
- Iterate over the indices [0, N/2 – 1] using the variable i.
- Store the left element of the pair, i.e., arr[i] in L, and the right element of the pair, i.e., arr[N – i – 1] in R.
- Decrement memo[min(L, R) + 1] and memo[L + R] by 1.
- Increment memo[L + R+ 1] and memo[max(L, R) + K + 1] by 1.
- Initialize ans as N to store the minimum number of required moves and curr as N to store the prefix sum of the memo[] at each step.
- Find the prefix sum of the array memo[] and in each iteration update the value of curr and ans if curr is less than ans.
- After the above steps, print the value of ans as the result.
Below is the implementation of the above approach:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to count the minimum replacements // required to make the sum of equidistant // array elements from the end equal void minReplacements(vector<int>& nums, int k) { // Store the size of nums array int N = nums.size(); // Initialize an auxiliary array vector<int> memo(k * 2 + 2, 0); // Iterate nums in range[0, N/2-1] for (int i = 0; i < N / 2; ++i) { // Store the left element and // the right element int l = nums[i], r = nums[N - 1 - i]; // Decrement memo[min(l, r) + 1] by 1 --memo[min(l, r) + 1]; // Decrement memo[l + r] by 1 --memo[l + r]; // Increment memo[l + r + 1] by 1 ++memo[l + r + 1]; // Increment memo[max(l, r) + k + 1] by 1 ++memo[max(l, r) + k + 1]; } // Store the minimum number of moves int ans = N; int curr = N; // Find the prefix sum of memo[] for (int i = 2; i <= k * 2; ++i) { curr += memo[i]; // Update ans ans = min(ans, curr); } // Print the answer cout << ans; } // Driver Code int main() { vector<int> arr{ 1, 2, 4, 3 }; int K = 4; // Function Call minReplacements(arr, K); return 0; } |
1
Time Complexity: O(N + K)
Auxiliary Space: O(K)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimize difference between maximum and minimum of Array by at most K replacements
- Minimum replacements to make elements of a ternary array same
- Maximize XOR subsequence possible by equidistant elements from both ends
- Minimize cost of choosing and skipping array elements to reach end of the given array
- Queries to replace subarrays by equal length arrays with at most P replacements allowed for any array element
- Kth array element after M replacements of array elements by XOR of adjacent pairs
- Minimize increment/decrement of Array elements to make each modulo K equal
- Minimize adding odd and subtracting even numbers to make all array elements equal to K
- Minimum replacements to make adjacent characters unequal in a ternary string | Set-2
- Form lexicographically smallest string with minimum replacements having equal number of 0s, 1s and 2s
- Number of cells in matrix which are equidistant from given two points
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Minimum sum of values subtracted from array elements to make all array elements equal
- Minimize increments or decrements required to make sum and product of array elements non-zero
- Make max elements in B[] equal to that of A[] by adding/subtracting integers in range [0, K]
- Lexicographically smallest permutation with distinct elements using minimum replacements
- Minimize operations required to make each element of Array equal to it's index value
- Minimize the number of steps required to reach the end of the array
- Minimize the number of steps required to reach the end of the array | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

