Minimum sum of two elements from two arrays such that indexes are not same
Last Updated :
12 Oct, 2023
Given two arrays a[] and b[] of same size. Task is to find minimum sum of two elements such that they belong to different arrays and are not at same index in their arrays.
Examples:
Input : a[] = {5, 4, 13, 2, 1}
b[] = {2, 3, 4, 6, 5}
Output : 3
We take 1 from a[] and 2 from b[]
Sum is 1 + 2 = 3.
Input : a[] = {5, 4, 13, 1}
b[] = {3, 2, 6, 1}
Output : 3
We take 1 from a[] and 2 from b[].
Note that we can't take 1 from b[]
as the elements can not be at same
index.
A simple solution is to consider every element of a[], form its pair with all elements of b[] at indexes different from its index and compute sums. Finally return the minimum sum. Time complexity of this solution is O(n2)
An efficient solution works in O(n) time. Below are steps.
- Find minimum elements from a[] and b[]. Let these elements be minA and minB respectively.
- If indexes of minA and minB are not same, return minA + minB.
- Else find second minimum elements from two arrays. Let these elements be minA2 and minB2. Return min(minA + minB2, minA2 + minB)
Below is the implementation of above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int minSum( int a[], int b[], int n)
{
int minA = a[0], indexA;
for ( int i=1; i<n; i++)
{
if (a[i] < minA)
{
minA = a[i];
indexA = i;
}
}
int minB = b[0], indexB;
for ( int i=1; i<n; i++)
{
if (b[i] < minB)
{
minB = b[i];
indexB = i;
}
}
if (indexA != indexB)
return (minA + minB);
int minA2 = INT_MAX, indexA2;
for ( int i=0; i<n; i++)
{
if (i != indexA && a[i] < minA2)
{
minA2 = a[i];
indexA2 = i;
}
}
int minB2 = INT_MAX, indexB2;
for ( int i=0; i<n; i++)
{
if (i != indexB && b[i] < minB2)
{
minB2 = b[i];
indexB2 = i;
}
}
return min(minB + minA2, minA + minB2);
}
int main()
{
int a[] = {5, 4, 3, 8, 1};
int b[] = {2, 3, 4, 2, 1};
int n = sizeof (a)/ sizeof (a[0]);
cout << minSum(a, b, n);
return 0;
}
|
Java
class Minimum{
public static int minSum( int a[], int b[], int n)
{
int minA = a[ 0 ], indexA = 0 ;
for ( int i= 1 ; i<n; i++)
{
if (a[i] < minA)
{
minA = a[i];
indexA = i;
}
}
int minB = b[ 0 ], indexB = 0 ;
for ( int i= 1 ; i<n; i++)
{
if (b[i] < minB)
{
minB = b[i];
indexB = i;
}
}
if (indexA != indexB)
return (minA + minB);
int minA2 = Integer.MAX_VALUE, indexA2 = 0 ;
for ( int i= 0 ; i<n; i++)
{
if (i != indexA && a[i] < minA2)
{
minA2 = a[i];
indexA2 = i;
}
}
int minB2 = Integer.MAX_VALUE, indexB2 = 0 ;
for ( int i= 0 ; i<n; i++)
{
if (i != indexB && b[i] < minB2)
{
minB2 = b[i];
indexB2 = i;
}
}
return Math.min(minB + minA2, minA + minB2);
}
public static void main(String[] args)
{
int a[] = { 5 , 4 , 3 , 8 , 1 };
int b[] = { 2 , 3 , 4 , 2 , 1 };
int n = 5 ;
System.out.print(minSum(a, b, n));
}
}
|
Python3
import sys
def minSum(a, b, n):
minA = a[ 0 ]
indexA = 0
for i in range ( 1 ,n):
if a[i] < minA:
minA = a[i]
indexA = i
minB = b[ 0 ]
indexB = 0
for i in range ( 1 , n):
if b[i] < minB:
minB = b[i]
indexB = i
if indexA ! = indexB:
return (minA + minB)
minA2 = sys.maxsize
indexA2 = 0
for i in range (n):
if i ! = indexA and a[i] < minA2:
minA2 = a[i]
indexA2 = i
minB2 = sys.maxsize
indexB2 = 0
for i in range (n):
if i ! = indexB and b[i] < minB2:
minB2 = b[i]
indexB2 = i
return min (minB + minA2, minA + minB2)
a = [ 5 , 4 , 3 , 8 , 1 ]
b = [ 2 , 3 , 4 , 2 , 1 ]
n = len (a)
print (minSum(a, b, n))
|
C#
using System;
public class GFG {
static int minSum( int []a, int []b,
int n)
{
int minA = a[0], indexA = 0;
for ( int i = 1; i < n; i++)
{
if (a[i] < minA)
{
minA = a[i];
indexA = i;
}
}
int minB = b[0], indexB = 0;
for ( int i = 1; i < n; i++)
{
if (b[i] < minB)
{
minB = b[i];
indexB = i;
}
}
if (indexA != indexB)
return (minA + minB);
int minA2 = int .MaxValue;
for ( int i=0; i<n; i++)
{
if (i != indexA && a[i] < minA2)
{
minA2 = a[i];
}
}
int minB2 = int .MaxValue;
for ( int i=0; i<n; i++)
if (i != indexB && b[i] < minB2)
minB2 = b[i];
return Math.Min(minB + minA2,
minA + minB2);
}
public static void Main()
{
int []a = {5, 4, 3, 8, 1};
int []b = {2, 3, 4, 2, 1};
int n = 5;
Console.Write(minSum(a, b, n));
}
}
|
Javascript
<script>
function minSum(a, b, n)
{
let minA = a[0], indexA;
for (let i=1; i<n; i++)
{
if (a[i] < minA)
{
minA = a[i];
indexA = i;
}
}
let minB = b[0], indexB;
for (let i=1; i<n; i++)
{
if (b[i] < minB)
{
minB = b[i];
indexB = i;
}
}
if (indexA != indexB)
return (minA + minB);
let minA2 = Number.MAX_SAFE_INTEGER, indexA2;
for (let i=0; i<n; i++)
{
if (i != indexA && a[i] < minA2)
{
minA2 = a[i];
indexA2 = i;
}
}
let minB2 = Number.MAX_SAFE_INTEGER, indexB2;
for (let i=0; i<n; i++)
{
if (i != indexB && b[i] < minB2)
{
minB2 = b[i];
indexB2 = i;
}
}
return Math.min(minB + minA2, minA + minB2);
}
let a = [5, 4, 3, 8, 1];
let b = [2, 3, 4, 2, 1];
let n = a.length;
document.write(minSum(a, b, n));
</script>
|
PHP
<?php
function minSum( $a , $b , $n )
{
$minA = $a [0];
for ( $i = 1; $i < $n ; $i ++)
{
if ( $a [ $i ] < $minA )
{
$minA = $a [ $i ];
$indexA = $i ;
}
}
$minB = $b [0];
for ( $i = 1; $i < $n ; $i ++)
{
if ( $b [ $i ] < $minB )
{
$minB = $b [ $i ];
$indexB = $i ;
}
}
if ( $indexA != $indexB )
return ( $minA + $minB );
$minA2 = 9999999;
$indexA2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $i != $indexA &&
$a [ $i ] < $minA2 )
{
$minA2 = $a [ $i ];
$indexA2 = $i ;
}
}
$minB2 = 999999;
$indexB2 = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $i != $indexB &&
$b [ $i ] < $minB2 )
{
$minB2 = $b [ $i ];
$indexB2 = $i ;
}
}
return min( $minB + $minA2 ,
$minA + $minB2 );
}
$a = array (5, 4, 3, 8, 1);
$b = array (2, 3, 4, 2, 1);
$n = count ( $a );
echo minSum( $a , $b , $n );
?>
|
Time Complexity : O(n)
Auxiliary Space : O(1)
New approach:- Here , Another approach to solve this problem is by using sorting. We can sort both arrays in non-decreasing order and then find the minimum sum by taking the sum of the first two elements of the sorted arrays.
Algorithm:
- Define a function minSum which takes input arrays a, b and their length n as arguments.
- Sort both arrays a and b in non-decreasing order using the Arrays.sort method.
- Initialize a variable minSum to Integer.MAX_VALUE.
- Initialize two variables i and j to 0.
- Use a while loop to iterate over the arrays a and b until either i or j is less than n.
- Check if the indexes of the current elements being compared are not the same, calculate their sum and if it is less than the current minSum, update the minSum.
- If the element in a at index i is less than the element in b at index j, increment i by 1. Otherwise, increment j by 1.
- Return the minimum sum calculated in step 3.
- In the main method, define two arrays a and b, set their values and their length n.
- Call the minSum function with arrays a, b and n as arguments and print the result.
Here is the implementation of this approach:-
C++
#include <bits/stdc++.h>
#include <algorithm>
#include <vector>
using namespace std;
int minSum(vector< int >& a, vector< int >& b, int n) {
sort(a.begin(), a.end());
sort(b.begin(), b.end());
int minSum = INT_MAX;
int i = 0, j = 0;
while (i < n && j < n) {
if (i != j) {
int sum = a[i] + b[j];
if (sum < minSum) {
minSum = sum;
}
}
if (a[i] < b[j]) {
i++;
} else {
j++;
}
}
return minSum;
}
int main() {
vector< int > a = {5, 4, 3, 8, 1};
vector< int > b = {2, 3, 4, 2, 1};
int n = 5;
cout << minSum(a, b, n) << endl;
return 0;
}
|
Java
import java.util.Arrays;
class Minimum {
public static int minSum( int a[], int b[], int n) {
Arrays.sort(a);
Arrays.sort(b);
int minSum = Integer.MAX_VALUE;
int i = 0 , j = 0 ;
while (i < n && j < n) {
if (i != j) {
int sum = a[i] + b[j];
if (sum < minSum) {
minSum = sum;
}
}
if (a[i] < b[j]) {
i++;
} else {
j++;
}
}
return minSum;
}
public static void main(String[] args) {
int a[] = { 5 , 4 , 3 , 8 , 1 };
int b[] = { 2 , 3 , 4 , 2 , 1 };
int n = 5 ;
System.out.print(minSum(a, b, n));
}
}
|
Python
def minSum(a, b, n):
a.sort()
b.sort()
minSum = float ( 'inf' )
i, j = 0 , 0
while i < n and j < n:
if i ! = j:
sum_val = a[i] + b[j]
if sum_val < minSum:
minSum = sum_val
if a[i] < b[j]:
i + = 1
else :
j + = 1
return minSum
a = [ 5 , 4 , 3 , 8 , 1 ]
b = [ 2 , 3 , 4 , 2 , 1 ]
n = 5
print (minSum(a, b, n))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static int MinSum(List< int > a, List< int > b, int n)
{
a.Sort();
b.Sort();
int minSum = int .MaxValue;
int i = 0, j = 0;
while (i < n && j < n) {
if (i != j) {
int sum = a[i] + b[j];
if (sum < minSum) {
minSum = sum;
}
}
if (a[i] < b[j]) {
i++;
}
else {
j++;
}
}
return minSum;
}
static void Main( string [] args)
{
List< int > a = new List< int >{ 5, 4, 3, 8, 1 };
List< int > b = new List< int >{ 2, 3, 4, 2, 1 };
int n = 5;
Console.WriteLine(MinSum(a, b, n));
}
}
|
Javascript
function minSum(a, b, n) {
a.sort((x, y) => x - y);
b.sort((x, y) => x - y);
let minSum = Number.MAX_SAFE_INTEGER;
let i = 0, j = 0;
while (i < n && j < n) {
if (i !== j) {
const sum = a[i] + b[j];
if (sum < minSum) {
minSum = sum;
}
}
if (a[i] < b[j]) {
i++;
} else {
j++;
}
}
return minSum;
}
const a = [5, 4, 3, 8, 1];
const b = [2, 3, 4, 2, 1];
const n = 5;
console.log(minSum(a, b, n));
|
Output:-
3
Time Complexity:- The time complexity of this approach is O(n log n), dominated by the sorting of the arrays using Arrays.sort() which takes O(n log n) time complexity. The while loop then iterates over both arrays once, which takes O(n) time complexity. Therefore, the overall time complexity is O(n log n).
Auxiliary space:- The auxiliary space complexity is O(1) as we are not using any additional data structures to solve the problem, only some variables to store the minimum sum and the current indices of the arrays.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...