Count of replacements required to make the sum of all Pairs of given type from the Array equal
Given an integer array arr of length N and an integer K, the task is to find the number of array elements to be replaced by a value from the range [1, K] such that each pair (arr[i], arr[N – 1 – i] have equal sum.
Examples :
Input: arr[] = {1, 2, 2, 1}, K = 3
Output: 1
Explanation:
Replace arr[0] with 3, so that the array becomes {3, 2, 2, 1}.
Now, it can be seen that arr[0] + arr[3] = arr[1] + arr[2] = 4.
Input: arr[] = {1, 2, 1, 2, 1, 2}
Output: 0
Explanation:
No need to make any changes as arr[0] + arr[5] = arr[1] + arr[4] = arr[2] + arr[3] = 3
Naive Approach :
The simplest approach to this problem could be like iterate for all possible values of X, which can be any number in the range 1 to 2*K, and find the number of operation require to achieve pair sum equal to X. And finally return the minimum numbers of replacement from all operations.
Time Complexity : O (N * K)
Auxiliary Space : O (1)
Efficient Approach: The above approach can be optimized by the following observations:
- X can clearly take values in the range [2, 2 * K].
- Considering pairs arr[i] and arr[N – i – 1], it can be observed that the sum of any pair can be made equal to X in 0, 1 or 2 replacements.
- Let the maximum of these two numbers be mx and minimum be mn. In one replacement, the sum of this pair will be in the range [mn + 1, mx + K].
- The pair for which mx is less than ( X – K) and mn is greater than or equal to X will need 2 replacements.
- Simply insert mx and mn for all pairs in two different vectors, sort them and apply binary search to find the number of pairs for which 2 replacements are required.
- Find the pairs which do not need any replacement by using Map by storing the frequencies of each sum.
- Finally number of pairs which require one replacement can be calculated by the equation (N / 2 – (pairs which need 2 replacements + pairs which need no replacement) ).
Follow these steps to find the solve the problem:
- Find maximum and minimum for all pairs arr [ i ], arr [ N – i – 1 ] and store them in max_values and min_values vectors respectively. Also store the frequencies of sum of all such pairs in a map.
- Sort the vectors max_values and min_values.
- Iterate from X = 2 to X = 2 * K, and for every value of X find the total number of replacements as described in the approach above. Update the answer as:
answer = min ( answer, 2 * (number of pairs for which we need to make 2 replacements) + (number of pairs for which we need to make one replacement) ).
Illustration :
arr[] = { 1, 2, 2, 1 }, K = 3
max_values = {1, 2}
min_values = {1, 2}
map = {{2, 1}, {4, 1}}
At X = 4, minimum replacement is required, that is, only 1 replacement, either conversion arr[0] to 3 or arr[3] to 3, is required to make the sum of all pairs equal to 4.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define int long long int
using namespace std;
const int inf = 1e18;
int minimumReplacement( int * arr, int N,
int K)
{
int ans = inf;
vector< int > max_values;
vector< int > min_values;
map< int , int > sum_equal_to_x;
for ( int i = 0; i < N / 2; i++) {
int mn = min(arr[i], arr[N - i - 1]);
int mx = max(arr[i], arr[N - i - 1]);
sum_equal_to_x[arr[i]
+ arr[N - i - 1]]++;
min_values.push_back(mn);
max_values.push_back(mx);
}
sort(max_values.begin(),
max_values.end());
sort(min_values.begin(),
min_values.end());
for ( int x = 2; x <= 2 * K; x++) {
int mp1
= lower_bound(max_values.begin(),
max_values.end(), x - K)
- max_values.begin();
int mp2
= lower_bound(min_values.begin(),
min_values.end(), x)
- min_values.begin();
int rep2 = mp1 + (N / 2 - mp2);
int rep0 = sum_equal_to_x[x];
int rep1 = (N / 2 - rep2 - rep0);
ans = min(ans, rep2 * 2 + rep1);
}
return ans;
}
int32_t main()
{
int N = 4;
int K = 3;
int arr[] = { 1, 2, 2, 1 };
cout << minimumReplacement(arr, N, K);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static int inf = ( int )1e18;
static int minimumReplacement( int [] arr, int N,
int K)
{
int ans = inf;
ArrayList<Integer> max_values = new ArrayList<>();
ArrayList<Integer> min_values = new ArrayList<>();
Map<Integer,
Integer> sum_equal_to_x = new HashMap<>();
for ( int i = 0 ; i < N / 2 ; i++)
{
int mn = Math.min(arr[i], arr[N - i - 1 ]);
int mx = Math.max(arr[i], arr[N - i - 1 ]);
sum_equal_to_x.put(arr[i] +
arr[N - i - 1 ],
sum_equal_to_x.getOrDefault(arr[i] +
arr[N - i - 1 ],
0 ) + 1 );
min_values.add(mn);
max_values.add(mx);
}
Collections.sort(max_values);
Collections.sort(min_values);
for ( int x = 2 ; x <= 2 * K; x++)
{
int mp1 = max_values.indexOf(x - K);
int mp2 = min_values.indexOf(x);
int rep2 = mp1 + (N / 2 - mp2);
int rep0 = sum_equal_to_x.getOrDefault(x, - 1 );
int rep1 = (N / 2 - rep2 - rep0);
ans = Math.min(ans, rep2 * 2 + rep1);
}
return ans;
}
public static void main (String[] args)
{
int N = 4 ;
int K = 3 ;
int arr[] = { 1 , 2 , 2 , 1 };
System.out.print(minimumReplacement(arr, N, K));
}
}
|
Python3
inf = 10 * * 18
def firstOccurrence(numbers, length,
searchnum):
answer = - 1
start = 0
end = length - 1
while start < = end:
middle = (start + end) / / 2
if numbers[middle] = = searchnum:
answer = middle
end = middle - 1
elif numbers[middle] > searchnum:
end = middle - 1
else :
start = middle + 1
return answer
def minimumReplacement(arr, N, K):
ans = inf
max_values = []
min_values = []
sum_equal_to_x = dict ()
for i in range (N / / 2 ):
mn = min (arr[i], arr[N - i - 1 ])
mx = max (arr[i], arr[N - i - 1 ])
if (arr[i] + arr[N - i - 1 ] not in sum_equal_to_x):
sum_equal_to_x[arr[i] + arr[N - i - 1 ]] = 0
sum_equal_to_x[arr[i] + arr[N - i - 1 ]] + = 1
min_values.append(mn)
max_values.append(mx)
max_values.sort()
min_values.sort()
for x in range ( 2 , 2 * K + 1 ):
mp1 = firstOccurrence(
max_values, len (max_values), x - K)
mp2 = firstOccurrence(
min_values, len (min_values), x)
rep2 = mp1 + (N / / 2 - mp2)
if x in sum_equal_to_x:
rep0 = sum_equal_to_x[x]
else :
rep0 = 0
rep1 = (N / / 2 - rep2 - rep0)
ans = min (ans, rep2 * 2 + rep1)
return ans
if __name__ = = '__main__' :
N = 4
K = 3
arr = [ 1 , 2 , 2 , 1 ]
print (minimumReplacement(arr, N, K))
|
C#
using System;
using System.Collections;
using System.Collections.Generic;
class GFG{
static int minimumReplacement( int [] arr, int N,
int K)
{
int ans = int .MaxValue;
ArrayList max_values = new ArrayList();
ArrayList min_values = new ArrayList();
Dictionary< int ,
int > sum_equal_to_x = new Dictionary< int ,
int >();
for ( int i = 0; i < N / 2; i++)
{
int mn = Math.Min(arr[i], arr[N - i - 1]);
int mx = Math.Max(arr[i], arr[N - i - 1]);
sum_equal_to_x[arr[i] + arr[N - i - 1]] =
sum_equal_to_x.GetValueOrDefault(arr[i] +
arr[N - i - 1], 0) + 1;
min_values.Add(mn);
max_values.Add(mx);
}
max_values.Sort();
min_values.Sort();
for ( int x = 2; x <= 2 * K; x++)
{
int mp1 = max_values.IndexOf(x - K);
int mp2 = min_values.IndexOf(x);
int rep2 = mp1 + (N / 2 - mp2);
int rep0 = sum_equal_to_x.GetValueOrDefault(
x, -1);
int rep1 = (N / 2 - rep2 - rep0);
ans = Math.Min(ans, rep2 * 2 + rep1);
}
return ans;
}
public static void Main( string [] args)
{
int N = 4;
int K = 3;
int []arr = { 1, 2, 2, 1 };
Console.Write(minimumReplacement(arr, N, K));
}
}
|
Javascript
<script>
const inf = 10**18
function firstOccurrence(numbers, length, searchnum)
{
let answer = -1
let start = 0
let end = length - 1
while (start <= end){
let middle = Math.floor((start + end)/2)
if (numbers[middle] == searchnum){
answer = middle
end = middle - 1
}
else if (numbers[middle] > searchnum)
end = middle - 1
else
start = middle + 1
}
return answer
}
function minimumReplacement(arr, N, K){
let ans = inf
let max_values = []
let min_values = []
let sum_equal_to_x = new Map()
for (let i=0;i<Math.floor(N / 2);i++){
let mn = Math.min(arr[i], arr[N - i - 1])
let mx = Math.max(arr[i], arr[N - i - 1])
if (sum_equal_to_x.has(arr[i] + arr[N - i - 1])== false )
sum_equal_to_x.set(arr[i] + arr[N - i - 1],0)
sum_equal_to_x.set(arr[i] + arr[N - i - 1],
sum_equal_to_x.get(arr[i] + arr[N - i -1])+1)
min_values.push(mn)
max_values.push(mx)
}
max_values.sort()
min_values.sort()
for (let x=2;x<2 * K + 1;x++){
let mp1 = firstOccurrence(max_values, max_values.length, x - K)
let mp2 = firstOccurrence(min_values, min_values.length, x)
rep2 = mp1 + (Math.floor(N / 2) - mp2)
if (sum_equal_to_x.has(x)){
rep0 = sum_equal_to_x.get(x)
}
else
rep0 = 0
rep1 = Math.floor(N /2) - rep2 - rep0
ans = Math.min(ans, rep2 * 2 + rep1)
}
return ans
}
let N = 4
let K = 3
let arr = [ 1, 2, 2, 1 ]
document.write(minimumReplacement(arr, N, K))
</script>
|
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Last Updated :
29 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...