Minimize Cost by selecting Distinct values from Array A
Last Updated :
04 Dec, 2023
Given an array A and cost array B of size N. The task is to determine the minimum cost(given in array B) by choosing all distinct values from array A.
Note: A[i] will cost us B[i]
Examples:
Input: N = 2, A[] = {16, 16}, B[] = {1, 2}
Output: 1
Explanation: Choosing 16 at the first index will be optimal.
Input: N = 3, A[] = {3, 4, 3}, B[] = {3, 2, 1}
Output: 3
Approach: To solve the problem follow the below idea:
Using map data structure, store the elements in A as key, and if we encounter less value in B for the same element, Update it.
Below are the steps involved:
- Declare a map data structure m.
- The key for the map will be valued in A and the value for the map will be cost in B.
- If we encounter the same key in array A:
- Update the corresponding value to minimum of value already existing or current cost.
- Initialize a sum variable.
- Iterate in map:
- Sum will be incremented by values of map
- Return sum.
Below is the implementation of the code:
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int solve( int A[], int B[], int N)
{
unordered_map< int , int > m;
int i = 0;
while (i < N) {
if (m.find(A[i]) != m.end()) {
if (m[A[i]] > B[i]) {
m[A[i]] = B[i];
}
}
else {
m[A[i]] = B[i];
}
i++;
}
int sum = 0;
for ( auto a : m) {
sum += a.second;
}
return sum;
}
int main()
{
int N = 4;
int A[] = { 3, 16, 3, 4 };
int B[] = { 3, 9, 1, 1 };
cout << solve(A, B, N);
return 0;
}
|
Java
import java.util.HashMap;
public class MinimumSum {
static int solve( int [] A, int [] B, int N) {
HashMap<Integer, Integer> map = new HashMap<>();
int i = 0 ;
while (i < N) {
if (map.containsKey(A[i])) {
if (map.get(A[i]) > B[i]) {
map.put(A[i], B[i]);
}
} else {
map.put(A[i], B[i]);
}
i++;
}
int sum = 0 ;
for ( int value : map.values()) {
sum += value;
}
return sum;
}
public static void main(String[] args) {
int N = 4 ;
int [] A = { 3 , 16 , 3 , 4 };
int [] B = { 3 , 9 , 1 , 1 };
System.out.println(solve(A, B, N));
}
}
|
Python3
def solve(A, B, N):
m = {}
i = 0
while i < N:
if A[i] in m:
if m[A[i]] > B[i]:
m[A[i]] = B[i]
else :
m[A[i]] = B[i]
i + = 1
sum_value = 0
for key, value in m.items():
sum_value + = value
return sum_value
if __name__ = = "__main__" :
N = 4
A = [ 3 , 16 , 3 , 4 ]
B = [ 3 , 9 , 1 , 1 ]
print (solve(A, B, N))
|
C#
using System;
using System.Collections.Generic;
public class Program
{
public static int Solve( int [] A, int [] B, int N)
{
Dictionary< int , int > m = new Dictionary< int , int >();
int i = 0;
while (i < N)
{
if (m.ContainsKey(A[i]))
{
if (m[A[i]] > B[i])
{
m[A[i]] = B[i];
}
}
else
{
m[A[i]] = B[i];
}
i++;
}
int sum = 0;
foreach ( var a in m)
{
sum += a.Value;
}
return sum;
}
public static void Main( string [] args)
{
int N = 4;
int [] A = { 3, 16, 3, 4 };
int [] B = { 3, 9, 1, 1 };
Console.WriteLine(Solve(A, B, N));
}
}
|
Javascript
function GFG(A, B, N) {
const m = {};
for (let i = 0; i < N; i++) {
if (m[A[i]] !== undefined) {
if (m[A[i]] > B[i]) {
m[A[i]] = B[i];
}
} else {
m[A[i]] = B[i];
}
}
let sumValue = 0;
for (const key in m) {
if (m.hasOwnProperty(key)) {
sumValue += m[key];
}
}
return sumValue;
}
const N = 4;
const A = [3, 16, 3, 4];
const B = [3, 9, 1, 1];
console.log(GFG(A, B, N));
|
Time Complexity: O(N)
Auxiliary space: O(N)
Share your thoughts in the comments
Please Login to comment...