Minimize replacements by integers up to K to make the sum of equidistant array elements from the end equal
Last Updated :
24 Mar, 2023
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 is 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++
#include <bits/stdc++.h>
using namespace std;
void minReplacements(vector< int >& nums, int k)
{
int N = nums.size();
vector< int > memo(k * 2 + 2, 0);
for ( int i = 0; i < N / 2; ++i) {
int l = nums[i], r = nums[N - 1 - i];
--memo[min(l, r) + 1];
--memo[l + r];
++memo[l + r + 1];
++memo[max(l, r) + k + 1];
}
int ans = N;
int curr = N;
for ( int i = 2; i <= k * 2; ++i) {
curr += memo[i];
ans = min(ans, curr);
}
cout << ans;
}
int main()
{
vector< int > arr{ 1, 2, 4, 3 };
int K = 4;
minReplacements(arr, K);
return 0;
}
|
Java
import java.util.*;
import java.util.Arrays;
class GFG{
static void minReplacements( int [] nums, int k)
{
int N = nums.length;
int [] memo = new int [(k * 2 + 2 )];
Arrays.fill(memo, 0 );
for ( int i = 0 ; i < N / 2 ; ++i)
{
int l = nums[i], r = nums[N - 1 - i];
--memo[(Math.min(l, r) + 1 )];
--memo[l + r];
++memo[l + r + 1 ];
++memo[(Math.max(l, r) + k + 1 )];
}
int ans = N;
int curr = N;
for ( int i = 2 ; i <= k * 2 ; ++i)
{
curr += memo[i];
ans = Math.min(ans, curr);
}
System.out.println(ans);
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 4 , 3 };
int K = 4 ;
minReplacements(arr, K);
}
}
|
Python3
def minReplacements(nums, k):
N = len (nums)
memo = [ 0 ] * (k * 2 + 2 )
for i in range (N / / 2 ):
l, r = nums[i], nums[N - 1 - i]
memo[ min (l, r) + 1 ] - = 1
memo[l + r] - = 1
memo[l + r + 1 ] + = 1
memo[ max (l, r) + k + 1 ] + = 1
ans = N
curr = N
for i in range ( 2 , 2 * k + 1 ):
curr + = memo[i]
ans = min (ans, curr)
print (ans)
if __name__ = = '__main__' :
arr = [ 1 , 2 , 4 , 3 ]
K = 4
minReplacements(arr, K)
|
C#
using System;
class GFG{
static void minReplacements( int [] nums, int k)
{
int N = nums.Length;
int [] memo = new int [(k * 2 + 2)];
for ( int i = 0; i < k * 2 + 2; ++i)
{
memo[i] = 0;
}
for ( int i = 0; i < N / 2; ++i)
{
int l = nums[i], r = nums[N - 1 - i];
--memo[(Math.Min(l, r) + 1)];
--memo[l + r];
++memo[l + r + 1];
++memo[(Math.Max(l, r) + k + 1)];
}
int ans = N;
int curr = N;
for ( int i = 2; i <= k * 2; ++i)
{
curr += memo[i];
ans = Math.Min(ans, curr);
}
Console.WriteLine(ans);
}
public static void Main()
{
int [] arr = { 1, 2, 4, 3 };
int K = 4;
minReplacements(arr, K);
}
}
|
Javascript
<script>
function minReplacements(nums, k)
{
var N = nums.length;
var memo = Array(k*2 +2).fill(0);
for ( var i = 0; i < N / 2; ++i) {
var l = nums[i], r = nums[N - 1 - i];
--memo[(Math.min(l, r) + 1)];
--memo[l + r];
++memo[l + r + 1];
++memo[(Math.max(l, r) + k + 1)];
}
var ans = N;
var curr = N;
for ( var i = 2; i <= k * 2; ++i) {
curr += memo[i];
ans = Math.min(ans, curr);
}
document.write( ans);
}
var arr = [ 1, 2, 4, 3 ];
var K = 4;
minReplacements(arr, K);
</script>
|
Time Complexity: O(N + K)
Auxiliary Space: O(K)
Share your thoughts in the comments
Please Login to comment...