Minimize square sum of given Arrays by swapping elements at same indices
Given two arrays arrA[] and arrB[] containing N integers each. Perform the following operation any number of times (possibly zero):
- Select any index i (0 <= i <= N-1) and
- Swap arrA[i] and arrB[i].
The task is to find the minimum sum of the square of array sums i.e. if Sa and Sb are the array sum of arrA[] and arrB[] after swapping, then find the minimum possible value of (Sa)2 + (Sb)2.
Examples:
Input : N = 4, arrA[ ] = { 3, 6, 6, 6 }, arrB[ ] = { 2, 7, 4, 1 }
Output: 613
Explanation :
If we perform the operation for i = 0 and i = 2, we get the resultant arrays as arrA[] = { 2, 6, 4, 6 } and arrB[] = { 3, 7, 6, 1 }.
Thus Sa = 18 and Sb = 17.
So, sum of their squares is (18)2 + (17)2 = 613, which is the minimum possible.
Input : N = 4, arrA[ ] = { 6, 7, 2, 4 }, arrB[ ] = { 2, 5, 3, 5 }
Output: 578
Naive Approach: The naive approach is to check all possible cases. For each index:
- Either swap those elements of that index or not.
- Calculate the sum of the arrays.
- Find the value of sum of square of array sums.
- The minimum of the values is the answer.
Time Complexity: O(2N * N)
Auxiliary Space: O(1)
Efficient Approach: The efficient approach is based on the following idea:
Use memoization to store the already calculated result up to an index and some array sum which will prevent repeated calculation and reduce the overall time.
Follow the steps mentioned below to implement the above idea:
- Create a dp[] array to store the calculated sum of squares of array sum up to some index i and array sums Sa and Sb.
- For each index there are two choices: either swap the elements or not.
- Perform this swap operation recursively and:
- If for any index the value is already calculated then return that value.
- Otherwise, calculate the sum and store the minimum value possible for that index.
- Return the minimum sum among all as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
map<vector< int >, int > dp;
int min_total_square_sum( int arrA[], int arrB[],
int i, int Sa,
int Sb, int n)
{
if (i >= n) {
int temp = Sa * Sa + Sb * Sb;
return temp;
}
vector< int > v = { i, Sa, Sb };
if (dp.count(v)) {
return dp[v];
}
int t1 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrA[i],
Sb + arrB[i], n);
int t2 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrB[i],
Sb + arrA[i], n);
return dp[v] = min(t1, t2);
}
int main()
{
int N = 4;
int arrA[] = { 6, 7, 2, 4 };
int arrB[] = { 2, 5, 3, 5 };
int Sa{ 0 }, Sb{ 0 };
cout << min_total_square_sum(arrA, arrB,
0, Sa, Sb, N);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
public static TreeMap<ArrayList<Integer>, Integer> dp = new TreeMap<ArrayList<Integer>, Integer>( new Comparator<ArrayList<Integer>>() {
public int compare(ArrayList<Integer> o1, ArrayList<Integer> o2){
for ( int i= 0 ; i< 2 ; i++){
if (o1.get(i).equals(o2.get(i))) continue ;
return o1.get(i).compareTo(o2.get(i));
}
return o1.get( 2 ).compareTo(o2.get( 2 ));
}
});
public static int min_total_square_sum(ArrayList<Integer> arrA, ArrayList<Integer> arrB, int i, int Sa, int Sb, int n){
if (i>=n){
int temp = Sa * Sa + Sb * Sb;
return temp;
}
ArrayList<Integer> v = new ArrayList<Integer>(
List.of(i, Sa, Sb)
);
if (dp.containsKey(v)){
return dp.get(v);
}
int t1 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrA.get(i), Sb + arrB.get(i), n);
int t2 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrB.get(i), Sb + arrA.get(i), n);
dp.put(v, Math.min(t1,t2));
return dp.get(v);
}
public static void main(String args[])
{
int N = 4 ;
ArrayList<Integer> arrA = new ArrayList<Integer>(
List.of( 6 , 7 , 2 , 4 )
);
ArrayList<Integer> arrB = new ArrayList<Integer>(
List.of( 2 , 5 , 3 , 5 )
);
int Sa = 0 , Sb = 0 ;
System.out.println(min_total_square_sum(arrA, arrB, 0 , Sa, Sb, N));
}
}
|
Python3
dp = {}
def min_total_square_sum(arrA, arrB, i, Sa, Sb, n):
if (i > = n):
temp = Sa * Sa + Sb * Sb
return temp
v = (i, Sa, Sb)
if (v in dp):
return dp[v]
t1 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrA[i],
Sb + arrB[i], n)
t2 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrB[i],
Sb + arrA[i], n)
dp[v] = min (t1, t2)
return dp[v]
if __name__ = = "__main__" :
N = 4
arrA = [ 6 , 7 , 2 , 4 ]
arrB = [ 2 , 5 , 3 , 5 ]
Sa, Sb = 0 , 0
print (min_total_square_sum(arrA, arrB, 0 , Sa, Sb, N))
|
Javascript
let dp = new Map();
function min_total_square_sum(arrA, arrB,
i, Sa, Sb, n)
{
if (i >= n) {
let temp = Sa * Sa + Sb * Sb;
return temp;
}
let v = [i, Sa, Sb];
if (dp.has(v)) {
return dp.get(vs);
}
let t1 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrA[i],
Sb + arrB[i], n);
let t2 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrB[i],
Sb + arrA[i], n);
dp.set(v, Math.min(t1, t2))
return dp.get(v);
}
let N = 4;
let arrA = [6, 7, 2, 4];
let arrB = [2, 5, 3, 5];
let Sa = 0, Sb = 0;
console.log(min_total_square_sum(arrA, arrB,
0, Sa, Sb, N));
|
C#
using System;
using System.Collections.Generic;
class Program
{
static Dictionary<Tuple< int , int , int >, int > dp = new Dictionary<Tuple< int , int , int >, int >();
static int min_total_square_sum( int [] arrA, int [] arrB, int i, int Sa, int Sb, int n)
{
if (i >= n)
{
int temp = Sa * Sa + Sb * Sb;
return temp;
}
Tuple< int , int , int > v = Tuple.Create(i, Sa, Sb);
if (dp.ContainsKey(v))
{
return dp[v];
}
int t1 = min_total_square_sum(arrA, arrB, i + 1, Sa + arrA[i], Sb + arrB[i], n);
int t2 = min_total_square_sum(arrA, arrB, i + 1, Sa + arrB[i], Sb + arrA[i], n);
return dp[v] = Math.Min(t1, t2);
}
static void Main( string [] args)
{
int N = 4;
int [] arrA = { 6, 7, 2, 4 };
int [] arrB = { 2, 5, 3, 5 };
int Sa = 0, Sb = 0;
Console.WriteLine(min_total_square_sum(arrA, arrB, 0, Sa, Sb, N));
}
}
|
Time Complexity: O(N3)
Auxiliary Space: O(N3)
Last Updated :
23 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...