Find minimum pair sum by selecting element from 2nd array at index larger than 1st array
Last Updated :
28 Mar, 2022
Given two arrays A[] and B[] of size N each, the task is to minimize A[i] + B[j] such that j ≥ i.
Examples:
Input: A[] = {34, 12, 45, 10, 86, 39, 77},
B[] = {5, 42, 29, 63, 30, 33, 20}
Output: 30
Explanation: For minimizing the sum, take i = 3 and j = 6.
Input: A[] = {34, 17, 45, 10, 19},
B[] = {2, 13, 7, 11, 16}
Output: 21
Explanation: For minimizing the sum, take both i and j = 3.
Naive Approach: The naive approach to solve this problem is to use 2 for loops, one for iterating over A[] and another for iterating over B[]. Just check and compare for all the possibilities to minimize the sum.
Below is the implementation of the above naive approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimumCost( int A[], int B[], int N)
{
int minimuPrice = INT_MAX;
for ( int i = 0; i < N; i++) {
for ( int j = i; j < N; j++) {
int currentPrice = A[i] + B[j];
minimuPrice = min(minimuPrice,
currentPrice);
}
}
return minimuPrice;
}
int main()
{
int A[] = { 34, 12, 45, 10, 86, 39, 77 };
int B[] = { 5, 42, 29, 63, 30, 33, 20 };
int N = sizeof (A) / sizeof (A[0]);
cout << minimumCost(A, B, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int minimumCost( int [] A,
int [] B,
int N)
{
int minimuPrice = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
for ( int j = i; j < N; j++) {
int currentPrice
= A[i] + B[j];
minimuPrice
= Math.min(minimuPrice,
currentPrice);
}
}
return minimuPrice;
}
public static void main(String[] args)
{
int [] A = { 34 , 12 , 45 , 10 , 86 , 39 , 77 };
int [] B = { 5 , 42 , 29 , 63 , 30 , 33 , 20 };
int N = A.length;
System.out.println(minimumCost(A, B, N));
}
}
|
Python
import sys
def minimumCost(A, B, N):
minimuPrice = sys.maxint
for i in range (N):
for j in range (i, N):
currentPrice = A[i] + B[j]
minimuPrice = min (minimuPrice,
currentPrice)
return minimuPrice;
if __name__ = = "__main__" :
A = [ 34 , 12 , 45 , 10 , 86 , 39 , 77 ]
B = [ 5 , 42 , 29 , 63 , 30 , 33 , 20 ]
N = len (A)
print (minimumCost(A, B, N))
|
C#
using System;
class GFG
{
static int minimumCost( int [] A, int [] B, int N)
{
int minimuPrice = Int32.MaxValue;
for ( int i = 0; i < N; i++) {
for ( int j = i; j < N; j++) {
int currentPrice = A[i] + B[j];
minimuPrice
= Math.Min(minimuPrice, currentPrice);
}
}
return minimuPrice;
}
public static int Main()
{
int [] A = { 34, 12, 45, 10, 86, 39, 77 };
int [] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.Length;
Console.Write(minimumCost(A, B, N));
return 0;
}
}
|
Javascript
<script>
const INT_MAX = 2147483647;
const minimumCost = (A, B, N) => {
let minimuPrice = INT_MAX;
for (let i = 0; i < N; i++) {
for (let j = i; j < N; j++) {
let currentPrice = A[i] + B[j];
minimuPrice = Math.min(minimuPrice,
currentPrice);
}
}
return minimuPrice;
}
let A = [34, 12, 45, 10, 86, 39, 77];
let B = [5, 42, 29, 63, 30, 33, 20];
let N = A.length;
document.write(minimumCost(A, B, N));
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: An efficient way to solve this problem is to create an array that contains the minimum value till ith index from the rear end of B[]. Then traverse through A[] and for each index the minimum sum will be the sum of minimum from starting of A[] and minimum from back of B[] till that index. Follow the steps below to solve the given problem.
- Create an array for storing the minimum value from the rear end of B[] for each index.
- Since there is only one possible value on the last index, assign the last index value of array B[] to the last index of the new array.
- Now, traverse B[] from the rear side and compare the current index (say i) value in B[] with the index (i+1) in the new array and store the minimum value of these two in the ith index of the newly created array.
- Now, iterate over elements of X and look at that index in the new array. Since that index at new array is the smallest possible value present in B[] for i and above.
Below is the implementation of the above efficient approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minimumCost( int A[], int B[], int N)
{
int cheapestPossible[N];
cheapestPossible[N - 1] = B[N - 1];
for ( int i = (N - 2); i >= 0; i--) {
cheapestPossible[i]
= min(cheapestPossible[i + 1],
B[i]);
}
int minimumPrice = INT_MAX;
for ( int i = 0; i < N; i++) {
minimumPrice
= min(minimumPrice,
A[i] + cheapestPossible[i]);
}
return minimumPrice;
}
int main()
{
int A[] = { 34, 12, 45, 10, 86, 39, 77 };
int B[] = { 5, 42, 29, 63, 30, 33, 20 };
int N = sizeof (A) / sizeof (A[0]);
cout << minimumCost(A, B, N);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int minimumCost( int [] A,
int [] B,
int N)
{
int [] cheapestPossible = new int [N];
cheapestPossible[N - 1 ]
= B[N - 1 ];
for ( int i = (N - 2 ); i >= 0 ; i--) {
cheapestPossible[i]
= Math.min(cheapestPossible[i + 1 ],
B[i]);
}
int minimumPrice = Integer.MAX_VALUE;
for ( int i = 0 ; i < N; i++) {
minimumPrice = Math.min(
minimumPrice, A[i]
+ cheapestPossible[i]);
}
return minimumPrice;
}
public static void main(String[] args)
{
int [] A = { 34 , 12 , 45 , 10 , 86 , 39 , 77 };
int [] B = { 5 , 42 , 29 , 63 , 30 , 33 , 20 };
int N = A.length;
System.out.println(minimumCost(A, B, N));
}
}
|
Python3
import sys
def minimumCost(A, B, N):
cheapestPossible = [ 0 ] * N
cheapestPossible[N - 1 ] = B[N - 1 ]
for i in range (N - 2 , - 1 , - 1 ):
cheapestPossible[i] = min (cheapestPossible[i + 1 ],
B[i])
minimumPrice = sys.maxsize
for i in range (N):
minimumPrice = min (minimumPrice,
A[i] + cheapestPossible[i])
return minimumPrice
if __name__ = = "__main__" :
A = [ 34 , 12 , 45 , 10 , 86 , 39 , 77 ]
B = [ 5 , 42 , 29 , 63 , 30 , 33 , 20 ]
N = len (A)
print (minimumCost(A, B, N))
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static int minimumCost( int [] A,
int [] B,
int N)
{
int [] cheapestPossible = new int [N];
cheapestPossible[N - 1]
= B[N - 1];
for ( int i = (N - 2); i >= 0; i--) {
cheapestPossible[i]
= Math.Min(cheapestPossible[i + 1],
B[i]);
}
int minimumPrice = int .MaxValue;
for ( int i = 0; i < N; i++) {
minimumPrice = Math.Min(
minimumPrice, A[i]
+ cheapestPossible[i]);
}
return minimumPrice;
}
public static void Main(String[] args)
{
int [] A = { 34, 12, 45, 10, 86, 39, 77 };
int [] B = { 5, 42, 29, 63, 30, 33, 20 };
int N = A.Length;
Console.WriteLine(minimumCost(A, B, N));
}
}
|
Javascript
<script>
function minimumCost(A, B, N)
{
var cheapestPossible = Array.from({length: N}, (_, i) => 0);
cheapestPossible[N - 1]
= B[N - 1];
for ( var i = (N - 2); i >= 0; i--) {
cheapestPossible[i]
= Math.min(cheapestPossible[i + 1],
B[i]);
}
var minimumPrice = Number.MAX_VALUE;
for ( var i = 0; i < N; i++) {
minimumPrice = Math.min(
minimumPrice, A[i]
+ cheapestPossible[i]);
}
return minimumPrice;
}
var A = [ 34, 12, 45, 10, 86, 39, 77 ];
var B = [ 5, 42, 29, 63, 30, 33, 20 ];
var N = A.length;
document.write(minimumCost(A, B, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...