Find the minimum value from an array associated with another array
Last Updated :
19 Dec, 2022
Given an integer array A[] and a character array B[] of equal lengths where every character of the array is from the set {‘a’, ‘b’, ‘c’}. Elements of both arrays are associated with each other i.e. the value of B[i] is linked to A[i] for all valid values of i. The task is to find the value min(a + b, c).
Examples:
Input: A[] = {3, 6, 4, 5, 6}, B[] = {‘a’, ‘c’, ‘b’, ‘b’, ‘a’}
Output: 6
Input: A[] = {4, 2, 6, 2, 3}, B[] = {‘b’, ‘a’, ‘c’, ‘a’, ‘b’}
Output: 5
Approach: In order to minimize the required value, the values of a, b and c have to be minimized. So, traverse the array and find the minimum values of a, b, and c associated with these characters in the integer array and finally return min(a + b, c).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMinimum( int A[], char B[], int n)
{
int minA = INT_MAX;
int minB = INT_MAX;
int minC = INT_MAX;
for ( int i = 0; i < n; i++) {
switch (B[i]) {
case 'a' :
minA = min(A[i], minA);
break ;
case 'b' :
minB = min(A[i], minB);
break ;
case 'c' :
minC = min(A[i], minC);
break ;
}
}
return min(minA + minB, minC);
}
int main()
{
int A[] = { 4, 2, 6, 2, 3 };
char B[] = { 'b' , 'a' , 'c' , 'a' , 'b' };
int n = sizeof (A) / sizeof (A[0]);
cout << getMinimum(A, B, n);
}
|
Java
class GFG
{
static int getMinimum( int A[], char B[], int n)
{
int minA = Integer.MAX_VALUE;
int minB = Integer.MAX_VALUE;
int minC = Integer.MAX_VALUE;
for ( int i = 0 ; i < n; i++)
{
switch (B[i])
{
case 'a' :
minA = Math.min(A[i], minA);
break ;
case 'b' :
minB = Math.min(A[i], minB);
break ;
case 'c' :
minC = Math.min(A[i], minC);
break ;
}
}
return Math.min(minA + minB, minC);
}
public static void main(String[] args)
{
int A[] = { 4 , 2 , 6 , 2 , 3 };
char B[] = { 'b' , 'a' , 'c' , 'a' , 'b' };
int n = A.length;
System.out.println(getMinimum(A, B, n));
}
}
|
Python3
def getMinimum(A, B, n):
minA = float ( 'inf' );
minB = float ( 'inf' );
minC = float ( 'inf' );
for i in range (n):
if B[i] = = 'a' :
minA = min (A[i], minA)
if B[i] = = 'b' :
minB = min (A[i], minB)
if B[i] = = 'c' :
minB = min (A[i], minC)
return min (minA + minB, minC)
if __name__ = = '__main__' :
A = [ 4 , 2 , 6 , 2 , 3 ]
B = [ 'b' , 'a' , 'c' , 'a' , 'b' ]
n = len (A);
print (getMinimum(A, B, n))
|
C#
using System;
class GFG
{
static int getMinimum( int []A, char []B, int n)
{
int minA = int .MaxValue;
int minB = int .MaxValue;
int minC = int .MaxValue;
for ( int i = 0; i < n; i++)
{
switch (B[i])
{
case 'a' :
minA = Math.Min(A[i], minA);
break ;
case 'b' :
minB = Math.Min(A[i], minB);
break ;
case 'c' :
minC = Math.Min(A[i], minC);
break ;
}
}
return Math.Min(minA + minB, minC);
}
public static void Main()
{
int []A = { 4, 2, 6, 2, 3 };
char []B = { 'b' , 'a' , 'c' , 'a' , 'b' };
int n = A.Length;
Console.WriteLine(getMinimum(A, B, n));
}
}
|
Javascript
<script>
function getMinimum(A, B, n)
{
var minA = 1000000000;
var minB = 1000000000;
var minC = 1000000000;
for ( var i = 0; i < n; i++) {
switch (B[i]) {
case 'a' :
minA = Math.min(A[i], minA);
break ;
case 'b' :
minB = Math.min(A[i], minB);
break ;
case 'c' :
minC = Math.min(A[i], minC);
break ;
}
}
return Math.min(minA + minB, minC);
}
var A = [4, 2, 6, 2, 3 ];
var B = [ 'b' , 'a' , 'c' , 'a' , 'b' ];
var n = A.length;
document.write( getMinimum(A, B, n));
</script>
|
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...