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++ code to implement the above approach #include <bits/stdc++.h> using namespace std;
// Using map of vector and int in place of // multidimensional array to store calculated // values to prevent memory limitexceed error map<vector< int >, int > dp;
// Function to find min total square of sum // from both arrays int min_total_square_sum( int arrA[], int arrB[],
int i, int Sa,
int Sb, int n)
{ // Base case
if (i >= n) {
int temp = Sa * Sa + Sb * Sb;
return temp;
}
vector< int > v = { i, Sa, Sb };
// If already calculated directly
// return the stored value
if (dp.count(v)) {
return dp[v];
}
// Case-1: when we don't swap the elements
int t1 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrA[i],
Sb + arrB[i], n);
// Case-2: when we swap the elements
int t2 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrB[i],
Sb + arrA[i], n);
// Returning minimum of the two cases
return dp[v] = min(t1, t2);
} // Driver code int main()
{ int N = 4;
int arrA[] = { 6, 7, 2, 4 };
int arrB[] = { 2, 5, 3, 5 };
int Sa{ 0 }, Sb{ 0 };
// Function call
cout << min_total_square_sum(arrA, arrB,
0, Sa, Sb, N);
return 0;
} |
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 ));
}
});
// Function to find remaining element
public static int min_total_square_sum(ArrayList<Integer> arrA, ArrayList<Integer> arrB, int i, int Sa, int Sb, int n){
// Base case
if (i>=n){
int temp = Sa * Sa + Sb * Sb;
return temp;
}
ArrayList<Integer> v = new ArrayList<Integer>(
List.of(i, Sa, Sb)
);
// if already calculated directly
// return the stored value
if (dp.containsKey(v)){
return dp.get(v);
}
// Case-1: when we don't swap the elements
int t1 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrA.get(i), Sb + arrB.get(i), n);
// Case-2: when we swap the elements
int t2 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrB.get(i), Sb + arrA.get(i), n);
// Returning minimum of the two cases
dp.put(v, Math.min(t1,t2));
return dp.get(v);
}
// Driver code
public static void main(String args[])
{
// Size of array
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 ;
// Function call
System.out.println(min_total_square_sum(arrA, arrB, 0 , Sa, Sb, N));
}
} // This code is contributed by subhamgoyal2014. |
# Python3 code to implement the above approach # Using map of vector and int in place of # multidimensional array to store calculated # values to prevent memory limitexceed error dp = {}
# Function to find min total square of sum # from both arrays def min_total_square_sum(arrA, arrB, i, Sa, Sb, n):
# Base case
if (i > = n):
temp = Sa * Sa + Sb * Sb
return temp
v = (i, Sa, Sb)
# If already calculated directly
# return the stored value
if (v in dp):
return dp[v]
# Case-1: when we don't swap the elements
t1 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrA[i],
Sb + arrB[i], n)
# Case-2: when we swap the elements
t2 = min_total_square_sum(arrA, arrB, i + 1 , Sa + arrB[i],
Sb + arrA[i], n)
# Returning minimum of the two cases
dp[v] = min (t1, t2)
return dp[v]
# Driver code if __name__ = = "__main__" :
N = 4
arrA = [ 6 , 7 , 2 , 4 ]
arrB = [ 2 , 5 , 3 , 5 ]
Sa, Sb = 0 , 0
# Function call
print (min_total_square_sum(arrA, arrB, 0 , Sa, Sb, N))
# This code is contributed by rakeshsahni
|
// JavaScript code for the above approach
// Using map of vector and int in place of
// multidimensional array to store calculated
// values to prevent memory limitexceed error
let dp = new Map();
// Function to find min total square of sum
// from both arrays
function min_total_square_sum(arrA, arrB,
i, Sa, Sb, n)
{
// Base case
if (i >= n) {
let temp = Sa * Sa + Sb * Sb;
return temp;
}
let v = [i, Sa, Sb];
// If already calculated directly
// return the stored value
if (dp.has(v)) {
return dp.get(vs);
}
// Case-1: when we don't swap the elements
let t1 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrA[i],
Sb + arrB[i], n);
// Case-2: when we swap the elements
let t2 = min_total_square_sum(
arrA, arrB, i + 1, Sa + arrB[i],
Sb + arrA[i], n);
// Returning minimum of the two cases
dp.set(v, Math.min(t1, t2))
return dp.get(v);
}
// Driver code
let N = 4;
let arrA = [6, 7, 2, 4];
let arrB = [2, 5, 3, 5];
let Sa = 0, Sb = 0;
// Function call
console.log(min_total_square_sum(arrA, arrB,
0, Sa, Sb, N));
// This code is contributed by Potta Lokesh |
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)
{
// Base case
if (i >= n)
{
int temp = Sa * Sa + Sb * Sb;
return temp;
}
Tuple< int , int , int > v = Tuple.Create(i, Sa, Sb);
// If already calculated directly
// return the stored value
if (dp.ContainsKey(v))
{
return dp[v];
}
// Case-1: when we don't swap the elements
int t1 = min_total_square_sum(arrA, arrB, i + 1, Sa + arrA[i], Sb + arrB[i], n);
// Case-2: when we swap the elements
int t2 = min_total_square_sum(arrA, arrB, i + 1, Sa + arrB[i], Sb + arrA[i], n);
// Returning minimum of the two cases
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;
// Function call
Console.WriteLine(min_total_square_sum(arrA, arrB, 0, Sa, Sb, N));
}
} |
578
Time Complexity: O(N3)
Auxiliary Space: O(N3)